Reputation: 301
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...). The BL would be inside the controllers. 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:
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
}
}
Upvotes: 0
Views: 1130
Reputation: 3213
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)
.
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.
Upvotes: 4