Search code examples
c#wpfentity-framework-43-tier

How to implement 3 tier approach using Entity Framework?


I know this question is asked many times, but I couldnt get a clear picture of what I need.

I have a WPF application which I need to redo using 3- Tier approach.

I have used Entity Framework for creating datamodel and using Linq queries for querying the data.

objCustomer = dbContext.Customers.Where(c => c.CustCode == oLoadDtl.CustNo).First();

I use Linq queries where ever I need in the program to get records from the database.

So, I just would like to know which all stuff comes under DAL, Business logic and UI layers.

Also, how do I separate them?

Can the entity datamodel considered as a DAL?

Is it a better idea to put the entity model in a separate class library?


Solution

  • It's better to create special class called DataAccess to encapsulate EntityFramework-invokes. For business logic you can create model classes, they will use DAL if needed. Other details depend on what your application should do. For example:

    //DAL
    public class DataAccess
    {
        public static void GetCustomerByNumber(int number)
        {
            var objCustomer = dbContext.Customers.Where(c => c.CustCode == number).First();
            return objCustomer;
        }
    }
    
    //Models
    public class Customer
    {
        public string Name { get; set; }
        public int Number { get; set; }
    
        public Customer GetCustomerByNumber(int number) 
        {
           return DataAccess.GetCustomerByNumber(number);
        }
    
        public void ChangeProfile(ProfileInfo profile)
        {
           //...
        }
    }
    

    Main things are extensibility, re-usability and efficiency of your solutions.