简体   繁体   中英

Asp.net MVC Architecture

I'm coming to the end of my first MVC project, and I'm not overly happy with how I constructed my Model objects and I'm looking for some ideas on how to improve them.

I use repositories for each DB table with Get, Save, Delete etc methods. The repositories use Linq2Sql for the DB access.

I do mapping from the Linq2Sql objects to MVC Model objects, in the main, these are very much 1 to 1 mappings.

My problem is, I don't think my MVC model objects were granular enough, and I am probably passing more data back and forth than needed.

For example, I have a User table. An admin can edit a users details as can the user themselves, so I reckon I should really have a "AdminUserModel" and "UserModel" objects, where "AdminUserModel" has a greater set of values (IsEnabled for example).

So my bigger question is really, what kind of architectures are people using out there in the wild, in order to map many similar, related Model objects down through the layers to the DB?

Any sample architecture solutions anyone can suggest beyond NerdDinner?

thanks in advance!

In the case of your user model, you should use inheritence in stead of 2 seperated models. In this way you can use the code that was created for user in the ones that inherite from it.

the type of model you use depends completely on what you want to do with it. A good thing might be to take a look at patterns and try to get the patterns working that are needed for your situation...

I usually take implement inheritance in my models. I usually have a base class of entity, which will have id, datecreated, valid and any other fields that are shared between entities (publishStatus, locked etc). If needs be you can create other base classes inheriting from entity: person entity, product entity etc.

this way you can have a generic repository base, constrained to Entity or IEntity , i find that most entities CRUD functions dont need much more behaviour than that provided by the generic base (perhaps you will need to add a few additional get methods for some types)

In your case, AdminUser could inherit from User

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