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?
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.
– Luiz Carlos Faria