none
Layered Application in C# RRS feed

  • Question

  • I have created a basic console layered application in Visual C#.

    Name - Product Management System

    Desc - PMS is aimed to perform all the CRUD operations in a layered architecture have 5 layers :

    1.Entity Layer - Defining the class structure for the application

    2.Exception Layer - handling all exception properly with error messages

    3.Data Access Layer (DAL) - All the CRUD data is saved in the file format using serializable attributes

    4.Bussiness Login Layer (BLL) - It is responsible for the logic and validations of entire application.

    5.Presentation Layer (PL) - All the UI part or the Layer that is visible on console is written in this layer.

    GitHub Link - https://github.com/MahenoorMahida/ProductMgmtSystem.git

    Wednesday, December 11, 2019 6:55 AM

All replies

  • Hi Mahenoor Mahida,
    I viewed your description, but I don't know what your requirement is. Can you explain it in detail?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 11, 2019 7:48 AM
  • 2.Exception Layer - handling all exception properly with error messages

    You should be using centralized exception handlining that catches all unhandled exceptions meaning no try/catch is done in any layer that makes it an unhandled exception, and the GEH catches all unhandled exceptions.

    http://www.tutorialspanel.com/create-a-global-error-handler-in-c-for-a-console-application/

    Also, a DTO travels between layers or processes.

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

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    You could look into using the DAO pattern in the DAL

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

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

    In the below example the DAO pattern is being used in the DAL for CRUD with a database, which pattern can be used in your solution too.

    Also learn how to use an Interface for loose coupling 

    https://www.c-sharpcorner.com/blogs/understanding-interfaces-via-loose-coupling-and-tight-coupling

    https://ardalis.com/new-is-glue

    using Entities;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    
    namespace DAL
    {
        public interface IDaoPayroll
        {
            Task<List<DtoPayroll>> GetAll();
            Task<DtoPayroll> Find(int id);
            Task<DtoPayroll> FindPayRollByAuthorId(int id);
            Task Add(DtoPayroll dto);
            Task Update(DtoPayroll dto);
            Task Delete(int id);
        }
    }
    
    =====================================================
    using DAL.Models;
    using System;
    using System.Threading.Tasks;
    using Entities;
    using System.Collections.Generic;
    using Microsoft.EntityFrameworkCore;
    using System.Data.SqlClient;
    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 async Task<List<DtoPayroll>> GetAll()
            {
                var dtos = new List<DtoPayroll>();
    
                var payrolls = await pc.Payroll.ToListAsync();
    
                foreach (var payroll in payrolls)
                {
                    var dtoauthor = await _daoauthor.Find(payroll.AuthorId); 
    
                    var dto = new DtoPayroll
                    {
                        PayrollId = payroll.PayrollId,
                        AuthorId = payroll.AuthorId,
                        AuthorFirstName = dtoauthor.FirstName,
                        AuthorLastName = dtoauthor.LastName,
                        Salary = payroll.Salary
                    };
    
                    dtos.Add(dto);
                }
    
                return dtos;
            }
    
            public async Task<DtoPayroll> Find(int id)
            {
                var dto = new DtoPayroll();
    
                var payroll = await pc.Payroll.FindAsync(id);
    
                if (payroll != null)
                { 
                    var dtoauthor = await _daoauthor.Find(payroll.AuthorId);
    
                    if (dtoauthor != null)
                    {
                        dto.PayrollId = payroll.PayrollId;
                        dto.AuthorId = payroll.AuthorId;
                        dto.AuthorFirstName = dtoauthor.FirstName;
                        dto.AuthorLastName = dtoauthor.LastName;
                        dto.Salary = payroll.Salary;
                    }
                    else
                    {
                        throw new Exception($"Author with ID = {id} was not found.");
                    }
                }
                else
                {
                    throw new Exception($"Payroll with ID = {id} was not found.");
                }
    
                return dto;
    
            }
    
            public async Task<DtoPayroll> FindPayRollByAuthorId(int id)
            {
                var dto = new DtoPayroll();
                
                var payroll = await pc.Payroll.Where(a =>a.AuthorId == id).SingleOrDefaultAsync();
    
                if (payroll != null)
                {
                    dto.PayrollId = payroll.PayrollId;
                }
    
                return dto;
            }
    
            public async Task Add(DtoPayroll dto)
            {
                var payroll = new Payroll
                {
                    AuthorId = dto.AuthorId,
                    Salary = dto.Salary
                };
    
                pc.Payroll.Add(payroll);
                await pc.SaveChangesAsync();
    
            }
    
            public async Task Update(DtoPayroll dto)
            {
                var payroll = new Payroll
                {
                    PayrollId = dto.PayrollId,
                    AuthorId = dto.AuthorId,
                    Salary = dto.Salary
                };
    
                pc.Entry(payroll).State = EntityState.Modified;
                await pc.SaveChangesAsync();
    
            }
    
            public async Task Delete(int id)
            {
                var payroll =  pc.Payroll.Find(id);
    
                if (payroll != null)
                {
                    pc.Payroll.Remove(payroll);
                    await pc.SaveChangesAsync();
                }
            }
    
        }
    }


    • Edited by DA924x Wednesday, December 11, 2019 3:58 PM
    Wednesday, December 11, 2019 3:16 PM