some design issue in ASP MVC project (Software Engineering question) [closed]
Want to improve this question? 开发者_如何学GoUpdate the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this questionI have an MVC web app that has a few models, my database calls wrapped in a class library.
I need to get a collection of items from the database (one of my model class), but clearly the model is unknown to the database class lib. I can define the item class in the database class lib instead of the web app but it doesn't look right.
is there a known solution to this situation?
I was thinking of two options:
- define another class library that holds all the models, and add references to the app and database lib.
- return dataset or datatable from the database class library and convert it to the items collection in my web app.
please advise
Thanks
Here's how you could structure your application:
- Models (contains POCO objects that represent your business entities)
- Repositories (contains CRUD operations with the models, this is the data access layer using a specific data access technology)
- Services (contains business operations with the models which are achieved through the repositories)
- MVC application which contains view models, controllers and views
So the MVC application uses the service layer to fetch a model from the database, maps this model to a view model and passes the view model to the view. The mapping between the models and the view models could be simplified with AutoMapper.
Typically the common approach is to have your domain entities live somewhere other than your web project. You can then map from these entities to your view specific models. Using your own mapping framework or something like Automapper or ValueInjecter.
精彩评论