0

I am trying to figure out how to use simpleinjector for following situation.

I have a solution with 4 Projects

  1. Business (Concrete Business Classes)
  2. Repo (Concrete Repo classes)
  3. Contract (Only Interfaces)
  4. API (Composition root)

Business , Repo and Api has direct reference to the Contract.

Contract has Interface for Business and Repo

Business Implements the IBusiness from Contract project and

Repo implements the IRepo from contract project

I don't want the API to have any project reference to Business or Repo project. Those 2 compiled assembly will be pushed to a general location (ie: bin folder on api). Here is a pic or what I want. enter image description here When I look at the documentation for simpleinjector, it asks me to do something like the following

container.Register<IRepo, Repo>();

This assumes the API project has knowledge of the Repo project. It doesn't. API Project only knows about the Contract that has the IRepo and IBusiness interfaces. So my API project will never compile since on the Composition root, while I am Registering the IRepo to Repo, the APi project has no knowledge about the Repo project. API has reference to only the Contract project which only has the interfaces (IBusiness and IRepo). Business and Repo projects are separate from the Contract project and they reference the IBusiness and IRepo Interfaces to create the Business and Repo concrete class.

QUESTION is: How do I do the late binding with SimpleInjector?

In the past, I used ninject and using the ninjectModule, I was able to tell the Business or Repo project If a request from Contract comes for IBusiness or IRepo , it should map that to Business or Repo. I didn't need any reference on my Api to Business concreate class or Repo concrete class.

Can the same be done with the simpleinjector? I don't want to put project reference for Business or Repo in API project.

I am not sure, if I am looking at the right item to achieve this. My problem is understanding this for SimpleInjector.

Please feel free to help.

Mhoque
  • 377
  • 3
  • 11
  • So which project is suppose to be the composition root? – Nkosi Jan 07 '19 at 22:35
  • The Api is the entry point – Mhoque Jan 07 '19 at 22:36
  • I edited the question to narrow the scope for better understanding of my issues. Please take a look. – Mhoque Jan 09 '19 at 22:09
  • @steven Please take a look at the question again. – Mhoque Jan 10 '19 at 17:29
  • You can late bind a type by defining its name in the config file and load the type using `Type.GetType(typeName)`, just as you can see in [listing 1.2](https://livebook.manning.com/#!/book/dependency-injection-in-dot-net-second-edition/chapter-1/v-13/152) of the freely available first chapter of [this book](https://manning.com/seemann2).\ – Steven Jan 10 '19 at 20:10
  • Even with your update, all the information you need is still given in the [duplicate post](https://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application), which is: 1). Neither Business nor Repo should wire their own dependencies, only the Composition Root should do so. 2). It is intentional that the Composition Root references everything, and should not be a concern except when you need to be able to replace either Business or Repo without recompiling your API project, which seems quite unlikely. – Steven Jan 10 '19 at 21:14
  • 1
    3). If you wish the assembly that contains your API Controllers to be decoupled from the Business and Repo assemblies, move that code out of the startup project, into a "Presentation Layer" project. Again, this is all information that can be found [here](https://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application), [here](https://freecontent.manning.com/dependency-injection-in-net-2nd-edition-understanding-the-composition-root/), and in much more detail [here](https://manning.com/seemann2). – Steven Jan 10 '19 at 21:15
  • The Simple Injector feature that is similar to Ninject's Modules, is Packages. You can read more about this [here](https://simpleinjector.org/howto#package-registrations). But as the documentation states, you only need Packages when you need late binding, which I agree you don't (or at least, nothing in your post signals that you require late binding) – Steven Jan 10 '19 at 21:18
  • Steven How will my Composition Root in API will find the Concrete Object Without having to reference the projects (what I want to avoid)? I do have the book and reading through it for awhile now. I also have the first version. I was able to create totally separated layers in the past. All of the projects only referenced the Contract. The business and Repo project had NinjectModule that was doing the mapping. I was still able to swap actual project and test projects properly. I was trying to achieve the same with the SimpleInjector and was having trouble with it.(Ref: Image on main post added) – Mhoque Jan 10 '19 at 21:48
  • Please forgive my ignorance on SimpleInjector, I am just trying to get a better grasp on the tool and I feel like I have a hammer and everything looks like a nail. So I am trying to figure out what "BASIC" part I am missing. – Mhoque Jan 10 '19 at 21:48
  • https://stackoverflow.com/questions/25856073/simple-injector-and-layers?rq=1 This is a similar problem like mine. – Mhoque Jan 10 '19 at 21:50
  • My answer stays the same. By definition, the Composition Root takes a hard dependency on every dependency in the system. This is not something you should prevent. You should prevent any other layer from having a dependency on the DI Container. Both points are described in the book. Simple Injector is by no means limiting you. You can use it in the same way as you did with Ninject. However, my argument is: you shouldn't do the same. Instead, follow the practices described in both editions as they will yield the best result. – Steven Jan 10 '19 at 22:51
  • @Steven, I think this answers if composition root (the API in this case) needs the reference to all the concrete classes. I prefer that API only has reference to the Contract and not any hard referenecs to the Business or Repo. Keeping the API flexible. So in the future, if I decide to create another API project, I can have it reference the Contract only. Business and Repo is aware what concrete class to provide as request comes. I appreciate all your help on this, I learned a lot. Hopefully I didn't ask too many silly questions. – Mhoque Jan 11 '19 at 21:52

0 Answers0