Saturday, May 26, 2018

Part 54 - Repository Pattern - 1 - Adding Business Layer in Asp.net MVC

In this tutorial, You will learn about how to add Business layer under repository pattern implementation in Asp.net MVC . In Repository pattern I will create different layers but it depends on you whether you need this or not. You can directly create Data Access Layer and create the generic repository methods into this. I will explain generic repository in my upcoming videos. Lets look at below layers 

1. Web Layer is your MVC web Project
2. Business Layer consist the CRUD operation, gets data from Data Access Layer, Manipulate them and finally returns data to the Controller ( Web Layer)
3. Domain Layer consist the Domain Models or Classes that hold the data coming from Data Access Layer. Both Web and Business Layer can use domain models to exchange data.
4. Data Access Layer consist the generic repository methods (generic CRUD operation), Unit of Work( Database Context) and NON Generic repository( User defined repository).




#How to create Business Layer? 

 Please follow below Steps 

Step 1 : Right Click on your solution and click on Add, then click on New Project.





Step 2 : After opening the popup, select class Library option and give a meaningful name





Step 3 : Add an Interface Folder into your business layer then create IEmployeBusiness interface and finally add a method into this example: GetEmployeeName(). After adding interface, add a concrete class EmpolyeeBusiness which will implement the IEmployeeBusiness Methods. Please see below screenshot. Copy below code in your interface and class.



A. IEmployeeBusiness( Interface) 
using MVCTutorial.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MVCTutorial.Business.Interface
{
    public interface IEmployeeBusiness
    {
        string GetEmployeeName(int EmpId);
       
    }
}

B. Employeebusiness ( Concrete class)

using MVCTutorial.Business.Interface;
using MVCTutorial.Domain;
using MVCTutorial.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MVCTutorial.Business
{
    public class EmployeeBusiness : IEmployeeBusiness
    {
        public string GetEmployeeName(int EmpId)
        {
            return "Ashish" + EmpId;
        }

       
    }
}



Step 4 : Go to your Web Layer and add the reference of Business layer (check the MVCTutorial.Businesss  checkbox ). You don't need to care about Domain Layer right now.

  
Step 4 : Create an instance of EmployeeBusiness Class in your controller, then access GetEmployeeName() method and you are done. Copy the controller code below.

#Controller Code(RepoController.cs) 

using MVCTutorial.Business;
using MVCTutorial.Business.Interface;
using MVCTutorial.Domain;
using MVCTutorial.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCTutorial.Controllers
{
    public class RepoController : Controller
    {
       
        // GET: Repo
        public ActionResult Index()
        {
            IEmployeeBusiness _empBusiness = new EmployeeBusiness();
            ViewBag.EmpName = _empBusiness.GetEmployeeName(254);

           
            return View();
        }
    }
}

Note: Don't forget to resolve your in case you get item missing error. Right click on error then resolve it.  What Next => (How to add Domain Layer )

Please Like, Share and subscribe our Channel. Have a great day.


All Code Factory

1 comment:

sameh said...

how can to Install Master & Details IN This Project