Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should really take a look at AutoMapper.</p> <p><a href="http://automapper.org" rel="noreferrer">http://automapper.org</a></p> <p>This is a piece of software that you can include in your solution that will automatically map values from one class to another.</p> <p>It'll map properties with the same name automatically, and is also pretty smart when it comes to child objects. However, it also offers complete mapping control when you need it.</p> <p><strong>EDIT</strong></p> <p>Couple of examples to show how AutoMapper works. Please note I'd never code like this in real life. Brevity!</p> <p>Example classes.</p> <pre><code>// Common scenario. Entity classes that have a connection to the DB. namespace Entities { public class Manager { public virtual int Id { get; set; } public virtual User User { get; set; } public virtual IList&lt;User&gt; Serfs { get; set; } } public class User { public virtual int Id { get; set; } public virtual string Firstname { get; set; } public virtual string Lastname { get; set; } } } // Model class - bit more flattened namespace Models { public class Manager { public int Id { get; set; } public string UserFirstname { get; set; } public string UserLastname { get; set; } public string UserMiddlename { get; set; } } } </code></pre> <p>Typically, you'd have a part of your project to configure all your AutoMapping. With the examples I've just given, you can configure a map between Entities.Manager and Models.Manager like so:-</p> <pre><code>// Tell AutoMapper that this conversion is possible Mapper.CreateMap&lt;Entities.Manager, Models.Manager&gt;(); </code></pre> <p>Then, in your code, you'd use something like this to get a new Models.Manager object from the Entity version.</p> <pre><code>// Map the class var mgr = Map&lt;Entities.Manager, Models.Manager&gt; ( repoManager, new Models.Manager() ); </code></pre> <p>Incidentally, AM is smart enough to resolve a lot of properties automatically if you name things consistently.</p> <p>Example above, UserFirstname and UserLastname should be automatically populated because:-</p> <ul> <li>Manager has a property called User</li> <li>User has properties called Firstname and Lastname</li> </ul> <p>However, the UserMiddlename property in Models.Manager will always be blank after a mapping op between Entities.Manager and Models.Manager, because User does not have a public property called Middlename.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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