Abhilash J A

Abhilash J A

  • 535
  • 2.4k
  • 596.3k

C# - passing objects between business and data access layer

Jan 9 2017 12:31 AM
Hello everyone,
 
I building a MVC 4 project with Entity Framework 6. There is have business layer and data access layer also both are have its own model classes.
 
If I want to get list of table values from sql table -> DAL -> BAL -> Controller, which is the fastest and best way retrieve list from each other without using foreach and automapper.dll ?
Eg:
 
 DAL--> 
  1. public class ProductsDAL
    {

  2. public List<ProductCategory> GetProductCategoryTree()  
  3.        {  
  4.            using (var context = new ReboxEntities())  
  5.            {  
  6.                return context.ProductCategories.ToList();  
  7.            }  
  8.        }  
  9. }
 BAL--> 
  1. public class ProductCategoryBL  
  2.   {  
  3.       public List<ProductCategoryDM> GetProductCategories()  
  4.       {  
  5.           ProductsDAL objProduct = new ProductsDAL();  
  6.           IList<ProductCategory> lsRes = new List<ProductCategory>();  
  7.           lsRes = objProduct.GetProductCategoryTree();  
  8.   
  9.           List<ProductCategoryDM> categories = new List<ProductCategoryDM>();  
  10.           foreach (var item in lsRes)  
  11.           {  
  12.               categories.Add(new ProductCategoryDM{                  
  13.                   idProductCategory = item.idProductCategory,  
  14.                   ProductCategoryName = item.ProductCategoryName,  
  15.                   ProductCategoryDescription = item.ProductCategoryDescription,  
  16.                   idParentCategoryProduct = item.idParentCategoryProduct,  
  17.                   AlternateNamesToSearch = item.AlternateNamesToSearch,  
  18.                   LevelID = item.LevelID,  
  19.                   CompletePathName = item.CompletePathName,  
  20.                   PCImage = item.PCImage  
  21.               });  
  22.           }            
  23.           return categories;  
  24.       }  
 then Controller,
 
  1. /*Inside Viewmodel folder*/   
  2.  public class SearchVM  
  3.     {  
  4.         public List<ProductCategoryDM> Res = new List<ProductCategoryDM>();  
  5.         public SearchVM()  
  6.         {  
  7.             Res = new List<ProductCategoryDM>();  
  8.         }  
  9.     }  
  10. /*End*/  
  11.   
  12.  [HttpPost]  
  13.         public ActionResult Search()  
  14.         {  
  15.             ProductCategoryBL objProduct = new ProductCategoryBL();  
  16.             List<ProductCategoryDM> lsRes = new List<ProductCategoryDM>();  
  17.             SearchVM vm = new SearchVM();  
  18.             lsRes = objProduct.GetProductCategories();  
  19.             vm.Res = lsRes;  
  20.             return PartialView("_searchresult", vm);  
  21.         }  
 Here is using too many foreach loop for mapping. How can I improve this? Please help me.

Answers (3)