inversion-of-control

Registering implementations of base class with Autofac to pass in via IEnumerable

放肆的年华 提交于 2020-01-02 01:10:19
问题 I have a base class, and a series of other classes inheriting from this: (Please excuse the over-used animal analogy) public abstract class Animal { } public class Dog : Animal { } public class Cat : Animal { } I then have a class that has a dependancy on an IEnumerable<Animal> public class AnimalFeeder { private readonly IEnumerable<Animal> _animals; public AnimalFeeder(IEnumerable<Animal> animals ) { _animals = animals; } } If I manually do something like this: var animals = typeof(Animal)

spring ioc injecting concrete implementation of interface to test

风格不统一 提交于 2020-01-01 19:40:54
问题 I have the following setup: @Component public class ImplOne implements IFace{ } @Component public class ImplTwo implements IFace{ } public interface IFace{ } I am trying to get a reference of ImplOne by type: @RunWith(SpringJUnit4ClassRunner.class) public class ImplOneTest { @Autowired private ImplOne impl; @Test public void test(){ Assert.assertNotNull(impl); } } Though with this I get the following exception: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean

Using Ninject 2.0 with ASP .Net 3.5

天涯浪子 提交于 2020-01-01 16:49:44
问题 I am trying to use Ninject 2.0 with Asp .Net 3.5 web application. Following are the DLLS and it's versions I am using. Ninject.dll - v2.0.0.0 Ninject.Extensions.Logging.dll v2.0.0.0 Ninject.Web.dll v1.0.0.0 In my global.ascx.cs I have following method. protected override IKernel CreateKernel() { IKernel kernel = new StandardKernel(); kernel.Bind<IDataAccess>().To<DataAccessEntBlock>().InSingletonScope(); return kernel; } When I run the application I get following error. Error activating

Autofac named registration constructor injection

那年仲夏 提交于 2020-01-01 08:49:31
问题 Does Autofac support specifying the registration name in the components' constructors? Example: Ninject's NamedAttribute . 回答1: You need to use the Autofac.Extras.Attributed package on top to achieve this So let's say you have one interface and two classes: public interface IHello { string SayHello(); } public class EnglishHello : IHello { public string SayHello() { return "Hello"; } } public class FrenchHello : IHello { public string SayHello() { return "Bonjour"; } } Then you have a

How do I implement a delegate factory?

ε祈祈猫儿з 提交于 2020-01-01 05:17:05
问题 The documentation for Autofac has an interesting page describing its ability to automatically generate delegate factories. It also strongly suggests that you can get similar results without Autofac by writing them by hand. I'm using Unity for IoC and would like to avoid passing the container around to objects that need to create other objects, so how would you write a delegate factory without Autofac? 回答1: Well I've never used Unity so far, so my answer is quite vague. The principal is simple

Autofac: How to limit the lifetime of an IDisposable object without passing around the IoC container

本秂侑毒 提交于 2020-01-01 02:39:31
问题 I'm currently learning how to use Autofac, and I'm stuck with disposing IDisposable objects deterministically. Let me first present the situation before I'll state my problem. Starting position: Let's say my object model is defined through the following interfaces: interface IApple : IDisposable { void Consume(); } interface IHorse { void Eat(IApple apple); // is supposed to call apple.Consume() } interface IHorseKeeper { void FeedHorse(); // is supposed to call horse.Eat(apple) // where

Castle Windsor or Spring.NET - advantages and disadvantages [closed]

有些话、适合烂在心里 提交于 2020-01-01 02:25:12
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Yesterday I was reading some articles in this site while I stumbled on an article about this two new IoC tools. Which one should I

How to configure unity container to provide string constructor value?

眉间皱痕 提交于 2020-01-01 01:31:06
问题 This is my dad class public class Dad { public string Name { get;set; } public Dad(string name) { Name = name; } } This is my test method public void TestDad() { UnityContainer DadContainer= new UnityContainer(); Dad newdad = DadContainer.Resolve<Dad>(); newdad.Name = "chris"; Assert.AreEqual(newdad.Name,"chris"); } This is the error I am getting "InvalidOperationException - the type String cannot be constructed. You must configure the container to supply this value" How do I configure my

Configuring Unity to resolve a type that takes a decorated dependency that has a parameter that varies with the type into which it is injected

我的梦境 提交于 2019-12-31 21:30:31
问题 This is a fairly straight forward decorator pattern scenario, with the complication that the decorated type has a constructor parameter that is dependent on the type into which it is being injected. I have an interface like this: interface IThing { void Do(); } And an implementation like this: class RealThing : IThing { public RealThing(string configuration) { ... implementation ... } public void Do() { ... implementation ... } } And a decorator like this: class DecoratingThing : IThing {

What is the actual difference betwen a service locatior and a dependency injection?

家住魔仙堡 提交于 2019-12-31 06:20:08
问题 I was going through the previous discussion in which there was a detailed discussion on the difference between a service locator and a dependency injector, but still I am not able to get that. Can I get a general response without any code? 回答1: This code sample applies the Dependency Injection principle: public class UserService : IUserService { private IUserRepository repository; // Constructor taking dependencies public UserService(IUserRepository repository) { this.repository = repository;