Ioc/DI – Why do I have to reference all layers/assemblies in application’s entry point?

If I wasn’t using a DI container, I wouldn’t have to reference EntityFramework library in my MVC3 app, only my business layer which would reference my DAL/Repo layer. Yes, that’s exactly the situation DI works so hard to avoid 🙂 With tightly coupled code, each library may only have a few references, but these again … Read more

Is there a pattern for initializing objects created via a DI container

Any place where you need a run-time value to construct a particular dependency, Abstract Factory is the solution. Having Initialize methods on the interfaces smells of a Leaky Abstraction. In your case I would say that you should model the IMyIntf interface on how you need to use it – not how you intent to … Read more

How to resolve IOptions instance inside ConfigureServices?

If you need to resolve service using the service provider manually you can use this AddSingleton/AddScoped/AddTransient overload: // Works for AddScoped and AddTransient as well services.AddSingleton<IBarService>(sp => { var fooService = sp.GetRequiredService<IFooService>(); return new BarService(fooService); } If you really want to, you can build an intermediate service provider using the BuildServiceProvider() method on the IServiceCollection: … Read more

Dependency injection through constructors or property setters?

Well, it depends :-). If the class cannot do its job without the dependency, then add it to the constructor. The class needs the new dependency, so you want your change to break things. Also, creating a class that is not fully initialized (“two-step construction”) is an anti-pattern (IMHO). If the class can work without … Read more

What is Castle Windsor, and why should I care?

Castle Windsor is an inversion of control tool. There are others like it. It can give you objects with pre-built and pre-wired dependencies right in there. An entire object graph created via reflection and configuration rather than the “new” operator. Start here: http://tech.groups.yahoo.com/group/altdotnet/message/10434 Imagine you have an email sending class. EmailSender. Imagine you have another … Read more

How to explain dependency injection to a 5-year-old? [closed]

I give you dependency injection for five-year-olds. When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn’t want you to have. You might even be looking for something we don’t even have or which has … Read more

Dependency Inject (DI) “friendly” library

This is actually simple to do once you understand that DI is about patterns and principles, not technology. To design the API in a DI Container-agnostic way, follow these general principles: Program to an interface, not an implementation This principle is actually a quote (from memory though) from Design Patterns, but it should always be … Read more

Do I need dependency injection in NodeJS, or how to deal with …?

In short, you don’t need a dependency injection container or service locater like you would in C#/Java. Since Node.js, leverages the module pattern, it’s not necessary to perform constructor or property injection. Although you still can. The great thing about JS is that you can modify just about anything to achieve what you want. This … Read more

How to avoid Dependency Injection constructor madness?

You are right that if you use the container as a Service Locator, it’s more or less a glorified static factory. For lots of reasons I consider this an anti-pattern (also see this excerpt from my book). One of the wonderful benefits of Constructor Injection is that it makes violations of the Single Responsibility Principle … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)