简体   繁体   中英

Where to store model classes in ASP.NET MVC solution?

I'm having some confusion deciding where to store my data classes (models) in my MVC projects. My solutions generally follow the following format:

  • SolutionName
    • ProjectName.Web (project)
      • Controllers
      • Models
      • Scripts
      • Views
    • ProjectName.Core (project)
      • Services
      • Interfaces
      • Models
    • ProjectName.Repositories (project) - contains repositories (databases, XML storage).
    • ProjectName.Test (project)- contains unit tests.

My question is- where should the data classes be stored? In the traditional MVC project, the models are obviously stored in the models folder in the web project. But as I try to keep my solution loosely coupled by separating concerns into multiple projects, should the data models be stored in a different project, like my Core project?

View model classes that are designed simply to provide data to MVC views should be in the ProjectName.Web project Models folder according to standard ASP .NET MVC conventions.

That is not a requirement though and you can easily keep them in a separate assembly.

If you are talking about domain models, ORM entities or any type of non-UI classes that contain data; those probably belong in a separate project.

Generally speaking, I would keep everything in your core library that would be necessary if you wanted to re-use the core library in another project. Using this approach, you would isolate your "domain" model ( http://en.wikipedia.org/wiki/Domain_model ) to just the core layer and only the web-specific functionality would be in your website.

Let's say on the website you want a user to be logged in, but on a winforms app, the authentication would be handled by the user logging into the pc.

You could put that in your website only, and handle authentication completely separately in different applications, without putting it in your domain model.

Then you would keep only domain-specific models in the core library.

You would then create data models that either wrap the core entities, or or their own representations of combined entities. Then you could have the view models that utilize the data in the models to control the output.

Edit: Here is an example setup (though lengthy, I kept it as brief as possible to illustrate the separation).

//--------------- In your domain library:

public class DataRepository : IDataRepository {

    public DataRepository() {
    } // end constructor

    public DataEntity GetData(DataRequest request) {
        //get data based on DataRequest
        return new DataEntity();
    } // end function GetData
} // end class DataRepository

public class DataRequest {

    public String RequestingUser {get; set;}

    public Dictionary<String, object> Parameters {get;}

} // end class DataRequest

public class DataEntity {

    public string Name {get; set;}
    public Guid Id {get; set;}
    public string SomeData {get; set;}

} // end class DataEntity

//--------------- In your web library:

public class UserRequest {

    public string UserName {get; set;}

} // end class UserRequest

public class LandingPageViewModel {

    public LandingPageViewModel() {
        Data = new DataItemViewModel();
    } // end constructor

    public void FillData(DataEntity entity) {
         Data.Name = entity.Name;
         Data.DataValue = entity.SomeValue;
         Data.ShowValue = !String.IsNullOrWhiteSpace(UserName);
    } // end method FillData

    public string UserName {get; set;}

    public List<string> Messages {get; set;}

    public DataItemViewModel Data {get; set;}

} // end class LandingPageViewModel

public class DataItemViewModel {

    public string Name {get; set;}
    public string DataValue {get; set;}

    public bool ShowValue {get; set;}

} // end class DataItemViewModel

public class MyController : Controller {

    private IDataRepository _repository;

    public MyController(IDataRepository repository) {
        _repository = repository;
    } // end constructor

    public ActionResult LandingPage(UserRequest user) {
        ActionResult result = null;
        DataRequest itemRequest = new DataRequest();
        itemRequest.RequestingUser = user.UserName;
        DataEntity myEntity = null;
        myEntity = _repository.GetData(itemRequest);
        if(myEntity != null) {
           LandingPageViewModel viewModel = new LandingPageViewModel();
           viewModel.UserName = user.UserName;
           viewModel.FillData(myEntity);
           result = View("LandingPage", viewModel);
        } else {
           result = View("Error");
        } // end if/else
        return result;
    } // end action LandingPage
} // end class MyController


// In a view

<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of LandingPageViewModel)" %>
<div>
    <%:Model.Name;%>
    <%
        if(Model.ShowValue) {
    %>
        <%:Model.DataValue;%>
    <%
        } // end if
    %>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM