
EmitMapper e IoC: l’unione fa la forza
In soldoni l’ObjectsMapperManager non serve ad una emerita cippa…[TestMethod] [TestCategory( "EmitMapper" )] public void EmitMapper_does_not_user_mapper_manager_to_provide_mappers() {
var actual = false;
ObjectMapperManager mm = new ObjectMapperManager();var m = mm.GetMapper<ReferencedType, ReferencedTypeDto>
(
new DefaultMapConfig()
.ConvertUsing<int, int>( i =>
{
actual = true;
return i;
} )
);var sut = mm.GetMapper<SpecialCategory, SpecialCategoryDto>
(
new DefaultMapConfig()
.DeepMap()
.ConvertUsing<Category, Int32>( ei => ei == null ? -1 : ei.Id )
);var sc = new SpecialCategory()
{
Id = 0,
Name = "Special Category",
Parent = null,
IsValid = true
};sc.ReferencedTypes.Add( new ReferencedType() { Foo = 12 } );var dto = sut.Map( sc );
actual.Should().Be.True();}


Vista l’esperienza fatta con l’estensione per gestire le liste non mi ci sono neanche messo a capire perché, semplicemente ho esteso la mia configurazione custom per supportare l’iniezione di un container per IoC da interrogare per sapere se esiste un mapper per soddisfare la necessità:
Il tutto è definitivamente diventato un estensione per EmitMapper presente in Radical.[TestMethod] [TestCategory( "EmitMapper" )] public void EmitMapper_extendedMapConfig_should_be_able_to_resolve_mappers_using_IServiceProvider() {
var actual = false;
ObjectMapperManager mm = new ObjectMapperManager();IServiceProvider container = new MyServiceProvider( t =>
{
var m = mm.GetMapper<ReferencedType, ReferencedTypeDto>
(
new DefaultMapConfig()
.ConvertUsing<int, int>( i =>
{
return i;
} )
);
actual = true;
return m;
} );var sut = mm.GetMapper<SpecialCategory, SpecialCategoryDto>
(
new MappingConfiguration( mm )
.ResolveSubMappersUsing( container )
.DeepMap()
.ConvertUsing<Category, Int32>( ei => ei == null ? -1 : ei.Id )
);var sc = new SpecialCategory()
{
Id = 0,
Name = "Special Category",
Parent = null,
IsValid = true
};sc.ReferencedTypes.Add( new ReferencedType() { Foo = 12 } );var dto = sut.Map( sc );
actual.Should().Be.True();}
.m