Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Entities vs. Service Models vs. View Models (MVC)

Tags:

I'm trying to understand and figure good practices for designing your app/domain models (POCOs/DTOs).

Let's say I have the following database table, Account:

UserID int Email varchar(50) PasswordHash varchar(250) PasswordSalt varchar(250) 

Of course, EF4 would build the entity like so:

public class Account {     public int UserID { get; set; }     public string Email { get; set; }     public string PasswordHash { get; set; }     public string PasswordSalt { get; set; } } 

Now, let's say I have a view model for registering a new user, which may look something like so:

public class RegistrationViewModel {     public string Email { get; set; }     public string Password { get; set; } } 

Lastly, I have a service which needs to register the user:

public class RegistrationService {     public void RegisterUser(??? registration)     {         // Do stuff to register user     } } 

I'm trying to figure out what to pass into the RegisterUser method. The view model is, of course, located under my web app (presentation layer), so I do not want this getting passed to my service.

So, I'm thinking one of four possibilities:

1) Set up a service model that is similar, if not identical, to the RegistrationViewModel, and use this:

public class RegistrationServiceModel {     public string Email { get; set; }     public string Password { get; set; } }  public class RegistrationService {     public void RegisterUser(RegistrationServiceModel registration)     {         // Do stuff to register user     } } 

2) Set up an interface of the model, and inherit this in my view model, and set up my method to accept the interface:

public interface IRegistrationModel {     string Email;     string Password; }  public class RegistrationServiceModel : IRegistrationModel {     public string Email { get; set; }     public string Password { get; set; } }  public class RegistrationService {     public void RegisterUser(IRegistrationModel registration)     {         // Do stuff to register user     } } 

3) Pass in the Account entity, doing the RegistrationViewModel-to-Account mapping in my controller:

public class RegistrationService {     public void RegisterUser(Account account)     {         // Do stuff to register user     } } 

4) Move my view model out of the presentation into a domain/service layer, and pass that into the service method:

public class RegistrationService {     public void RegisterUser(RegistrationViewModel account)     {         // Do stuff to register user     } } 

None of these three scenarios seem ideal, as I see problems in each of them. So I'm wondering if there's another method I can't think of.

What are good practices for this?

Thanks in advance.

like image 562
Jerad Rose Avatar asked Feb 25 '11 15:02

Jerad Rose


People also ask

What is the difference between model and ViewModel in MVC?

A model is usually more closely related to how your data is stored (database, services, etc.) and the model will closely resemble those. The ViewModel on the other hand is closely related to how your data is presented to the user. It is usually a flatten version of your model, denormalized, etc.

What is difference between MVC and Entity Framework?

MVC is framework mainly concentrates on how you deliver a webpage from server to client. Entity framework is an object relational mapper which helps you to abstract different types of databases (MSSQL,MySQL etc) and helps querying objects instead of having sql strings in our project.

Is it OK to use entities for view models in ASP NET MVC?

You can't. You need to represent different contexts as different view models because validation on those models changes depending on the screen representation.

What is the difference between View and ViewModel?

VIEW: ( Platform Specific Code – USER INTERFACE ) What the user sees, The Formatted data. VIEWMODEL: ( Reusable Code – LOGIC ) Link between Model and View OR It Retrieves data from Model and exposes it to the View. This is the model specifically designed for the View.


1 Answers

You never pass a view model to the service. A service doesn't even know about the existence of a view model that you might have defined in your presentation tier. A service works with domain models.
Use Auto mapper to map between view model and domain model and vice versa.

Personally, I've never heard of service models in DDD (view models for services).

like image 195
šljaker Avatar answered Sep 26 '22 02:09

šljaker