I am making a website using ASP.NET MVC and an onion architecture. I have the following architecture:
- Domain : Entities / Domain Interfaces
- Repository : Generic repository (for now) using Entity Framework Code First Approach
- Service : Generic Service that calls the Repository
- MVC
Now I am trying to create a method in my controller to start testing the methods I have implemented in Repository
and Service
, and I am having a hard time as to what I am allowed to create in this controller. I want to test a simple Get
method in the Repository
, but to do that I need GenericService
object and GenericRepository
object in my controller. To demonstrate what I mean here's a snippet of my GenericRepository(I will skip the interfaces):
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private readonly PrincipalServerContext context;
private DbSet<T> entities;
public Repository(PrincipalServerContext context)
{
this.context = context;
entities = context.Set<T>();
}
}
Now my GenericService:
public class GenericService<T> : IGenericService<T> where T : class
{
private IRepository<T> repository;
public GenericService(IRepository<T> repository)
{
this.repository = repository;
}
public T GetEntity(long id)
{
return repository.Get(id);
}
}
And finally, my question, am I allowed to create these objects in my controller as follows (using my dbcontext called PrincipalServerContext):
public class NavigationController : Controller
{
private IGenericService<DomainModelClassHere> domainService;
private IGenericRepository<DomainModelClassHere> domainRepo;
private PrincipalServerContext context;
public ActionResult MyMethod(){
context = new PrincipalServerContext();
domainRepo = new GenericRepository<DomainModelClassHere>(context);
domainService = new GenericService<DomainModelClassHere>(domainRepo);
if(domainService.GetEntity(1)==null)
return View("UserNotFound");//Just as an example
return View();
}
}
Is this allowed? According to Jeffrey Palermo, UI can depend on Service
and Domain
so I don't know about the Repository
. Technically I am not using methods from repository
, but I do need to add a reference to the project.
If I can't then how can I create a new GenericService
if I don't have a GenericRepository
? Is there a better way to instantiate my objects ?
EDIT I think the answer to my question resides in Startup.cs
where I can put something like service.addScoped(typeof(IGenericRepository<>),typeof(GenericRepository<>));
but I 'm not sure about this, any ideas?