用于集合的ASP.NET MVC ViewModel最佳实践

本文关键字:ViewModel 最佳 MVC NET 集合 ASP 用于 | 更新日期: 2023-09-27 17:58:40

我有EF类,我为每个类创建了具有各种DAL功能(获取、设置、排序等)的模型(没有从EF poco类继承)。BL将在控制器内部。对于模型的单个实例,一切看起来都很好,但现在我需要绑定以查看数据列表。下面是我如何做到的一个例子。我是MVC的新手,不确定这是否是最好的做法:

型号:

public class CustomerWishlistModel
{
    static storeDataEntities db = new storeDataEntities();
    //Some properties of the model
    public int CustomerID { get; set; }
    public int ProductID { get; set; }
    public string ProductName { get; set; }
    public string BrandName { get; set; }
    public CustomerWishlistModel(){}
    //Get wishlists by customer
    public static List<CustomerWishlist> GetCustomerWishLists(int customerID)
    {
         return db.CustomerWishlists.Where(x => x.CustomerID == customerID).ToList();
    }
    //Convert wishlist to model
    public static CustomerWishlistModel GetWishListModel(CustomerWishlist thisWishList)
    {  
        CustomerWishlistModel thisWishListModel = new CustomerWishlistModel()
        {
            CustomerID = thisWishList.CustomerID,
            ProductID = thisWishList.ProductID,
            BrandName = thisWishList.Product.Supplier.BrandName,
            ProductName = thisWishList.Product.Name
       };
       return thisWishListModel;
    }
}

控制器:

[Authorize]
[HttpGet]
public ActionResult Index(string id)
{
    //Get all wishlists to current customer
    List<CustomerWishlist> thisWishList = CustomerWishlistModel.GetCustomerWishLists(int.Parse(id));
    //Get language from url
    Language thisLanguage = LanguageModel.GetLanguageByCulture(RouteData.Values["language"].ToString());
    if (Session["UserID"] != null && Session["UserID"].ToString() == id && thisWishList != null && thisLanguage != null)
    {
        List<CustomerWishlistModel> thisWishlistModel = new List<CustomerWishlistModel>();
        //Get all wishlists that their status is online and language equals to current
        foreach (CustomerWishlist item in thisWishList)
        {
            if (item.Product.Status == (int)EnumModel.ProductStatus.Online && item.Product.LanguageID == thisLanguage.ID)
            {
                thisWishlistModel.Add(CustomerWishlistModel.GetWishListModel(item));
            }
        }
        return View(thisWishlistModel);
    }
    else
    {
        return RedirectToAction("Login", "Customer");
    }
}

视图:

@model IEnumerable<Store.Models.CustomerWishlistModel>
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = Resources.Store.WishList;    
}
@using (Html.BeginForm())
{
    <h2>@Resources.Store.WishList</h2>
    foreach (Store.Models.CustomerWishlistModel item in Model.ToList())
    {
         item.BrandName...... and other markup tags
    }
}

用于集合的ASP.NET MVC ViewModel最佳实践

最佳实践是从控制器中删除任何业务逻辑,假设它们只处理调用适当的服务和将模型数据传递到视图。您的模型不应该有任何数据访问代码,相反,最好将收集的数据抽象到另一个类中,并返回一个干净的模型。例如,最流行的DAL抽象模式之一是通过Customer CustomerRepository.GetCustomer(int id)等简单接口返回数据的存储库模式。

您的所有业务逻辑也应该包含在服务中,这些服务将调用存储库来获取所需的数据,并根据您的业务规则处理数据。该服务将返回一个干净的模型对象,您的控制器将把它传递给视图。简单干净。