Models in different layers in a project

Cheng Chen

Recently I'm confused by "models". Let's say we have a table "Users" in the database including 4 columns, and in the data layer I have a corresponding "User" model.

namespace MyProject.Data
{
    public class User
    {
         public int Id { get; set; }
         public string UserName { get; set; }
         public string Password { get; set; }
         public int UserType { get; set; }
    }
}

While in the user registration page, usually I have a view model named "User"(or you can name it UserRegistration)

namespace MyProject.Web
{
    public class User
    {
        public string UserName { get; set; }
        public string PlainTextPassword { get; set; }
    }
}

Also we probably have a user profile page, which has a corresponding view model named "User" or "UserProfile" that has very similar properties(but not 100% same).

First question the above user models are similar, what's the correct way to design?

  1. separate them in different independent classes

    this is what I'm currently doing, but some properties are common, I need to set values manually(or using something like AutoMapper)

  2. sharing/inheritance between them

    it seems that the "common properties" get shared, we have no "UserName" property in different "User" models. However, the view model in the registration page is very strange because it has properties named "Password" and "PlainTextPassword", and even "UserType", but actually only UserName and PlainTextPassword are set.

-

[HttpPost]
public ActionResult Register(User user)
{
    //only user.UserName and user.PlainTextPassword are useful
    //however you will have user.Password, user.UserType with default values
    //it even breaks the validation if you use ModelState.IsValid
}

It seems option 1 is better? Correct me if I'm wrong or you have another idea. Then here comes another question based on option 1.

Second question We have MyProject.Web.User as the view model and MyProject.Data.User as the data model. The registration page posts a MyProject.Web.User instance to my controller, finally I need to save it to database via MyProject.Data.User. Should it looks like this:

//the controller in the web project
[HttpPost]
public ActionResult Register(User user)
{
     UserManager.Register(user.UserName, user.PlainTextPassword);
     //blabla
}

//UserManager in the business layer
public void Register(string userName, string plainTextPassword)
{
     //validation
     //set properties to MyProject.Data.User
     //save the changes to db (probably via EF)
}

you can see the signature of UserManager.Register grows if more properties added, e.g. string address, string mobile. I think a "user" model is necessary here. MyProject.Web.User is located in the web so it can't be used in UserManager. While MyProject.Data.User is located in the data layer, if we use MyProject.Data.User here, then MyProject.Web project needs to add a reference MyProject.Data, but currently the reference relationship is UI->Core(the business)->Data. What's the correct design?

Dimitar Dimitrov

This is what worked for me in the past.

In your scenario, I'd create a User entity (in another project - probably called Entities or Models). Then in my UI layer I'd create bunch of view specific ViewModels (you can of course have a base ViewModel shared between some of the views), for example something like:

// This goes in MyProject.Web/ViewModels ...

public abstract class UserSecurityViewModel {
    // stuff that is shared between the other ViewModels
    public string Username { get; set; }
    public string Password { get; set; }
    // you get the idea ...
}

public class RegistrationViewModel : UserSecurityViewModel {
    // Registration specific properties
    public string Email { get; set; }
    // more ...
}

public class LoginViewModel : UserSecurityViewModel {
    // you don't need the email here ...
    public bool RememberMe { get; set; }
}

Then of course I'd use something like AutoMapper to map between the ViewModels and the Models (in your case User).

For example:

[HttpPost]
public ActionResult Register(RegistrationViewModel viewModel) {
   User user = viewModel.ToUser(); // <-- this is an AutoMapper helper

   // now pass that to your business logic layer and do magic
   _userManager.Register(user);
}

I hope I'm making sense (and that I didn't misunderstand you).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

gganimate: two layers with different geometries and timepoints

From Dev

Passing different models to RenderPartial()

From Dev

Concepts regarding project layers

From Dev

Different Cluster to Different Layers in the same Map

From Dev

Showing sets of markers on different layers of Google map

From Dev

Neural network - different input layers

From Dev

Models in different layers in a project

From Dev

Entity Framework Code first with models in a different project

From Dev

URL for the profile of different models

From Dev

ggVis : creating a plot with multiple layers on different dataset

From Dev

How to perform back propagation with different sized layers?

From Dev

Produce a composed image with different sized layers with transparency

From Dev

R: Interpolation between raster layers of different dates

From Dev

hiding method from certain layers in project

From Dev

making a shape fall down in different layers in actionscript

From Dev

Passing different models to RenderPartial()

From Dev

Showing sets of markers on different layers of Google map

From Dev

Surface view: text draws on different layers

From Dev

Entity Framework Code first with models in a different project

From Dev

MVC different models errors

From Dev

Transformations of different models not working

From Dev

Different notification partials for different models?

From Dev

WPF validation showing from different layers

From Dev

Reference existing CF project models

From Dev

how to flatten 2 different image layers?

From Dev

different Models with devise

From Dev

Will nodes from different layers interact

From Dev

How to represent an unknown number of layers in data with Django models

From Dev

How to add information on different layers of OpenLayers?

Related Related

  1. 1

    gganimate: two layers with different geometries and timepoints

  2. 2

    Passing different models to RenderPartial()

  3. 3

    Concepts regarding project layers

  4. 4

    Different Cluster to Different Layers in the same Map

  5. 5

    Showing sets of markers on different layers of Google map

  6. 6

    Neural network - different input layers

  7. 7

    Models in different layers in a project

  8. 8

    Entity Framework Code first with models in a different project

  9. 9

    URL for the profile of different models

  10. 10

    ggVis : creating a plot with multiple layers on different dataset

  11. 11

    How to perform back propagation with different sized layers?

  12. 12

    Produce a composed image with different sized layers with transparency

  13. 13

    R: Interpolation between raster layers of different dates

  14. 14

    hiding method from certain layers in project

  15. 15

    making a shape fall down in different layers in actionscript

  16. 16

    Passing different models to RenderPartial()

  17. 17

    Showing sets of markers on different layers of Google map

  18. 18

    Surface view: text draws on different layers

  19. 19

    Entity Framework Code first with models in a different project

  20. 20

    MVC different models errors

  21. 21

    Transformations of different models not working

  22. 22

    Different notification partials for different models?

  23. 23

    WPF validation showing from different layers

  24. 24

    Reference existing CF project models

  25. 25

    how to flatten 2 different image layers?

  26. 26

    different Models with devise

  27. 27

    Will nodes from different layers interact

  28. 28

    How to represent an unknown number of layers in data with Django models

  29. 29

    How to add information on different layers of OpenLayers?

HotTag

Archive