I recently used Dependency Injection pattern with Autofac as the IoC Container.
Normally, I would use it inside core application (Winform, WCF, WPF etc).
Currently I'm learning to create a class library project as a framework for my peer developers, and I tend to stick with the DI pattern since it allows me to unit test as I go.
How do I configure the IoC Container in a class library project without an entry point?
Should I just make something like :
public static void ConfigureLibrary() {
//.. Do bootstraping here
}
and let core application to call it when the core app start?
How did libraries like the Patterns & Practices Enterprise Library or Spring.Net configured?
How did libraries like the Patterns & Practices Enterprise Library or
Spring.Net configured?
Here is an interesting article from Chris Tavares about the subject.
How do I configure the IoC Container in a class library project without an entry point?
Generally the application should be the root for your service registrations. But yes, one may provide default-registrations in a library. Here is a blog-post I did some days ago how I and my team currently do.
Related
I'm configuring DI container to be able to resolve several services inside my app.
If I would have used autofac, I would use modules from there to logically bundle registrations.
Does ASP.NET core framework has analogues for modules? How can I achieve that if I want?
Official doc on DI says:
Does that mean that I shouldn't be looking for module system anymore?
And if I have like 20-50 services, it's best to create an extension for each separate one?
AFAIK there is nothing comparable to autofac's modules registration in the core DI implementations.
You would have to implement it on your own, which should not become a really big deal.
I am confused in DI. I have seen lot of articles that explains DI can be implemented using constructor and some articles mentioned DI using ninject. So I am not able to understand the use of ninject. if ninject is not there then also the code will work. Can you please explain what will the advantage of using ninject.
Quoting Darin Dimitrov
What does using Ninject provide for me that I can't do by just
following basic principals of loose coupling?
Fewer lines of code in the Composition Root
A standard container for handling your object lifetimes
Many plugins for injection in specific contexts such as classic WebForms, ASP.NET MVC, ASP.NET Web API
Possibility to automatically dispose your IDIsposable objects
...
All things that you should be handling manually otherwise. This being said, the DI framework is of little importance. It should be fast and offer the specific features you need for your application. But the DI framework should absolutely in no way influence the way you are designing your code and the different layers in your application in a loosely coupled manner (programming against interfaces and abstract classes to weaken the coupling between the different layers of your application).
So think of the DI framework as something that intervenes only in the Composition Root of your application and as a framework that you could replace in a blink of an eye with a different framework or even manually handling your object lifetimes.
For example the code you have shown in your question is very bad as it ties your layers to a specific DI framework. This [Inject] attribute over there is like a cancer. It means that your application code relies on a specific DI framework.
Also i suggest you read this book.
A would like to make my ASP.NET Core project could see only another library: Api, where I have interfaces. And another library makes wiring for interfaces and their implementation.
Instead of it Microsoft Dependency Injection Library proposes to make a wiring point right in the asp.net project. In this case this project will see both libraries: Api and ApiImpl. And it's not acceptable.
I wanna find a solution like Ninject, for example, does with modules.
A kind solution of this is here:
https://github.com/aspnet/DependencyInjection/issues/497
I have a set of projects which provide all the functionality necessary to run a .Net service. The project makes use of Dependency Inject via Ninject.
My (simplified) solution looks like this:
Project 1: Windows Service (Composition Root)
^
Project 2: Server Engine & Heavy Lifting (Ninject Module)
^
Project 3: Persistence / DAL (Ninject Module)
^
Project 4: Interfaces & Basic / Shared functionality (Interfaces)
After some significant development, turns out I will need to create a new "client" library project used to communicate with the Windows Service. This client becomes a new project (Project 5) and depends on the functionality in Project 3 and Project 4. I.e.:
Project 5: Client (Class Library)
^
Project 3: Persistence / DAL (Ninject Module)
^
Project 4: Interfaces & Basic / Shared functionality (Interfaces)
3 Immediate Problems:
I don't want to put the responsibility of wiring up an object graph on the consumer of the Client. The consumer should just use it like you have any other library you've used. You shouldn't have to worry about the internals of the library to make it work.
The Client is a class library and doesn't have an entry point of its own to consider my composition root and establish the object graph.
Even if I did have an entry point in the Client, my reading suggests I shouldn't have library dependencies on a DI framework "In that case [of creating a reusable library] you should typically make your library working without a DI container. You should yourself not take a dependency on such a container, because this would drag the container in" (from Locate the correct composition root for a .NET library). Another excerpt from a different post reads: "Only applications should have Composition Roots. Libraries and frameworks shouldn't." (from http://blog.ploeh.dk/2011/07/28/CompositionRoot/).
These problems root from the fact that I have already built much functionality into Project 3 and Project 4 that is mandatory to be used in the Client, yet these 2 projects already have a dependency on Ninject, so by proxy, the client also has a dependency on Ninject.
I can easily solve this problem using various methods, but I haven't found a resouding solution, they all feel "hacky", like I'm shoving a square peg in a round hole (What are the best practices for class libraries using dependency injection for internal operations?).
Is there any IoC container out there which supports (or can be made to) the Portable Class Libraries yet?
I fiddled around with some (SimpleInjector, AutoFac) but they always had one dependency or another which prevented me from using them as a portable class library.
I'm fairly new to the topic so I maybe totally on the wrong track here.
In more detail:
I want to create a library containing my models (and later viewmodels) for a MMVM app which should run on .Net 4.5, WP7 and WinRT. This models should be saveable as files. Since the implementation of the particular save algorithms (desktop filesystem, isolated storage) is specific to every platform I hoped to utilize an IoC container to decouple it from the models themselves.
I believe there is a beta of autofac for portable libraries: http://code.google.com/p/autofac/downloads/detail?name=Autofac-2.6.1.841-Portable.zip