dependency-injection

Using a dependency injection framework or write your own?

When starting out with a new application, would you rather just use an existing dependency framework and risk the possible shortcomings, or would you opt to write your own which is completely adaptable and why? ...

Dependency Injection in an n-tier application?

I have a 3-tier .NET service app, which follows the standard approach: Frontend -> Object Model / Business Logic -> Data Access I'm trying to learn about dependency injection along the way, and thus far have found it great (using Autofac). Each of the 3 tiers needs to create an assortment of objects, sometimes with extra configuration...

Castle Windsor: How to prevent circular references in factory-created objects were the created objects refers back to the factory.

I am using windsor castle as my IoC container, and has run in to a bit of a problem. This is best explained in code, so I´ll give it a try. I have a factory class, that should provide me with implementations of a certain interface: public interface IObjectCreatorFactory { IObjectCreator GetObjectCreator(Type objectType); } public in...

Primitive types and IoC containers

How do you handle primitive types when using a IoC container? I.e. given that you have: class Pinger { private int timeout; private string targetMachine; public Pinger(int timeout, string targetMachine) { this.timeout = timeout; this.targetMachine = targetMachine; } public void CheckPing() { ...

Using a dependency injection system how do you unit test your code.

As far as I can see there are two ways, both with their drawbacks. Get the object you are unit testing from the dependency injection system. This is low maintenance as you don’t have to manage anything when you change the framework around. But you are essentially testing the whole system from the point of view of your object, if a comp...

StructureMap CacheBy InstanceScope.HttpSession not working

This is my StructureMap bootstrapping: ObjectFactory.Initialize(factory => { //Business Conversation is per session factory.ForRequestedType<IConversation>(). TheDefaultIsConcreteType<Conversation>(). CacheBy(InstanceScope.HttpSession); //Session Factory is life time factory.ForRequestedType<INHibernateSessionManager>...

What are some of the things to watch for (pitfalls) while using IOC container?

What are some of the things to watch for (pitfalls) while using IOC container? ...

good open source project using Dependency Injection Framework?

does anyone know about an open source project Implementing Ninject Framework or another one, I haven't work with a DI framework before and Ninject calls my attention because you don't need to have your configuration in XML files. i tend to grasp a lot more when reviewing already implemented code. also if you know about another good p...

Understanding the need for a DI framework

This might be a naive question. I'm currently learning the Spring framework and dependency injection. While the basic principle of DI is rather easy to grasp, it's not immediately obvious why you need an elaborate framework to implement it. Consider the following: public abstract class Saw { public abstract void cut(String wood); }...

Why do Dependency Injection frameworks support container hierarchies?

I've been following Daniel Cazzulino's series about building a DI container using TDD. In part five of the series, he adds support for container hierarchies without commenting on what makes this feature useful. I've seen mention of support for hierarchies in many of the DI frameworks, but I'm having trouble understanding when they'd be u...

Why would you want Dependency Injection without configuration?

After reading the nice answers in this question, I watched the screencasts by Justin Etheredge. It all seems very nice, with a minimum of setup you get DI right from your code. Now the question that creeps up to me is: why would you want to use a DI framework that doesn't use configuration files? Isn't that the whole point of using a DI...

Testing IO.Stream interaction

I have a method in my business logic layer that accepts a stream, which in the GUI comes from a user uploading a file, and I am interested in which is an appropriate way to test that the method appropriately uses this stream to make decisions. public Sub Initialize(ByVal uploadStream As Stream) ''// Logic using uploadStream End Sub ...

Presenter injection in Model-View-Presenter pattern with StructureMap

I've implemented my own copy of the model view presenter pattern (in vein of web client software factory) so I can leverage my own DI framework instead of being tied to WCSF's ObjectBuilder which I had numerous problems with. I've come up with a few ways to do it but none of them particularly make me happy. I wanted to know if anyone els...

Do Dependency Injection frameworks lead to poor/lazy design?

I'm fairly new to the DI concept, but I have been using it to some extent in my designs - mainly by 'injecting' interfaces into constructors and having factories create my concrete classes. Okay, it's not configuration-based - but it's never NEEDED to be. I started to look at DI frameworks such as Spring.NET and Castle Windsor, and stu...

dependency injection and logging interfaces

I was wondering what some of the best practices were around logging and logging frameworks and dependency injection. Specifically, if I am designing a class that needs a way to log, how should I go about getting an interface to log to keeping dependency injection in mind? Dependency injection seems states that external dependencies shou...

Is there a commonly used OO Pattern for holding "constant variables"?

I am working on a little pinball-game project for a hobby and am looking for a pattern to encapsulate constant variables. I have a model, within which there are values which will be constant over the life of that model e.g. maximum speed/maximum gravity etc. Throughout the GUI and other areas these values are required in order to correc...

Unity - How to register types in the main container, but resolve in a child container

I have a unity container and use RegisterType to register the following repository and implementer using ContainerControlledLifetimeManager. public interface IPersonRepository { Person GetByID(ObjectSpace objectSpace, int id); } Using this pattern I am able to have multiple threads (it's a web app) using the same repository instance...

Is there ever a case for 'new' when using dependency injection?

Does dependency injection mean that you don't ever need the 'new' keyword? Or is it reasonable to directly create simple leaf classes such as collections? In the example below I inject the comparator, query and dao, but the SortedSet is directly instantiated: public Iterable<Employee> getRecentHires() { SortedSet<Employee> entries ...

Injector.getInstance(..) returns a new instance for a singleton

My Module: bind( Translator.class ).to( TranslatorImpl.class ).in( Scopes.SINGLETON ); Now I expect to get the same instance everytime when I do Injector injector = ...; injector.getInstance( Translator.class ); But if I do injector.getInstance( TranslatorImpl.class ); I get a new instance everytime. Is this a bug or expected b...

ASP.NET MVC + fluent nNibernate, what IoC tool?

I'm working on a ASP.NET MVC project where we have decided to use Fluent nHibernate for dataccess. To enable loose coupling we go for a IoC/DI pattern. My questions is what IoC tool to go for. I've tried to find the differences between windsor, ninject, spring, structuremap and unity, but it's difficult to see the benefits each one has t...