Search code examples
c#asp.netasp.net-mvcdependency-injectiondecoupling

How can i decouple this project


I have a scenario where my business layer has a direct reference in the form of a dll to my database layer which is acting as the repository in this project, now this means there is a direct dependancy on the database layer which would need decoupling, please see structure below:

Project Structure between Business layer and Database Layer Business layer directly referring to database

I know how to use DI and containers for this, but i would like some tips and how to decouple this in some form without having to add a IRepository interface in the business layer and the database layer having concrete repositories calling the irepository in the business layer. Does this make sense at all.

I have a repository interface which sits in the Database_Layer dll :

 public interface IRepository
 {
    Task Add(Object item);

    Task Remove(int id);

    Task<Object> Find(int id);

    Task UpdateAsync(Object item);

    Task<IEnumerable<Object>> GetAllAsync();

    Task<IEnumerable<Object>> GetAllAsync(Object id);
}

Then i have a class in the Business tier called ContentPanel :

using Database_Layer.Interfaces;
namespace Business_Tier.Concrete
{
public class ContentPanel : IPanel
{
    IRepository _repository;
    public ContentPanel(IRepository repo)
    { 
        this._repository = repo;
    }

    public IResolver _Resolver { get; set; } = null;
    public IResolver Resolver { get => _Resolver; set => _Resolver.Resolve<IRepository>("ContentRepository",_repository); }

    public void Delete_Content_Panel(int id)
    {

    }

}

I want to eliminate the using reference the database_layer.interfaces and just resolve the repository dependancy, would i have to define the repository interface in the Business layer, in order to do this?


Solution

  • You business layer(dll) have a dependency on your data access layer(dll)

    You need to use an interface to abstract the business layer contract from the data access implementation

    Using DI in the Startup project will allow you to inject the desired implementation to that interface

    You business layer will have always a physical dependency in the data access layer but will not have a dependency in the data access implementation.