1
\$\begingroup\$

I have created 3 projects to implement layered architecture design mainly the - Presentation Layer (Web) - Domain Model Layer - Infrastructure Layer

For simplicity, I have only two domain model entities: Customer.cs and Category.cs. In my domain model layer, I also put my repository interface (both custom and generic).

In Infrastructure layer, I have the dbcontext, Unit of Work (including its interface) and repository classes (which implement repository interface).

Here is my generic repository interface

public interface IRepositoryGeneric<TEntity> where TEntity : class
{
    // CRD
    void Add(TEntity entity);
    void Remove(TEntity entity);
    TEntity Get(object Id);
    IEnumerable<TEntity> GetAll();

    void Save();
}

Generic Repository class

public class RepositoryGeneric<T> : IRepositoryGeneric<T> where T : class
{
    protected readonly AppDbContext db;
    public RepositoryGeneric(AppDbContext db)
    {
        this.db = db;
    }

    public void Add(T entity)
    {
        throw new NotImplementedException();
    }

    public T Get(object Id)
    {
        throw new NotImplementedException();
    }

    public IEnumerable<T> GetAll()
    {
        return db.Set<T>().ToList();
    }

    public void Remove(T entity)
    {
        throw new NotImplementedException();
    }

    public void Save()
    {
        throw new NotImplementedException();
    }
}

Unit of Work Interface

public interface IUnitOfWork : IDisposable
{
    IRepositoryCustomer Customers { get; }
    IRepositoryCategory Categories { get; }

    int SaveChanges();
}

Unit of Work class

public class UnitOfWork : IUnitOfWork
{
    private readonly AppDbContext db;
    public UnitOfWork()
    {
        db = new AppDbContext();
    }

    private IRepositoryCustomer _Customers;
    public IRepositoryCustomer Customers
    {
        get
        {
            if (this._Customers == null)
            {
                this._Customers = new RepositoryCustomer(db);
            }
            return this._Customers;
        }
    }

    private IRepositoryCategory _Categories;
    public IRepositoryCategory Categories
    {
        get
        {
            if (_Categories == null)
            {
                _Categories = new RepositoryCategory(db);
            }
            return _Categories;
        }
    }

    public void Dispose()
    {
        db.Dispose();
    }

    public int SaveChanges()
    {
        return db.SaveChanges();
    }
}

Yeah, I have yet to add Application layer, which is the Web API layer.

Any comments on the structure of the whole solution? Any comments are welcome.

My whole solution source is available at my github - https://github.com/ngaisteve1/LayeredArchitectureDesignCSharp

\$\endgroup\$

closed as off-topic by yuri, pacmaninbw, Edward, Graipher, IEatBagels Jun 6 at 16:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – yuri, pacmaninbw, Edward, Graipher, IEatBagels
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ I was about to review RepositoryGeneric when I realised this is a mockup class. Perhaps you could be more precise in describing which classes to review. \$\endgroup\$ – dfhwze Jun 5 at 10:56
  • \$\begingroup\$ Yeah, this whole solution is just a mockup to implement repository and uow concept. But, this mockup can be executed. \$\endgroup\$ – Steve Ngai Jun 5 at 11:00
  • 3
    \$\begingroup\$ Since you want a review of your mockup of layers, without any tangible code, I suggest you ask the question at 'software enigneering' softwareengineering.stackexchange.com \$\endgroup\$ – dfhwze Jun 5 at 11:03
  • 2
    \$\begingroup\$ You might want to read this and save yourself a lot of work: cpratt.co/… And also: thereformedprogrammer.net/… ("No, the repository/unit-of-work pattern (shortened to Rep/UoW) isn’t useful with EF Core. EF Core already implements a Rep/UoW pattern, so layering another Rep/UoW pattern on top of EF Core isn’t helpful.") \$\endgroup\$ – BCdotWEB Jun 5 at 11:43
  • 1
    \$\begingroup\$ @bcdotweb I found that unit testing is difficult when EF Core is used directly. The in memory provider works great for dbset, but doesn't really work with dbquery. Also in regards to domain driven design, EF appears to be a bad fit. Domain driven design seems to limit relationship modeling whereas EF is more relational. I am pretty new to domain driven design so I could be wrong. What are your thoughts? \$\endgroup\$ – RockLeeroy Jun 7 at 5:01