dependency-injection

Dependency injection - best practice for fully decoupled components?

I want to use dependency injection (Unity) and at the moment I'm thinking about how to setup my project (it's a fancy demo I'm working on). So, to fully decouple all components and have no more assembly dependencies, is it advisable to create an assembly ".Contracts" or something similar and put all interfaces and shared data structures...

Deterministic initialization and dependency injection (constructor based)

My demo application I'm working on has a very long startup routine. The application I'm trying to replace with the new ideas log a lot to the console during that (imagine: "now loading data... reticulating splines... login to third party service..."). After spending the whole day learning DI basically from scratch, I create the whole (!...

Unity: How to ensure singleton instances for stateful classes across instances of the UnityContainer class?

The application I'm working on requires objects to be created on the fly, some of them also use singleton instances (lifetime manager based, of course), because there is state involved (think WCF service sessions, etc...). As long as I resolve via the same instance of the UnityContainer class, everything is fine, but I think here the sn...

Simplest explanation of how a DI container works?

In simple terms and/or in high-level pseudo-code, how does a DI container work and how is it used? ...

Can I take advantage of Dependency Injection here?

Hi All, I'm working on a project that's using the MS Application Blocks. I see the 'Unity' dll is available to me. It's version 1.1 by the way. How can I use dependency injection here? I have a class public class ScheduleDataDetailsDC { public int ScheduleID; public List<ScheduleRateLineItem> MinRateList; public List<Schedu...

Configure Unity Injection for all descendants of some base class

How to configure Unity so that any class derived from some base class would go through injection pipeline defined for base class. public abstract class Base { public IDependency Dependency {get;set;} }; public class Derived1: Base { }; public class Derived2: Base { }; container.RegisterType<Base>(new InjectionProperty("Dependenc...

Inject Array of Interfaces in Ninject

Consider the following code. public interface IFoo { } public class Bar { public Bar(IFoo[] foos) { } } public class MyModule : NinjectModule { public override void Load() { Bind<IFoo[]>().ToConstant(new IFoo[0]); // ToConstant() is just an example } } public class Program { private static void ...

Dependency Injection and loading dependent assembly in .NET

I'm working with dependency injection (DI from now on) to resolve components and am looking for an elegant solution to loading dependent assemblies in a .NET environment. As an explanation, lets say that we have 3 assemblies, A, B and C, where A depends on B and B depends on C. Now I need to load components from assembly A, but because I...

How do I make an optional binding in Guice?

Here is my client: class Client { @Inject(optional=true) Service service; } Sometimes that Service isn't needed, and we know that information when the JVM starts (i.e before the binder is run). How do I make the binding optional? If I don't specify a binding at all it tries to new the Service (and fails because there is no zero-ar...

Castle Windsor RemoveComponent False

I am calling Kernel.RemoveComponent on my Windsor container and it is returning false. I know the component is present (I have verified by calling GetHandler with the same key and it returns the expected info)...so why can't I remove my component from the container? How can I troubleshoot this? I have a bunch of authentication and autho...

Effective Java item 1 applicability with TDD and dependency injection

I have been reading Effective Java and I have some concerns regarding the first Item "use static factory method instead of constructor" in relation to TDD and dependency injection. The item says that you should avoid having public/protected/default constructor and expose it using static factory. I agree with all the advantages relate...

Dependency injection when the class created also needs runtime values?

Assume you divide up your systems in Value objects and Services objects (as suggested in "Growing Object-Oriented Software, Guided by Tests". Misko Hevery calls these "newables" and "injectables". What happens when one of your value objects suddenly needs to access a service to implement it's methods? Let's say you have a nice simple ...

Unity 2.0: How to create child containers on CTor injection?

Hello, I have a MessageSender class that its constructor looks like this: public MessageSender(IInputComponent input, IOutputComponent output) { m_Input = input; m_Output = output; } This is how I instantiate it: Container.RegisterType<IMessageSender, MessageSender>(); Container.RegisterType<IInpu...

How to expose classes that implement interfaces and use unmanaged resources?

Hi. I have a question regarding use of interfaces when unmanaged resources come to play. Suppose I have a web service and generated WCF client. Service contract looks like this: [ServiceContract] public interface ITestService { [OperationContract] string GetData(int value); } On client side I use dependency injection and bind...

Unit testing object construction/initialization.

I have a class Foo that uses another class Bar to do some stuff. I'm trying to do test driven development, and therefore am writing unit tests for Foo to make sure it calls the appropriate methods on Bar, and for this purpose I'm using dependency injection and mocking Bar (using Rhino Mocks). E.g. (in C#): class Foo { private IBar b...

TDD, creating layer of abstraction

Hi guys, Basically, there is a system at my work place that provides OCR capabilities. The process is that, a third party application is configured to display a captured screen (during the OCR process) and a user sits at the pc making sure the captured data is correct. This capture stage has validation on each of the fields. for exampl...

Is it ok to register components in Windsor without specifying an interface?

Is it considered bad form to register components in Windsor without specifying an interface? i.e. container.Register(Component.For().LifeStyle.Transient); as opposed to... container.Register(Component.For().ImplementedBy().LifeStyle.Transient); I understand the benefits of coding to an interface rather than a concrete implementation ...

Is this a good reason to use IoC/DI?

Im new to the IoC/DI concept.. I have a good MEF demo which shows how you can make a program that accepts .DLLs that implement a certain interface and so you can have different filters on a picture http://www.wintellect.com/CS/blogs/jprosise/archive/2010/01/04/silverlight-4-s-new-managed-extensibility-framework.aspx So my case is a WC...

Does Funq IoC Container support property injection?

Hi, I'm looking for an IoC container to use in my Compact Framework application. Trying out Funq I noticed that I can't find a way to do Property Injection with it. I've looked through the discussion on the project's site and the unit tests for it, but I can't find any example of Property Injection. Does Funq support Property Injecti...

What's the best way to inject a class into another Objective C class for testing purposes?3

Let's say I have a class DataFeed which creates an instance of another class, Parser, as part of its work. i.e. Parser *parser = [[Parser alloc] init]; [parser start]; I'm writing unit tests for DataFeed. I want to substitute a mock for the Parser. I can't work out how best to do this. I'm sure I must be missing something really obvio...