ASP.NET MVC中的最佳实践模型

时间:2012-08-24 14:34:53

标签: c# asp.net-mvc model-view-controller

我有两个问题与在mvc项目中管理模型的最佳方法有关。

  • 我可以使用构造函数初始化我的模型(显然是与其相关的逻辑)?
  • 为此目的,最好使用“构造函数方式”,还是应该在创建新模型实例后使用控制器调用的扩展方法?

例如,我有一个联系表格的模型。用户可以有三个角色:匿名,客户或供应商,他可以在每个州提交表格。我唯一想要的是,如果用户登录(如客户角色或供应商的角色),我想在文本框中预加载他的数据。 为此,我写了这段代码:

using System;
using System.Web;
using System.Web.Security;
using DrOkR2.Bll.Managers;

namespace DrOkR2.WebFront.Models
{

    public class RequestModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Prov { get; set; }
        public string Request { get; set; }
        public bool UsageConditions { get; set; }

        public RequestModel()
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated) return;
            if (HttpContext.Current.User.IsInRole("Client"))
            {
                var guid = (Guid)Membership.GetUser().ProviderUserKey;
                var manager = new ClientManager();
                var client = manager.GetClient(guid);
                client.Email = Membership.GetUser().Email;

                FirstName = client.FirstName;
                LastName = client.LastName;
                Email = client.Email;
                Phone = client.Phone;
                Prov = client.Prov;
            }
            else if (HttpContext.Current.User.IsInRole("Supplier"))
            {
                var guid = (Guid)Membership.GetUser().ProviderUserKey;
                var manager = new SupplierManager();
                var supplier = manager.GetSupplier(guid);
                supplier.Email = Membership.GetUser().Email;

                FirstName = supplier.FirstName;
                LastName = supplier.LastName;
                Email = supplier.Email;
                Phone = supplier.PrimaryPhone;
                Prov = supplier.BusinessProv;
            }
        }
    }
}

它完美无缺,但我的问题是:我使用最好的方式吗?

3 个答案:

答案 0 :(得分:5)

不,您不应该依赖HttpContext或模型中的任何数据访问。你正在通过模型绑定(更不用说紧密耦合)来解决各种问题。

在Controller中设置模型属性,或在Controller使用的存储库中设置。

public ActionResult Contact(string id)
{
   var client = _repository.GetClient(id);
   var model = new RequestModel(){ /* set your properties from client */ };

   return View(model);
}

答案 1 :(得分:1)

您的视图模型中有太多的业务逻辑供我喜欢。您还将视图模型与HttpContext耦合,这不是一个好主意。

这种初始化属于控制器,例如

public class RequestModel
{
    public string FirstName { get; set; }
    ....
}

public ActionResult ContactForm(...)
{
    var contactModel = new RequestModel();  
    if (User.IsInRole("Client"))
    {
        ...
    } 
    else if (User.IsInRole("Supplier"))
    {
        ...
    }

    return View(contactModel);
}

答案 2 :(得分:0)

我意识到了这一点。为了完成我把它放在这里的线程。有什么建议吗? 型号:

public class RequestModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string Prov { get; set; }
    public string Request { get; set; }
    public bool UsageConditions { get; set; }

    public RequestModel(DrOkUser user )
    {

        if (user == null) return;
        var type = user.GetType().Name;         //Client o Supplier
        Email = user.Email;
        if (type == "Client")
        {
            FirstName = ((Client)user).FirstName;
            LastName = ((Client)user).LastName;
            Phone = ((Client)user).Phone;
            Prov = ((Client)user).Prov;
        }
        if (type == "Supplier")
        {
            FirstName = ((Supplier)user).FirstName;
            LastName = ((Supplier)user).LastName;
            Phone = ((Supplier)user).PrimaryPhone;
            Prov = ((Supplier)user).BusinessProv;
        }
    }
}

控制器方法:

public class HomeController : BaseController
{
 public ActionResult Assistance()
    {
        var user = GetCurrentUser();
        var mdl = new RequestModel(user);
        return View(mdl);
    }
}

和BaseController:

public class BaseController : Controller
{
 public DrOkUser GetCurrentUser()
    {
        if (!HttpContext.User.Identity.IsAuthenticated) return null;
        DrOkUser user = null;
        var guid = (Guid)Membership.GetUser().ProviderUserKey;
        if (HttpContext.User.IsInRole("Client"))
             user = ClientManager.GetClient(guid);
        if (HttpContext.User.IsInRole("Supplier"))
            user = SupplierManager.GetSupplier(guid);

        user.Email = Membership.GetUser().Email;            
        return user;
    }

}