This feels a bit repetitious and I'm wondering what everyone else's thoughts and opinions are on this. This seems like a voilation of the DRY principal.
Im working on a distributed web application using MVC3, WCF, Entity Framework.
I have Entities for the DAL, DataContracts for the WCF Services and ViewModels for the Presentation Layer all of which are holding basically the same data.
Is this really the best way to be doing this? Could anyone explain why or point me to a good article?
I have Entities for the DAL, DataContracts for the WCF Services and ViewModels for the Presentation Layer all of which are holding basically the same data.
In such scenario, I would use following project structure
Data Access Layer (DAL) (EF entity would be created)
Service Layer (add reference to DAL) : I will not define data contract unless it is specific required. And from service I would return EF entity instance or list for e.g.
public List<DAL.EF.Person> GetPersonList()
{
}
Web UI (MVC) Layer( add service reference to service layer
but do not add reference of DAL). This will create proxy classes of
DAL.EF.Person as well in MVC app. Now I would use this proxy (Person) class as a ViewModel in MVC.
Again this fits bests only if data entity, data contract and viewmodel types require identical.
Yes it seems like repeatitive and boring work. For instance you consider a customer object then you need to write four duplicate class for this for each layer. Generally people uses object mapper for this purpose. You can take the help of any existing a
new object mapper for instance you can check the following-
You can create a new one for yourself. One approach I can suggest is - create an attribute which has one properly called name. And add that attribute to all the properties of all four customer classes. For instance you have CustomerID properly. In each
class you may have given a different name for this proprly but you can add same attributename across all the classes. And create a mapper class that use reflection and read the attribute value and assign the properties of two objects.
Thanks & Regards
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
ntek designs
Member
23 Points
41 Posts
Entities, Data Contracts & ViewModels - Repetitious....
Jul 17, 2012 09:15 PM|LINK
This feels a bit repetitious and I'm wondering what everyone else's thoughts and opinions are on this. This seems like a voilation of the DRY principal.
Im working on a distributed web application using MVC3, WCF, Entity Framework.
I have Entities for the DAL, DataContracts for the WCF Services and ViewModels for the Presentation Layer all of which are holding basically the same data.
Is this really the best way to be doing this? Could anyone explain why or point me to a good article?
Nandip Makwa...
Participant
1267 Points
293 Posts
Re: Entities, Data Contracts & ViewModels - Repetitious....
Nov 23, 2012 10:01 AM|LINK
In such scenario, I would use following project structure
public List<DAL.EF.Person> GetPersonList() { }Again this fits bests only if data entity, data contract and viewmodel types require identical.
Software Engineer by Profession, Learner by Passion!
asteranup
All-Star
30184 Points
4906 Posts
Re: Entities, Data Contracts & ViewModels - Repetitious....
Nov 30, 2012 03:53 AM|LINK
Hi,
Yes it seems like repeatitive and boring work. For instance you consider a customer object then you need to write four duplicate class for this for each layer. Generally people uses object mapper for this purpose. You can take the help of any existing a new object mapper for instance you can check the following-
http://automapper.org/
http://antix.co.uk/Blog/Object-Object-Mapping
You can find many others online.
You can create a new one for yourself. One approach I can suggest is - create an attribute which has one properly called name. And add that attribute to all the properties of all four customer classes. For instance you have CustomerID properly. In each class you may have given a different name for this proprly but you can add same attributename across all the classes. And create a mapper class that use reflection and read the attribute value and assign the properties of two objects.
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog