vhrobles / NET-Core--EF7-with-Angular

First code changes
0 stars 0 forks source link

Using Data Annotations on your entities? #3

Open apryiomka opened 8 years ago

apryiomka commented 8 years ago

You entities are now "bound" to the UI since they are using data annotation attributes which are specific to the UI (unobtrusive mvc) validation. Do you think there is a better way to use separation of concerns for you entities and the view models?

vhrobles commented 8 years ago

Hi Alex, the reason I used data annotation in my DB entities was to use the code first approach for Entity Framework. My view models which interact with the UI, there is a separate folder which is called ViewModels. Not sure if there would be a better approach to use the migrations without the DataNotations

apryiomka commented 8 years ago

I was rather asking if it is abetter approach to keep your entities clean. Think of POCO object

...Simply put, a POCO does not have any dependency on an external framework...

... Another way to put it is that POCOs are objects unencumbered with inheritance or attributes needed for specific frameworks and are persistence ignorant objects...

If your entities are POCO objects it is good idea to keep them "separate" from the UI as they are not necessarily have UI dependency. For instance, you use the same entities for both, the UI and the API, so why would you mark the object with the UI attributes (RequiredAttribute). The better approach would be to have SecurityViewModel representation of the Security entity and use libraries such AutoMapper or similar to map the objects. This is where separation of concerns comes in place.

vhrobles commented 8 years ago

I see, that's a good point. Will update the classes accordingly