Dependency injection and coupling - how wrong is this?

Asked

Viewed 83 times

1

I created a project where the intention was to have a simple architecture, a rich domain and a webApi, but as soon as I started and already seeing the cagad*s, this is very bad?

So come on, I created a generic repository on my domain, I implemented this repository on the bottom...

public interface IRepository<TEntity> where TEntity : Entity
{
    void Add (TEntity obj);
    TEntity Get(int id);
    IEnumerable<TEntity> GetAll();
    void Remove(TEntity obj);
    TEntity Update(TEntity obj);    
}

So far so good, but wanting to save time... now comes what to me is wrong, I decided that for each entity I will have a service as follows:

public class DepartmentService
    {
        private readonly IRepository<Department> Repository;
        public DepartmentService(IRepository<Department> repository)
        {
            Repository = repository;
        }

        public void AddDepartment(Department obj)
        {
            Repository.Add(obj);
        }
        public List<Department> AllDepartments()
        {
            return Repository.GetAll().ToList();
        }
    }

(It is a domain service in it that will stand the rules of business)

My service receives a repository of a type (in this case Department) by dependency injection and in my controller ta thus:

public class DepartmentController : ControllerBase
{

    private DepartmentService _Service;

    public DepartmentController(IRepository<Department> repository)
    {
        _Service = new DepartmentService(repository);
    }

    [HttpPost]
    public IActionResult AddDepartment(Department obj)
    {
        _Service.AddDepartment(obj);

        return NoContent();
    }

    [HttpGet]
    public ActionResult<IEnumerable<Department>> GetAllDepartment()
    {

        return  _Service.AllDepartments();
    }

}

As you may notice, I prompt a service by passing a repository of a specific type, which I am receiving by dependency injection....

And to solve this in my Configureservices I used Addscoped:

services.AddScoped<IRepository<Department>, Repository<Department>>();

I didn’t want to generate so much coupling, but in my webApi it is dependent on the domain and the infra.

That’s what I did?

  • 1

    This is a simple CRUD design, in a Data Centric modeling. It’s neither good nor bad, that’s it. It may or may not be appropriate: this is the point. It is necessary to understand the problem to understand if this is the best solution.

1 answer

1


I don’t see a problem, some coupling will need to have. The important thing is that the domain has no reference. You could in the case, inject also the Departamentservice and apply the Unitofwork Pattern and separate what is query from what is transactional (CQRS Pattern). Always trying to get the maximum coupling from the Controller.

You can check out an example of monolithic clean architecture, but using design standards principles:

https://github.com/dotnet-architecture/eShopOnWeb

Or using DDD with microservices:

https://github.com/dotnet-architecture/eShopOnContainers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.