N-Tier Architecture with Entity Framework

Hi,

Today i will show you that how to create N-Tier Architecture with Entity Framework

Hope you know about the N-Tier Architecture so I am starting with creating the Project.

First You need to understand the Naming Convention

1) UI – Presentation Layer

2) BAL – Business Access Layer

3) DAL – Data Access Layer

—————————————————————————————————————————————————————

Now I am starting for creating the Project.

I am selecting “ASP.NET Empty Web Application” and name it as “ArchDesign.UI“. It contains all the aspx, JS, CSS, Images etc.

Now creating another Project, I am selecting “Class Library” and name it as “ArchDesign.BAL“. It contains all the Entity File and major two files BaseEntity and ObjectContextFactory file. I will discuss later about this. So You need to create Entity for every table.

Now creating another Project, I am selecting “Class Library” and name it as “ArchDesign.DAL“. It contains the Entity Framework(edmx).

—————————————————————————————————————————————————————

Now next step is to create Entity Framework. Right Click on the “ArchDesign.DAL” and click on Add new Item then select “ADO.NET entity Data model” name it as “ArchDesign.edmx” > Now select “Generate from Database” and click on Next > Choose your database by clicking on the New Connection button and check the Checkbox which says “Save entity connection settings in App.config as” and name that “ArchDesignEntities” and click on Next > Check all the tables, Views and SP and Functions and Type Proper Model Namespace: Here i am using “ArchDesignModel” and Click on Finish. That’s it completed with Entity Framework. Now Save and Build the DAL Project only.

—————————————————————————————————————————————————————

Now move Back to the BAL Project

Add Reference of the DAL Project and EntityFramework

Add following references into the project. System.Web, System.Web.Entity, System.Web.Entity.Design

Create one “Class file” into it and name it “BaseEntity.cs” and add namespace of DAL.

Add another “Class file” into it and name it “ObjectContextFactory.cs

You will get both of the file on the following URL. Just copy and Paste the required things into it. (Copy all the things and change namespace)

https://drive.google.com/folderview?id=0B0Dm4JdbKHbjMjRRa2NJejNRU0U&usp=sharing

Now open the Base Entity file and you will find the line written as “publicArchDesignEntities db = null;

Change “public” to “Internal” and “ArchDesignEntities” to the name of connectionString written in App.config of DAL Project.

Now you need to create entity of the Tables. Use specific naming convention

I have 2 tables named “Employee”and “Desgination” so my Entity name will be as follows “EmployeeEntity” and “DesignationEntity”. Both the entity will be class file. Access modifier is public and implements the Base Entity. Also add DAL namespace in both the files.

Now implement the Insert, Update, Delete and select operation in the BAL Entity

Reference Code for Designation:

publicclassDesignationEntity : BaseEntity

{

    publicDesignation CurrentRecord { get; set; }

    publicList<Designation> CurrentList { get; set; }

 

    public DesignationEntity()

    {

    }

 

    public DesignationEntity(Designation designation)

    {

        CurrentRecord = designation;

    }

 

    public DesignationEntity(int dID)

    {

        CurrentRecord = GetByID(dID);

    }

 

    privateDesignation GetByID(int id)

    {

        return db.Designations.Where(x => x.ID == id).FirstOrDefault();

    }

 

    publicvoid Create()

    {

        CurrentRecord = db.Designations.Create();

    }

 

    publicvoid Save()

    {

        if (CurrentRecord.ID == 0)

        {

            db.Designations.Add(CurrentRecord);

        }

        else

        {

 

        }

        db.SaveChanges();

    }

 

    publicvoid Delete()

    {

        if (CurrentRecord != null)

        {

            db.Designations.Remove(CurrentRecord);

            db.SaveChanges();

        }

    }

 

    publicList<Designation> getallDesignation()

    {

        return db.Designations.ToList();

    }

}

Implement same for the Employee.

—————————————————————————————————————————————————————

Now Move to the UI section and create one webform that list all the designation in grid.

Give BAL and DAL reference and Add namespace of BAL and DAL

Now Add Connection String into the Web.Config of the UI Project. Just Copy Paste the connectionStrings tag from the App.config of the DAL Project.

Now in the aspx.cs file of the DesignationList just add the following code in Page_Load method  to get the list of all the designaton.

//for listing

DesignationEntity designationEntity = newDesignationEntity();

List<Designation> lstDestignation = newList<Designation>();

 

lstDestignation = designationEntity.getallDesignation();

 

gvDesignation.DataSource = lstDestignation;

gvDesignation.DataBind();

 —————————————————————————————————————————————————————

Now if you want to Add/Update the Record, you just need to code as listed below.

//for adding or Updating

protectedvoid btnSave_Click(object sender, EventArgs e)

{

    int dID = 0;

    DesignationEntity designation = newDesignationEntity(dID);

 

    if (designation != null)

    {

        designation.Create();

    }

 

    //If for adding and else for Updating

    if (dID == 0)

    {

        designation.CurrentRecord.Name = txtDesignation.Text;

    }

 

    designation.Save();

 

    if (dID == 0)

    {

        //Added Successfully.

        dID = designation.CurrentRecord.ID;

    }

    else

    {

        //Updated Successfully.

    }

}

That’s it its al completed and enjoy the Entity framework with N-Tier Architecture

For Common Functions you can add the New Project as “Class Library”. Have a great day….

Contact me if you have any Query.

Leave a comment