none
3-tier C# EF project. RRS feed

  • Question

  • Hi All.

    I'm new in 3-tier architecture in C#. For instance, I created class that has some methods return values which I planning to use to populate some controls in UI Layer such as ComboBox, DataGrid. So, that class should be located in Data Layer or Business Layer? Can someone explain more detail about 3-tier architecture? I will appreciate for sample of project where using such architecture.

    Thanks

    Friday, May 29, 2020 10:56 PM

All replies

  • Hello,

    You want to look at MVVM (Model-View-ViewModel) pattern. Both Entity Framework 6, Entity Framework Core will work.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, May 30, 2020 1:32 AM
    Moderator
  • You're not using tiers. Tiers involve having presentation, business logic and data access logic on 3 different physical computers called tiers or physical boundaries.

    What you are talking about using is called layered architecture where all layers are on the same physical computer.

    https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

    In the Data Access Layer, you'll want to implement a pattern such as the DAO pattern. I prefer the DAO pattern over the UoW and Repository patterns since EF already implements the patterns, which is frowned upon impleading in code one or both patterns EF is already using. 

    https://javarevisited.blogspot.com/2013/01/data-access-object-dao-design-pattern-java-tutorial-example.html

    https://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm

    https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext?view=entity-framework-6.2.0

    I also use the DTO pattern sending the DTO through the layers leaving the EF entity at the DAL. The DTO(s) are kept in a classlib project I call mine Entities, and the layers that need to know about the DTO(s) have project reference to Entities. The presentation layer and its controls use the DTO.

    https://www.codeproject.com/articles/1050468/data-transfer-object-design-pattern-in-csharp

    Example code of DAL using the DAO, DTO patterns and EF in 3 layer Windows form desktop solution.

    using Entities;
    using System.Collections.Generic;
    
    namespace DAL
    {
        public interface IDaoPayroll
        {
            List<DtoPayRoll> GetAll();
            DtoPayRoll Find(int id);
            DtoPayRoll FindPayRollByAuthorId(int id);
            void Add(DtoPayRoll dto);
            void Update(DtoPayRoll dto);
            void Delete(int id);
        }
    }
    
    ===============================================
    using DAL.Models;
    using System;
    using Entities;
    using System.Collections.Generic;
    using Microsoft.EntityFrameworkCore;
    using System.Linq;
    
    namespace DAL
    {
        public class DaoPayroll :IDaoPayroll
        {
            private PublishingCompanyContext pc;
            private IDaoAuthor _daoauthor;
    
            public DaoPayroll(PublishingCompanyContext dbcontext, IDaoAuthor daoAuthor)
            {
                pc = dbcontext;
                _daoauthor = daoAuthor;
            }
    
            public List<DtoPayRoll> GetAll()
            {
                var dtos = new List<DtoPayRoll>();
    
                var payrolls = pc.Payroll.AsNoTracking().ToList();
    
                foreach (var payroll in payrolls)
                {
                    var dtoauthor =  _daoauthor.Find(payroll.AuthorId); 
    
                    var dto = new DtoPayRoll
                    {
                        PayrollId = payroll.PayrollId,
                        AuthorId = payroll.AuthorId,
                        Salary = payroll.Salary
                    };
    
                    dtos.Add(dto);
                }
    
                return dtos;
            }
    
            public DtoPayRoll Find(int id)
            {
                var dto = new DtoPayRoll();
    
                var payroll = pc.Payroll.AsNoTracking().SingleOrDefault(a => a.PayrollId == id);
    
                if (payroll != null)
                { 
                    dto.PayrollId = payroll.PayrollId;
                    dto.AuthorId = payroll.AuthorId;
                    dto.Salary = payroll.Salary;
                   
                }
                else
                {
                    throw new Exception($"Payroll with ID = {id} was not found.");
                }
    
                return dto;
    
            }
    
            public DtoPayRoll FindPayRollByAuthorId(int id)
            {
                var dto = new DtoPayRoll {Salary = 0};
                 
                var payroll = pc.Payroll.AsNoTracking().SingleOrDefault(a => a.AuthorId == id);
    
                if (payroll != null)
                {
                    dto.PayrollId = payroll.PayrollId;
                    dto.AuthorId = payroll.AuthorId;
                    dto.Salary = payroll.Salary;
                }
    
                return dto;
            }
    
            public void Add(DtoPayRoll dto)
            {
                var payroll = new Payroll
                {
                    AuthorId = dto.AuthorId,
                    Salary = dto.Salary
                };
    
                pc.Payroll.Add(payroll);
                pc.SaveChanges();
            }
    
            public void Update(DtoPayRoll dto)
            {
                var payroll = pc.Payroll.Single(a => a.PayrollId == dto.PayrollId);
    
                payroll.AuthorId = dto.AuthorId;
                payroll.Salary = dto.Salary;
                
                pc.SaveChanges();
            }
    
            public void Delete(int id)
            {
                var payroll =  pc.Payroll.AsNoTracking().SingleOrDefault(a => a.PayrollId == id);
    
                if (payroll != null)
                {
                    pc.Payroll.Remove(payroll);
                    pc.SaveChanges();
                }
            }
        }
    }
    

    namespace Entities
    {
        public class DtoPayRoll
        {
            public int PayrollId { get; set; }
            public int AuthorId { get; set; }
            public int? Salary { get; set; }
    
            public virtual DtoAuthor DtoAuthor { get; set; } = new DtoAuthor();
    
        }
    }
    

    Saturday, May 30, 2020 3:56 AM
  • Hi DA924x. Thanks for reply.

    Your post is very interesting. But my knowledge is very beginning level. I understood what if my solution has some projects such as: UI layer, Business Layer, DTO layer and Data layer in one computer to call them tier isn't correct. They are layers. Can you suggest. Now the structure of my solution look like:

    1. Data layer has folder with Model and Entity Framework tables.
    2. DTO layer has some classes of DataTables.
    3. Business layer has class with couple methods to save and search data.
    4. UI layer has couple WPF windows form.

    But also I has one more class with some methods which I'm using to populate DataGrid and ComboBoxes in UI layer. What is correct layer for that class? And what you can say about structure of my project? And what you can advice? If you have and can share a sample of a simple project I will be appreciate.

    Thanks.



    • Edited by zleug Saturday, May 30, 2020 11:03 PM
    Saturday, May 30, 2020 10:52 PM
  • Data layer has folder with Model and Entity Framework tables.

    Yes, you should have a folder in the DAL called Model where the EF dbcontext and virtual object model classes/entities reside that represent the database tables of a database.

    DTO layer has some classes of DataTables.

    DTO layer is abstraction away from the underlying database technology. PL and BLL don't care about the database technology being used such as EF, becuase they are never in direct contact with the EF entities and they never see the EF entities. 

    Business layer has class with couple methods to save and search data.OOK

    OK

    But also I has one more class with some methods which I'm using to populate DataGrid and ComboBoxes in UI layer. What is correct layer for that class? And what you can say about structure of my project? And what you can advice? If you have and can share a sample of a simple project I will be appreciate.

    You should learn how to use the MVVM UI design pattern with WPF, MVVM classes/objects call out to methods on the BLL objects/classes and they in turn call out to methods on DAO objects/classes in the DAL for CRUD operations with the database.

    On one hand you are learning SoC. You also need to learn SoD at the presentation layer.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

    There are plenty of WPF MVVM tutorials out there. There are even some free MVVM frameworks you can implement into your WPF solution.

    You can be help with MVVM at the WPF forum. 

    https://docs.microsoft.com/en-us/answers/topics/wpf.html


    Sunday, May 31, 2020 4:48 AM
  • Here is an article that might help learning MVVM (it's old but still useful).

    Easy MVVM examples (in extreme detail)

    Microsoft code samples

    https://docs.microsoft.com/en-us/search/?search=MVVM&category=All


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, May 31, 2020 10:39 AM
    Moderator
  • some controls in UI Layer such as ComboBox, DataGrid. So, that class should be located in Data Layer or Business Layer?

    Neither the Data Layer nor the Business Layer; the UI Layer, aka the Presentation Layer.

    Multitier architecture - Wikipedia provides an introduction but there are many articles to be found in the internet and there are even books on the subject.

    I am not familiar with MVVM but there seems to a lot of confusion and disagreement about how MVVM relates to n-tier.



    Sam Hobbs
    SimpleSamples.Info


    Wednesday, June 3, 2020 2:12 AM
  • I am not familiar with MVVM but there seems to a lot of confusion and disagreement about how MVVM relates to n-tier.

    The don't relate at all. N-tier is an architectural design pattern and MVVM is a UI design pattern. The only thing they have in common is a way of object interactions due to design pattern usage.  

    Friday, June 5, 2020 4:40 PM