1

I am new to dependancy injection but found simple injector which seems simple to use yet powerful.

In my project i have the following layers

Dal - consists of repositories that call dbcontext. Each repo has its own interface which inturn implent IRepository

Eg CompanyRepository implements ICompanyRepository

ICompanyRepository implements IRepository

class CompanyRepository : ICompanyRepository
{
    public CompanyRepository(IDbContext context)
    {
        _context = context;
    }
}

Models - all my poco objects

Service - business logic that calls Dal to load model objects. Each service has its own interface which esch implement IService. Eg.

CompanyService implements ICompanyService.

ICompanyService implements IService.

class CompanyService : ICompanyService
{
    public CompanyService(ICompanyRepository repo)
    {
        _repo = repo;
    }
}

WebApi - mvc web api project. Should call Service layer to load and save models. No reference to Dal.

In WebApi i have added SimpleInjector. And my CompanyCo troller has 1 constructor like so

class CompanyController  : ApiController
{
    public CompanyController(ICompanyService service)
    {
        _service = service;
    }
}

Now in my application_start code i register ICompanyService to inject CompanyService. Now i get an error saying that CompanyService expects ICompanyRepository to inject. But my webApi project doesnt reference the dal layer.

How can i get this to work? I shouldnt have to reference the dal from the webapi. I am guessing i am going to get same problem with repos as they have IDbContext in constructor.

Thanks in advance

Gillardo
  • 9,518
  • 18
  • 73
  • 141
  • Even though it's not *directly* referenced, the `ICompnayRepository` is still a dependency of the `CompanyService` and must therefore also be resolved (this is a good thing). It allows you to swap out the concrete instance bound to ICompanyRepository if you wanted to say switch database storage mechanisms. In short, if the type you're binding isn't in the composition root where you're binding it, you either need to define one, or reference the DAL. – xDaevax Sep 15 '14 at 20:09
  • How can i resolve it when i dont reference the project? – Gillardo Sep 15 '14 at 20:10
  • You will need to have a reference to the interface (and the type it binds to). Is there a reason you don't want to? – xDaevax Sep 15 '14 at 20:12
  • 1
    Does it not go against separating code layers? As in the layer abpve shpuld only see the layer below??? – Gillardo Sep 15 '14 at 20:13
  • At some point all the pieces must be stitched together. This is the way you want it though, otherwise where would you put the ICompanyRepository => CompanyRepository binding? If you put it in your DAL, it would be hard-coded always when the DAL was referenced, and couldn't be overridden. Additionally, a Library doesn't have a single point of entry like a composition root, which means there is no suitable place for the binding to occur, even if you could. It isn't bad to reference these things in the composition root (this keeps the rest of your laryers ignorant, but not the CR). – xDaevax Sep 15 '14 at 20:16
  • What you would probably want to avoid, is cross-layer injecting (injecting an IDBContext into a service for example). This is solved only through convention. – xDaevax Sep 15 '14 at 20:17
  • You may find this useful: http://stackoverflow.com/questions/6277771/what-is-a-composition-root-in-the-context-of-dependency-injection – xDaevax Sep 15 '14 at 20:19
  • I asked similar question about the hard dependency of the composition root. I believe it is a bad choice for the API (in my case) to have reference to both repo and business class. I only like my api has reference to a contract (interfaces) project. Much more flexible design. Here is the link to the question https://stackoverflow.com/questions/54082750/simpleinjector-with-api-how-to-do-late-binding-with-simpleinjector?noredirect=1#comment95107626_54082750 – Mhoque Jan 14 '19 at 16:18

0 Answers0