3

我有两个与在 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;
            }
        }
    }
}

它工作得很好,但我的问题是:我是否使用了最好的方法?

4

3 回答 3

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);
}
于 2012-08-24T14:41:17.200 回答
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);
}
于 2012-08-24T14:41:56.117 回答
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);
    }
}

和基本控制器:

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;
    }

}

于 2012-08-24T16:12:49.323 回答