This project is read-only.

Entity Translator Service

Assembly  EnterpriseLibraryExtensions.dll
 Namespace  EnterpriseLibraryExtensions.Services.Mapping
 Class  EntityTranslatorService
 From version  1.5

Note: The original idea to the Entity Translator Service originates in the Microsoft Smart Client framework.

The Entity Translator Service makes translation between two entities possible by registering individual translators in the service which in themselves are capable of translating between two types. Having a consistent, well working and decoupled translation service is essential when creating layered applications.

Creating a class which simply maps to and from two different types is enough to make the EntityTranslatorService capable of translating lists, arrays and IEnumerables of one type to the other without any further work.

Create Translator

Assembly  EnterpriseLibraryExtensions.dll
 Namespace  EnterpriseLibraryExtensions.Services.Mapping
 Class  EntityMapperTranslator
 From version  1.5

Derive class from the abstract EntityMapperTranslator<TOneType, TAnotherType>. In this example we will translate from int to string.

  public class MyTranslator : EntityMapperTranslator<int, string>
    protected override string ToAnotherType(IEntityTranslatorService service, int value)
      return value.ToString();

    protected override int ToOneType(IEntityTranslatorService service, string value)
      return Convert.ToInt32(value);

Note in the above example, that the IEntityTranslatorService is provided as a parameter to the translation methods. This is to support translation of aggregated member types.

Registering the Translator

To register the translator and enable the use of it, obtain an instance of EntityTranslatorService and call * RegisterEntityTranslator* method.

var entityTranslatorService = new EntityTranslatorService();
entityTranslatorService.RegisterEntityTranslator(new MyTranslator());


Using the instance created above, we now have a translator registered which maps between int and string.

string translatedString = entityTranslatorService.Translate(10);
int translatedInt = entityTranslatorService.Translate("10");

Integrate with Unity

To use with Unity, simply register the EntityTranslatorService:

var unity = new UnityContainer();
unity.RegisterType<IEntityTranslatorService, EntityTranslatorService>();

Last edited Apr 24, 2012 at 9:07 PM by RasmusTherkelsen, version 10


No comments yet.