Model replication using Web API and MVC together

So, I'm working on a project and I have a quick simple "architecture" plan described here: http://i.stack.imgur.com/IA9QI.png

I will have a web api project that will contain all my business and data access logic, and then I will have various other elements that will call the web api service that will return JSON. The other elements will be standalone MVC app, mobile app and device in the box.

Obviously, my business models will be the same for all elements, and I want consistency when using these models. For example, let's say I have a model called "User" which is a user account and it has fields FirstName, LastName, Username, etc., and for FirstName and LastName I have certain restrictions that I want to apply for example datatype, max length, etc. The model class might look like this:

public class User
{

    public int UserId { get; set; }

    [Display(Name = "UserName")]
    [MinLength(3, ErrorMessage = "Your {0} must be at least {1} characters long")]
    [MaxLength(100, ErrorMessage = "Your {0} must be no more than {1} characters")]
    public string Username { get; set; }


    public string Password { get; set; }

    [Display(Name = "First Name")]
    [MinLength(3, ErrorMessage = "Your {0} must be at least {1} characters long")]
    [MaxLength(100, ErrorMessage = "Your {0} must be no more than {1} characters")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    [MinLength(3, ErrorMessage = "Your {0} must be at least {1} characters long")]
    [MaxLength(100, ErrorMessage = "Your {0} must be no more than {1} characters")]
    public string LastName { get; set; }
}

      

  • Can I duplicate these models in my web api project as well as my MVC and mobile app? Or is there a better way to pass these properties from the web api project so that there is no inconsistency (i.e.: the FirstName field in the MVC web app is 100 max long and the FirstName field in the mobile app has a max length of 80)

2. Is it better to keep the web API project and the MVC web application project in the same solution and just have a reference to the MVC web application in the web API project so that they use the same models and attributes? but then we create a dependency between them and you still have to worry about the device being in the field as well as the mobile app.
Or, is it better to extract the data models into a separate class library project and put all the model definitions with their attributes?
Or is there another better solution?

Any insight is helpful. Thanks in advance!

+3


source to share


1 answer


Do it

1) Keep the user of the model the same as when validating

2) for web api

a) create a separate project for DTO (data transformation object)



b) mesh data between model objects at the business / application service level and returns the data to the api controller in the form of a data transformation object (which will be just properties and can be annotated for validation)

3) With mvc

a) Create a separate project for the ViewModel

b) mesh data between model objects at the business / application service level and returns the data to the mvc controller as a viewmodel (which will be just properties and can be annotated to validate the data). return the viewmodel object to the mvc view.

0


source







All Articles