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.
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.
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.
You can't. You need to represent different contexts as different view models because validation on those models changes depending on the screen representation.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With