Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COAs far as I understand the Profile concept it is a way to create and manage various, lets say styles, of maps. The mapping per profile still has to be defined in code, I'm looking for a way to configure such a profile outside code/the actual application, like you change parameters in a web.config.
      singulars
    2. COI know what you want to do, and I am telling you that you do not want to do it. But even if you did do it, you would need the XML configuration file in order for your application to run, correct? If that is the case, then the profile config would not be "outside of the actual application", even if it is not done in code. Like I said you can keep these profiles in a different assembly and then include that assembly as one of your app's dependencies, just like you would include the XML file as one of your app's dependencies. I suppose the biggest question I have is WHY do you want it this way?
      singulars
    3. COWHY I have to do it? I received some requirements on the application I have to implemented and one is that the mapping between these objects has to be defined by an operator who is running the application in that way(change after restart is ok). My requirement's interpretation ended in this question (I'm aware of type safety issues, horrible pitfalls in case there are any issues with the XML and so on), in the meanwhile I implemented a small proof-of-concept that is only based on reflection and is working.
      singulars
 

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