simpleinjector / SimpleInjector

An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
https://simpleinjector.org
MIT License
1.21k stars 153 forks source link

How to use SimpleInjector in a class library of ASP.NET MVC #417

Closed vxhoa closed 7 years ago

vxhoa commented 7 years ago

Hi,

I created a class library project that it is being used/referred in the ASP.NET MVC project. The question is how to set up SimpleInjector in a class library. What is the entry point in a class library?

Thanks Hoa

dotnetjunkie commented 7 years ago

What is the entry point in a class library?

A class library does not have an entry point.

how to set up SimpleInjector in a class library.

You don't. You set up your DI Container inside the Composition Root and only start-up projects (your MVC project) should contain a Composition Root.

cezarlamann commented 7 years ago

Hi @vxhoa! How have you been? I'll give my 2 cents here.

In my scenario, I created a "Cross-cutting" library that has references to every other "injectable" library in my project. This way, I could inject repositories on some integration tests, just by creating a "base test class" that calls my container configuration method (from that "cross-cutting" library) inside of this "base test class" constructor method.

This way, my base test class act as a "composition root" for every integration test (I know that it's not the best approach but I'll fine tune this later).

Reminder: This is NOT a proper way of doing things, but it's the easiest way I found to "inject my injectables" inside an integration test project.