Model View Controller如何实现上下文 [英] Model View Controller how to implement context

查看:60
本文介绍了Model View Controller如何实现上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我想知道下一个样品使用EF5的MVC的最佳实现



如果我们有这样的观点

Dear All,

I want to know the best implementaion of MVC with EF5 for the next sample

if we have a view like this

@model RegistirationModel

@using(Html.BeginForm())
{
@Html.TextBoxFor(x=>x.UserName)
@Html.PasswordFor(x=>x.Passowrd)
@Html.PasswordFor(x=>x.ConfirmPassword)
@Html.DropdownListFor(x=>x.Country_ID,new SelectList(ViewBag.Countries,"ID","Name"))

}





示例模型



Sample For model

public class RegistirationModel
{
public string UserName {get;set;}
public string Password {get;set;}
public string ConfirmPassword {get;set;}
public string Country_ID {get;set;}
}





控制器样本



Sample of Controller

public partial class AccountController :Controller
{
[HttpGet]
[AllowAnonymouse]
public ActionResult Register()
{
  ViewBage.Countries=context.Contries.ToList<Country>();//direct from  context
}
[HttpPost] 
public ActionResult Register(RegistirationModel model)
{
if (ModelState.IsValid)
{
//do something
return RedirectToAction();
}
return view(model);
}
}



现在如果ModelState无效我应该重新分配ViewBage.Countries所以我应该再次从数据库带来它还是有任何方式将其重新分配给模型(最佳实现)



i知道我可以将其置于应用程序状态或事件会话状态,但这会降低网站性能。



如果我有一个国家的存储库应该放在什么内部

我需要建议我应该在域中创建存储库类项目或Web项目,如果我放入域项目,我应该在Web和域内有EF5参考



最好的问候

Ahmed Alsharu


now if the ModelState is not valid i should reassign the ViewBage.Countries so should i bring it from database again or there is any way to re assign it to model (best implementation )

i know that i can put it in application state or event session state but is that decrease the web site performance .

and if i have a repository for the countries what should i put inside
and i need advice should i make the repository classes in Domain Project or Web project and if i put in in the domain project i should have the EF5 reference inside both Web and Domain

Best Regards
Ahmed Alsharu

推荐答案

Hello Ahmed,

根据您的理解,您做得很好。我很感激,但我想在这里建议你。:)

首先是从不在控制器中使用Context操作方法。

尝试使用Context方法的服务编写,即数据库操作应该完成。

有一个接口和服务,将实现接口中的成员。

然后你可以注入依赖,即使用接口在控制器中访问DB / Query操作。为此,您需要了解依赖注入。

使用入门使用依赖注入 [ ^ ]

以上链接会给你一些想法。

然后控制器如下所示:

Hello Ahmed,
Based on your understanding, you have done a good job. I appreciate, but I would like to suggest you something here.:)
First is never use Context in a controller Action Method.
Try using Services where the Context method would be written i.e. the Database operations should be done.
Have a Interface and a Service that would implement the members in the Interface.
Then you can inject dependency i.e. use the Interfaces in the controllers to access the DB/Query operations. For this you need to understand the Dependency Injection.
Getting Started with Dependency Injection[^]
The above link would give you some idea.
Then the Controller would look like below:
public partial class AccountController :Controller
{
    private IDemoVariable _demoVariable;
    public AccountController(IDemoVariable demoVariable)
     {
        _demoVariable = demoVariable;
     }     
[HttpGet]
[AllowAnonymouse]
public ActionResult Register()
{
  ViewBage.Countries=_demoVariable.MethodReturningTheQueryContextResults;//
}
[HttpPost]
public ActionResult Register(RegistirationModel model)
{
if (ModelState.IsValid)
{
//do something
return RedirectToAction();
}
return view(model);
}
}



该服务将定义或实现上下文。

希望您对此有所了解。

谢谢

:)


The service would define or implement the Context .
Hope you get some idea on this.
Thanks
:)


这篇关于Model View Controller如何实现上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆