title: Entity Framework - MVC - Converting data types requested from database in Model (POCO objects) into business objects used in the View
body: <p>I am using MVC pattern and entity framework 4.1. I have three projects: Model (M) , View (V) and Controller (C).</p>
  
  <pre><code>                         M &lt;-------- V --------&gt; C
                           ^                       |
                           |_______________________|
  </code></pre>
  
  <p>View project references Controller and mode projects.
  Controller project references model project.</p>
  
  <p>and between Model and View there is another project, let's say project MV, with contains an interface and some converters (I will explain below).Model project references MV. Also view references MV as well. But MV is independent, it does not reference any project, neither Model nor view.</p>
  
  <p>In the model I Have my POCO objects (Entities), those define the physical database design.
  As I want to isolate model from view, that is, I do not want model depends on view, the models has its own entities and the view has its own as well. </p>
  
  <p>for example in the View namespace I have below object:</p>
  
  <pre><code>          public class EntityA : InterfaceMV
            {
               public string Property1 { get; set; }
               public int Property2 { get; set; }
               EntityB entityB;   &lt;--- refrences an entity in the same namespace              
            }
  </code></pre>
  
  <p>and in the Model namespace I have below object, that in fact it contains the same attributes:</p>
  
  <pre><code>         public class EntityA : InterfaceMV
           {
               public string Property1 { get; set; }
               public int Property2 { get; set; }
               EntityB entityB;  &lt;-- foreign key to an entity in the same namespace
           }
  </code></pre>
  
  <p>I do not have entities that are shared in Model and View so my problem is when from view I access directly to the model to retrieve some data using the context so from view I do:</p>
  
  <pre><code>          using (DBContext context = new DBContext())
            {
                Namespace.Model.EntityA a = context.EntitiesA.Find(id);
            }
  </code></pre>
  
  <p>as model and view uses different entities (despite they have the same attributes and the same name they are treated as different objects as they are beloging to different namespaces), I have to convert data from model object to view object. This conversion is done using a converter (interface) that I have in the interface (InterfaceMV) contained in project MV commented above. The InterfaceMV is as below:</p>
  
  <pre><code>public interface IDataTypeConverterEntityA
  {
      string Property1 {get; set;}
      int Property2 {get; set; }
  }
  </code></pre>
  
  <p>and InterfaceMV is implemented by EntityA in model and EntityA in View. So once I retrieve data from model from the view:</p>
  
  <pre><code>         Namespace.Model.EntityA a = context.EntitiesA.Find(id);
  </code></pre>
  
  <p>I have to convert the 'a' object (which is of type Namespace.Model.EntityA) into the equivalent in the View:</p>
  
  <pre><code>         EntityA aConverted = DataTypeConverterEntityA.Convert&lt;Namespace.Model.EntityA, EntityA&gt;(a);
  </code></pre>
  
  <p>the the converter is like below (it is inside the class DataTypeConverterEntityA):</p>
  
  <pre><code>    public static Y Convert&lt;T, Y&gt;(T itemToConvert) 
           where T : new(), IDataTypeConverterEntityA 
           where Y : new(), IDataTypeConverterEntityA
      {
          return new Y 
                  {
                      property1 = itemToConvert.property1,
                      property2 = itemToConvert.property2                        
                  };
       }
  </code></pre>
  
  <p>Basicaly, I have my POCO entities that represent tables in my DB, and I have some business classes  and I use the converter to move from one to the other.</p>
  
  <p>The problems are the following:
  1.- As project MV which contains the interfaceMV does not references neither the model nor the view, convert method can only convert scalar types but not for example other types such as EntityB property so once obtained converted object aConverted in the view:</p>
  
  <pre><code>// I retrieve the matching entity from the context
  EntityA aConverted = DataTypeConverter.Convert&lt;Namespace.Model.EntityA, EntityA&gt;(a);
  </code></pre>
  
  <p>I have to do antoher request to database to obtain EntityB, then convert it and finally assign from the view to EntityA.entityB by doing:</p>
  
  <pre><code>// I retrieve the matching entity from the context
  Namespace.Model.EntityA a = context.EntitiesA.Find(id);
  EntityA aConverted = DataTypeConverterEntityA.Convert&lt;Namespace.Model.EntityA, EntityA&gt;(a);
  
  // Inject values into the entity
  Namespace.Model.EntityB b = context.EntitiesB.Find(id);
  EntityB bConverted = DataTypeConverterEntityB.Convert&lt;Namespace.Model.EntityB, EntityB&gt;(b);
  
  aConverted.entityB = bConverted;
  </code></pre>
  
  <p>2.- As you can see I must have a converter for each entity because from the view I cannot treat the object received from the database request as the view uses its own entities..... and obviously I have to implement the interface in each entity, that in model's entity and that in View's entity.</p>
  
  <p>The cause of this is that I have isolated the model project completely, it is using its own entites and the view also is using its own.</p>
  
  <p>Maybe keeping entities (EntityA, EntityB, ....) in only one place and sharing them between Model and View would solve this issue.... avoiding using data types converters.</p>
  
  <p>so my question is: is there any better approach to avoid using data type converters each time? or do you think this architecture is good as it isolates Model from View? with this approach if you do changes it entities in View, the model it is not affected as it has its own entities. My problem is using data type converters each time....</p>
  
  <p>Any ideas?</p>