locked
Data Layer RRS feed

  • Question

  • Is it possible that in a project I have four layers of Model, Repository, Service, and Context, and manage the data? Could you send a sample of the project?
    Wednesday, April 17, 2019 4:49 AM

All replies

  • Hi Ali_Lonbar,

    You could download the source file from the code project for reference.

    https://www.codeproject.com/search.aspx?q=Data+Layer+c%23&doctypeid=1%3b2%3b3%3b13%3b14

    Best Regards,

    Wendy


    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, April 17, 2019 8:55 AM
  • Hello,

    I don't have a code sample, all of my work is rather complex in nature. Here I show one class project which includes Model, Repository and controller while the second class project is the core for data foundation. What is not shown, another solution which contains generic methods for working with controller/model and repository.

    Note the pattern which works for my team may not work for you or your team, no matter, you and/or your team should study different patterns and decide what feels good to you and what fits into a solution being coded.

    Code such as this resides in the shared class project.

    public class BaseRevenueAsyncApiController<TEntity, TDtoEntity, TBusinessLogic> :
        ApiController, IRevenueAsyncApiController<TEntity, TDtoEntity>
        where TEntity : class
        where TDtoEntity : class
        where TBusinessLogic : IBusinessLogic<TEntity,TDtoEntity>
    {
        public TBusinessLogic BusinessLogic { get; set; }
    
        /// <summary>
        /// constructor for dependency injection
        /// </summary>
        /// <param name="pBusinessLogic"></param>
        public BaseRevenueAsyncApiController(TBusinessLogic pBusinessLogic)
        {
            BusinessLogic = pBusinessLogic;
            CreateMap();
        }
    
        /// <summary>
        /// Return item from database by id field
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<IHttpActionResult> Get(int id)
        {
            return Ok(Mapper.Map<TEntity, TDtoEntity>((await BusinessLogic.GetByKey(new object[] { id }))));
        }


    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

    Friday, April 19, 2019 1:26 PM
  • If you are referring to the Entity Framework DbContext - that would not be a layer. It would be a component of your data access layer, in your case a repository. You would inject it into your repository.

    Model(s) will be in a separate project.

    I assume you are trying to implement the Repository pattern with a UnitOfWork. Keep in mind, its a bit redundant given the DbContext is basically a UnitOfWork and the DbSet is a repository.

    Here is some good guidance on using EF Core.

    https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-implemenation-entity-framework-core


    william xifaras

    Friday, April 19, 2019 6:27 PM