Note that there are some explanatory texts on larger screens.

plurals
  1. POview-model to entity binder
    text
    copied!<p>I have a question that I don't know how to ask it. See; I have an entity like this:</p> <pre><code>public class Person{ public int Id {get; set;} public string FirstName {get; set;} public string LastName {get; set;} } </code></pre> <p>and also have a view-model with same properties as entity:</p> <pre><code>public class PersonModel{ public int Id {get; set;} public string FirstName {get; set;} public string LastName {get; set;} } </code></pre> <p>My question is: how can I create a auto convertor for reading properties from one of them, and create a new another one and write it properties from the first? see:</p> <pre><code>// DAL public class Repository{ public void Save(Person person){ // saving a person entity } } // BLL public class Service{ public void CreatePerson(PersonModel model){ var person = new Person{ Id = model.Id, FirstName = model.FirstName, LastName = model.LastName }; new Repository().Save(person); } } </code></pre> <p>I have to repeat this code for converting all view-models to entities when I want to pass them from service to repository. I know about <code>explicit</code> and <code>implicit</code>, I don't want to use them. I mean I'm searching a way to auto binding them via reflection and generics. for example something like this:</p> <pre><code>public class Binder{ public TOut Bind&lt;TIn, TOut&gt;(TIn inObject){ } } </code></pre> <p>Has everybody any idea that where I should start or where can I find some resources to learning this? Thanks in advanced.</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