Setting Up AutoMapper For Use From A Container
Dusty Candland |
|
I like getting all my service from the container and I like using AutoMapper. So I added the AutoMapper to the container, a WindsorContainer in this case.
The main interface is IMappingEngine, which is implemented by MappingEngine which also implements IMappingEngineRunner. This contains a getter to an IConfiguration interface which is also an IConfigurationExpression. So to replace Mapper you need an instance of IMappingEngine and then you can configure the engine as follows:
public CustomMapper(IMappingEngine engine)
{
engine = engine;
<p style="margin: 0px">
 
</p>
<p style="margin: 0px">
    <span style="color: blue">var</span> runner = </em>engine <span style="color: blue">as</span> <span style="color: #2b91af">IMappingEngineRunner</span>;
</p>
<p style="margin: 0px">
    <span style="color: blue">if</span> (runner == <span style="color: blue">null</span>)
</p>
<p style="margin: 0px">
        <span style="color: blue">return</span>;
</p>
<p style="margin: 0px">
 
</p>
<p style="margin: 0px">
    <span style="color: blue">var</span> configuration = runner.Configuration <span style="color: blue">as</span> <span style="color: #2b91af">IConfigurationExpression</span>;
</p>
<p style="margin: 0px">
    <span style="color: blue">if</span> (configuration == <span style="color: blue">null</span>)
</p>
<p style="margin: 0px">
        <span style="color: blue">return</span>;
</p>
<p style="margin: 0px">
 
</p>
<p style="margin: 0px">
    configuration.CreateMap<<span style="color: #2b91af">IApplication</span>, B.<span style="color: #2b91af">Application</span>>();
</p>
<p style="margin: 0px">
}
</p></p></div>
<p>
Next setting up the container, the MappingEngine needs an IConfiguration, the Configuration needs some IObjectMapper instances.
</p>
<div style="font-size: 10pt; background: white; color: black; font-family: courier new">
<p style="margin: 0px">
<span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">void</span> RegisterMappingEngine()
</p>
<p style="margin: 0px">
{
</p>
<p style="margin: 0px">
    <em>container.Register(<span style="color: #2b91af">AllTypes</span>.Of<<span style="color: #2b91af">IObjectMapper</span>>()</p>
<p style="margin: 0px">
                            .FromAssembly(<span style="color: blue">typeof</span> (<span style="color: #2b91af">IObjectMapper</span>).Assembly)
</p>
<p style="margin: 0px">
                            .Configure(configurer => configurer.Named(configurer.ServiceType.Name))
</p>
<p style="margin: 0px">
                            .Configure(configurer => configurer.LifeStyle.Transient));
</p>
<p style="margin: 0px">
 
</p>
<p style="margin: 0px">
    </em>container.Register(<span style="color: #2b91af">Component</span>.For<<span style="color: #2b91af">IConfiguration</span>>().ImplementedBy<<span style="color: #2b91af">Configuration</span>>()
</p>
<p style="margin: 0px">
                            .LifeStyle.Transient.ServiceOverrides(
</p>
<p style="margin: 0px">
                            <span style="color: #2b91af">ServiceOverride</span>.ForKey(<span style="color: #a31515">"mappers"</span>).Eq(
</p>
<p style="margin: 0px">
                                <span style="color: #a31515">"CustomTypeMapMapper"</span>,
</p>
<p style="margin: 0px">
                                <span style="color: #a31515">"TypeMapMapper"</span>,
</p>
<p style="margin: 0px">
                                <span style="color: #a31515">"NewOrDefaultMapper"</span>,
</p>
<p style="margin: 0px">
                                <span style="color: #a31515">"StringMapper"</span>,
</p>
<p style="margin: 0px">
                                <span style="color: #a31515">"EnumMapper"</span>,
</p>
<p style="margin: 0px">
                                <span style="color: #a31515">"ArrayMapper"</span>,
</p>
<p style="margin: 0px">
                                <span style="color: #a31515">"EnumerableMapper"</span>,
</p>
<p style="margin: 0px">
                                <span style="color: #a31515">"NullableMapper"</span>,
</p>
<p style="margin: 0px">
                                <span style="color: #a31515">"AssignableMapper"</span>)
</p>
<p style="margin: 0px">
                            ));
</p>
<p style="margin: 0px">
 
</p>
<p style="margin: 0px">
    _container.Register(<span style="color: #2b91af">Component</span>.For<<span style="color: #2b91af">IMappingEngine</span>>().ImplementedBy<<span style="color: #2b91af">MappingEngine</span>>()
</p>
<p style="margin: 0px">
                            .LifeStyle.Transient);
</p>
<p style="margin: 0px">
}
</p></p></div>
<p>
These are all set as Transient because each CustomMapper can be Singleton and I don’t want the mappings to over lap between the CustomMappers.
</p></p>
Webmentions
These are webmentions via the IndieWeb and webmention.io. Mention this post from your site: