Asp.Net Core: register implementation with multiple interfaces and lifestyle Singleton

The service collection by definition is a collection of ServiceDescriptors, which are pairs of service type and implementation type. You can however get around this by creating your own provider function, something like this (thanks user7224827): services.AddSingleton<IInterface1>(); services.AddSingleton<IInterface2>(x => x.GetService<IInterface1>()); More options below: private static MyClass ClassInstance; public void ConfigureServices(IServiceCollection services) { ClassInstance = new … Read more

With Unity how do I inject a named dependency into a constructor?

You can configure dependencies with or without names in the API, attributes, or via the config file. You didn’t mention XML above, so I’ll assume you’re using the API. To tell the container to resolve a named dependency, you’ll need to use an InjectionParameter object. For your ClientModel example, do this: container.RegisterType<IClientModel, ClientModel>( new InjectionConstructor( … Read more

Resolve IContainer

You’ll need IComponentContext or ILifetimeScope for most purposes. These are automatically provided by Autofac, so all you need to do is take a (constructor) dependency on one of them and the current instance will be injected. Both interfaces provide the standard Resolve() operations, while ILifetimeScope extends IComponentContext by adding methods for starting new nested lifetimes. … Read more

Application architecture/composition in F#

This is easy once you realize that Object-Oriented Constructor Injection corresponds very closely to Functional Partial Function Application. First, I’d write Dings as a record type: type Dings = { Lol : string; Rofl : string } In F#, the IGetStuff interface can be reduced to a single function with the signature Guid -> seq<Dings> … Read more

What do programmers mean when they say, “Code against an interface, not an object.”?

Consider: class MyClass { //Implementation public void Foo() {} } class SomethingYouWantToTest { public bool MyMethod(MyClass c) { //Code you want to test c.Foo(); } } Because MyMethod accepts only a MyClass, if you want to replace MyClass with a mock object in order to unit test, you can’t. Better is to use an interface: … Read more

Why not use an IoC container to resolve dependencies for entities/business objects?

The first question is the most difficult to answer. Is it bad practice to have Entities depend on outside classes? It’s certainly not the most common thing to do. If, for example, you inject a Repository into your Entities you effectively have an implementation of the Active Record pattern. Some people like this pattern for … Read more

What is a composition root in the context of dependency injection?

The composition root is the single place in your application where the composition of the object graphs for your application take place, using the dependency injection container (although how this is done is irrelevant, it could be using a container or could be done manually using pure DI). There should only be one place where … Read more

Using IoC for Unit Testing

Generally speaking, a DI Container should not be necessary for unit testing because unit testing is all about separating responsibilities. Consider a class that uses Constructor Injection public MyClass(IMyDependency dep) { } In your entire application, it may be that there’s a huge dependency graph hidden behind IMyDependency, but in a unit test, you flatten … Read more

Enterprise Library Unity vs Other IoC Containers [closed]

I am preparing a presentation for a usergroup. As such I just went through a bunch of them. Namely: AutoFac, MEF, Ninject, Spring.Net, StructureMap, Unity, and Windsor. I wanted to show off the 90% case (constructor injection, which is mainly what people use an IOC for anyway). You can check out the solution here (VS2008) … Read more

Must Dependency Injection come at the expense of Encapsulation?

There is another way of looking at this issue that you might find interesting. When we use IoC/dependency injection, we’re not using OOP concepts. Admittedly we’re using an OO language as the ‘host’, but the ideas behind IoC come from component-oriented software engineering, not OO. Component software is all about managing dependencies – an example … Read more

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