简体   繁体   English

ASP.NET MVC ViewModel集合最佳实践

[英]ASP.NET MVC ViewModel for collections best practice

I have EF classes, for each of them I made model (no inheritance from EF poco classes) with various DAL functionality (get, set, order and etc...). 我有EF类,我为每个模型创建了具有各种DAL功能(获取,设置,排序等)的模型(没有从EF poco类继承)。 The BL would be inside the controllers. BL将位于控制器内部。 Everything looks fine for single instance of model, but now I need to bind to view a list of data. 对于单个模型实例,一切看起来都很好,但是现在我需要绑定以查看数据列表。 Below is an example of how I did it. 以下是我如何操作的示例。 I'm new to MVC and not sure if it's the best practice to do it: 我是MVC的新手,不确定这样做是否是最佳做法:

The Model: 该模型:

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

The Controller: 控制器:

[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");
    }
}

The View: 风景:

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

The best practice is to remove any business logic from your controllers, they are only suppose to handle calling appropriate services and passing model data to the view. 最佳实践是从控制器中删除任何业务逻辑,它们仅假设处理调用适当的服务并将模型数据传递给视图的情况。 You models should not have any data access code and instead it's better to abstract gathering data to another class and return a clean model. 您的模型不应具有任何数据访问代码,而最好将收集的数据抽象到另一个类并返回干净的模型。 For example, one of the most popular patterns to abstract DAL is a repository pattern that returns data via simple interface like Customer CustomerRepository.GetCustomer(int id) . 例如,最抽象的DAL模式之一是存储库模式,它通过诸如Customer CustomerRepository.GetCustomer(int id)简单接口返回数据。

All your business logic should also be contained inside services which will call repositories to get required data and the process the data based on your business rules. 您的所有业务逻辑也应包含在服务中,这些服务将调用存储库以获取所需数据并根据您的业务规则处理数据。 The service will return a clean model object which your controller will pass to the view. 该服务将返回一个干净的模型对象,您的控制器将其传递到视图。 Simple and clean. 简单干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM