Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't want to do what you are asking about. Like @Gruff Bunny says, automapper already has the <code>Profile</code> class which essentially does all of the configuration you are looking for.</p> <p>Why don't you want to do this with an XML (or other configuration) file?</p> <p>Firstly, because you will lose the strongly-typed nature of the automapper configurations. You could write code to parse an XML or any other type of file to read the mappings and then call <code>CreateMap</code> based on the textual mappings. But if you do this, then you really need a unit test for each configuration to make sure no exceptions will be thrown at runtime.</p> <p>Secondly, you say that you want to configure this at runtime. But simply replacing the configuration file will not be sufficient. In order for the <code>CreateMap</code> methods to be invoked again, you need an entry point, which is usually <code>Global.asax</code> in web applications. So after you replace the config file, you will still need to recycle or restart the app for the new config to take place. It won't happen automatically like it does when you replace <code>web.config</code>.</p> <p>Thirdly, it slows startup time of your application when you do this. It is much faster for the <code>CreateMap</code> calls to happen straight from CLR code than to parse text for mappings.</p> <p>How can you accomplish different mapping configurations without an XML or other external text file?</p> <p>With <code>AutoMapper.Profile</code>. There is nothing in AutoMapper or .NET for that matter that says you have to declare your mappings in the same assembly as your application. You could create <code>AutoMapper.Profile</code> classes in another assembly which defines these mappings in a strongly-typed manner. You can then load these <code>Profile</code> classes when you bootstrap automapper. Look for the <a href="https://github.com/danludwig/AutoAutoMapper" rel="nofollow">AutoAutoMapper library in my github account</a> for some helpers that will make this easier.</p> <pre><code>public class CalendarEventProfile : AutoMapper.Profile { public override void Configure() { CreateMap&lt;CalendarEvent, CalendarEventForm&gt;() //.ForMember(d =&gt; d.Title, o =&gt; o.MapFrom(s =&gt; s.Title)) //redundant, not necessary .ForMember(d =&gt; d.EventDate, o =&gt; o.MapFrom(s =&gt; s.EventDate.Date)) .ForMember(d =&gt; d.EventHour, o =&gt; o.MapFrom(s =&gt; s.EventDate.Hour)) .ForMember(d =&gt; d.EventMinute, o =&gt; o.MapFrom(s =&gt; s.EventDate.Minute)) ; } } </code></pre> <p>By writing this class you have essentially externalized the mapping configuration in the same manner that you would have by putting it in an XML file. The biggest and most advantageous difference is that this is typesafe, whereas an XML configuration is not. So it is much easier to debug, test, and maintain. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload