开发者

ASP.Net MVC How to separate view models from DB models?

I can't quite decide how to go about separating my view models from my DB models.

I'm using an ActiveRecord pattern for my DB access. Meaning I get a User class instance for each User row in the database.

In WebForms I'm used to use these as my model objects, implementing most of the business l开发者_运维技巧ogic directly on my ActiveRecords.

I realize this isn't exactly 3-tiered design, and I'd really like to improve upon it, especially in MVC, where separation of concerns is empathized.

So I'd think the Controller shouldn't have access to my DB models, but how do I then go about storing/loading data from the DB ?

It's not my impression you should place a huge amount of business logic in your view models either, so somehow I think I'm missing a central piece of the puzzle.

What I'm looking for is some best-practice advice I guess :-)

I hope all this made sense, otherwise please ask.


I strongly suggest creating one view model per view and using AutoMapper to map properties from your active records to your view models. I don't believe that there's a problem with your controller having access to your DB models; the controller should be responsible for translating them into view models.

As for translating view models (really post data models) back into active records, you can use AutoMapper for this as well in simple cases and custom code for the rest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜