开发者

Multiple granular view-models or a single model - Asp.NET MVC3

I'm looking at creating my first ASP.NET MVC application using MVC3.

The project template I used included some models for registering users, logging in and updating a forgotten password.

I want users to be authenticated against my own data store (probably using Entity Framework) and using google OAuth.

I assumed that I'd want a User model class that co开发者_开发百科ntained a few standard properties and some business logic which handled the "local" authentication and the OAuth call but the project template has confused me.

Should I be creating multiple view-models for different actions like Login, Register, etc and then using the controller to instantiate and invoke my model to perform the business logic or should I use my User model for all the different actions?

Thanks Ben


Should I be creating multiple view-models for different actions like Login, Register, etc and then using the controller to instantiate and invoke my model to perform the business logic or should I use my User model for all the different actions?

View model per view. That's the rule. There might even be 2 view models per view (one for rendering data in the GET and one for receiving data from the view in the POST action). Don't be shy in creating view models. You definitely shouldn't be using a User model for all different actions, that would be catastrophic. The model should be used by your service layer. A User model will be manipulated by this layer, and never passed to a view.

You may also checkout AutoMapper for mapping between your model classes and view models. It's a great tool and comes in handy especially when the number of view models start to increase.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜