Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Convention Automapper for two-way Mapping (Entities to/from ViewModels)
    primarykey
    data
    text
    <p><strong>UPDATE:</strong> this stuff has evolved into a nice project, see it at <a href="http://valueinjecter.codeplex.com" rel="nofollow noreferrer">http://valueinjecter.codeplex.com</a> <hr/> check this out, I just wrote a simple automapper, it takes the value from the property with the same name and type of one object and puts it into another, and you can add exceptions (ifs, switch) for each type you may need<br/></p> <p>so tell me what do you think about it ?</p> <p>I did it so I could do something like this:</p> <pre><code>Product –&gt; ProductDTO ProductDTO –&gt; Product </code></pre> <p>that's how it begun:</p> <p><strong>I use the "object" type in my Inputs/Dto/ViewModels for DropDowns because I send to the html a IEnumerable&lt;SelectListItem&gt; and I receive a string array of selected keys back</strong></p> <pre><code> public void Map(object a, object b) { var pp = a.GetType().GetProperties(); foreach (var pa in pp) { var value = pa.GetValue(a, null); // property with the same name in b var pb = b.GetType().GetProperty(pa.Name); if (pb == null) { //no such property in b continue; } if (pa.PropertyType == pb.PropertyType) { pb.SetValue(b, value, null); } } } </code></pre> <p><strong>UPDATE:</strong> the real usage:<br/> the Build methods (Input = Dto):</p> <pre><code> public static TI BuildInput&lt;TI, T&gt;(this T entity) where TI: class, new() { var input = new TI(); input = Map(entity, input) as TI; return input; } public static T BuildEntity&lt;T, TI, TR&gt;(this TI input) where T : class, new() where TR : IBaseAdvanceService&lt;T&gt; { var id = (long)input.GetType().GetProperty("Id").GetValue(input, null); var entity = LocatorConfigurator.Resolve&lt;TR&gt;().Get(id) ?? new T(); entity = Map(input, entity) as T; return entity; } public static TI RebuildInput&lt;T, TI, TR&gt;(this TI input) where T: class, new() where TR : IBaseAdvanceService&lt;T&gt; where TI : class, new() { return input.BuildEntity&lt;T, TI, TR&gt;().BuildInput&lt;TI, T&gt;(); } </code></pre> <p>in the controller:</p> <pre><code> public ActionResult Create() { return View(new Organisation().BuildInput&lt;OrganisationInput, Organisation&gt;()); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(OrganisationInput o) { if (!ModelState.IsValid) { return View(o.RebuildInput&lt;Organisation,OrganisationInput, IOrganisationService&gt;()); } organisationService.SaveOrUpdate(o.BuildEntity&lt;Organisation, OrganisationInput, IOrganisationService&gt;()); return RedirectToAction("Index"); } </code></pre> <p>The real Map method</p> <pre><code>public static object Map(object a, object b) { var lookups = GetLookups(); var propertyInfos = a.GetType().GetProperties(); foreach (var pa in propertyInfos) { var value = pa.GetValue(a, null); // property with the same name in b var pb = b.GetType().GetProperty(pa.Name); if (pb == null) { continue; } if (pa.PropertyType == pb.PropertyType) { pb.SetValue(b, value, null); } else if (lookups.Contains(pa.Name) &amp;&amp; pa.PropertyType == typeof(LookupItem)) { pb.SetValue(b, (pa.GetValue(a, null) as LookupItem).GetSelectList(pa.Name), null); } else if (lookups.Contains(pa.Name) &amp;&amp; pa.PropertyType == typeof(object)) { pb.SetValue(b, pa.GetValue(a, null).ReadSelectItemValue(), null); } else if (pa.PropertyType == typeof(long) &amp;&amp; pb.PropertyType == typeof(Organisation)) { pb.SetValue(b, pa.GetValue&lt;long&gt;(a).ReadOrganisationId(), null); } else if (pa.PropertyType == typeof(Organisation) &amp;&amp; pb.PropertyType == typeof(long)) { pb.SetValue(b, pa.GetValue&lt;Organisation&gt;(a).Id, null); } } return b; } </code></pre>
    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.
 

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