dependency-injection

conditional beans using spring

I am trying to write a ValidatorFactory which will give me a validator based on its type public Validator getNewValidator(ValidatorType type){ switch: case a : new Validator1(); break; case b : new Validator2(); break; } I want to write using spring xml beans definition I can use method inje...

GWT / GIN: Adding user class to all presenters

I have a hazy understanding of GIN, but have it working for injecting presenters, etc. I'm trying to inject a self-made "User" class into all my presenters in order to get the currently logged-in user. I've added @Inject to the constructor on my User class, and added User to my GIN module ... but apart from that, I'm totally lost. Do ...

Is Dependency Injection a pattern and, is this it?

I was having a heated debate with one of my colleagues on dependency injection, and realized I didn't quite know all the facts on the subject. So, take this code (just so you know,we're using Castle Windsor) IPlayerService service = Container.Resolve<IPlayerService>(); The above code is obviously an example of DI using an IoC. Howev...

Avoiding circular dependencies in XNA using Ninject 2.0

I have been using Ninject as the IOC for an XNA project, and was wanting to migrate it to Ninject 2.0. However, XNA is not dependency injection friendly, as certain classes must be instantiated in the constructor of the game class, but must pass the game class to their constructors as well. For example: public MyGame () { this.graph...

Service behaviour in Entity - how to avoid service injection into entity?

Hi, I have an entity structure as follows: IManager: IDeletable { IEnumerable<IFund> Funds {get;} IFailureNotification Delete(); } IFund : IDeletable { IEnumerable<IFundClass> FundClasses IFailureNotification Delete(); } IFundClass: IDeletable, IInvestable { IFailureNotification Delete(); } And I have a service which takes an IDele...

The decorator pattern and @Inject

When using Spring's based XML configuration, it's easy to decorate multiple implementations of the same interface and specify the order. For instance, a logging service wraps a transactional service which wraps the actual service. How can I achieve the same using the javax.inject annotations? ...

Making Custom IoC - How to Implement DI That Has Scope?

I'm writing an IoC container for my own learning/growth. Normally I would write something like the following: using(DisposableObject dispObj = new DisposableObject()) { UserRepository users = new UserRepository(dispObj); // Do stuff with user. } Would turn to: using(IDisposableObject dispObj = Container.Resolve<IDisposableOb...

Dependency injection and mock framework for .net

I am trying to set up expectations on methods of a mocked object in Moq. At the same time, using Ninject, I try to have the kernel return my set up mock whenever a caller wants the corresponding interface. For more clarity, here's some pseudocode Class Car { Void buildChassis() { Engine = ObjectBuilder.get<Iengine>() ...

How does Java ServiceLoader work during development time? (Unit test before building JARs?)

Java's ServiceLoader needs those entries to be present inside the JAR file. Is there a way to programatically add those service entries at runtime time for unit testing, when inside the IDE? Especially when the JARs are not built yet. ...

C# Dependency Injection problem

I have 3 classes: A, B, and C All of these classes implement an interface IMyInterface I would like the interface to be defined like so: internal IMyInterface<E> where E: class { E returnData(); } So that it can return data of type E. The type "E" will be a POCO object created with the Entity Framework v4. In a separate class I...

Remove Dependency on IoC Container

After reading more and more about IoC containers, I read this post about not having IoC.Resolve() etc in your code. I'm really curious to know then, how can I remove the dependency on the container? I'll want to write code like the following: public void Action() { using(IDataContext dc = IoC.Resolve<IDataContext>()) { ...

C# Dependency Injection Cast Issue

I have several classes that implement the same interface. interface IBidManager<E> where E : IEntity public class BidManager : IBidManager<EntityObject1> public class BidManager2 : IBidManager<EntityObject2> public class BidManager3 : IBidManager<EntityObject3> In my business manager class I have: public class BusinessManager : Mana...

How to make a strongly typed reference to a namespace without picking an arbitrary type in it

I'm working on a project in which I use Depency Injection. When registering a set of interfaces and classes, I need to point out the namespaces at which those interfaces and classes are located. I don't like providing string constants though, mainly because it hurts refactorability. I don't like taking one of the interfaces/classes and ...

How to register types that implement a specific interface on a ninject container

How can I register on a ninject container all the types that implement a specific interface? It's worth saying that I'm using Webforms so IBuildManager is not available. I'd like to achieve something I saw on a MVC application with unity which goes like this: private static void RegisterRepositories(IBuildManager buildManager, IUnityCon...

spring IOC, can I drop a .jar that adheres to an interface and modify the app-config.xml?

Say I have a database layer, with DTO's for each table, and a factory that returns the DTO's for each table. As long as I build to interfaces, I can re-implement the db layer and then just change my app-config.xml to use another implementation. Now, is it possible for me to have this new implementation in another .jar file? The goal...

Castle Windsor Transient Lifestyle Not Activating

Hi, I'm having a problem with a component in my container with a Transient lifestyle. The first time I call resolve, I hit the constructor of the implementation type as expected. However, the second time I call resolve, a new instance is not constructed. Rather, the existing instance is reused. I don't think this should happen, since ...

Can you use dependency injection everywhere a singleton is needed?

This may sound like a stupid question, but can DI be used everywhere where a Singleton is needed? Or are there use cases where a Singleton makes more sense? A professor of mine said that there a few but valid cases where a Singleton is "good enough" but I'm somehow not happy with that :-/. ...

Simple Factory method using Structuremap

Hello, I'm trying to recreate the following logic using Structuremap.IContainer: public IReplyParams CreateParams(Interface @interface, string response) { switch (@interface) { case Interface.InOtherAssetsReply: return new ReplyNonFinancialAssetsParams(response, infoOrderDataProvider); case Interface...

StructureMap Instance Scope in Web Application

I am initialising the StructureMap DI container at web application start-up with a concrete type for an interface. The concrete type is used to provide a reentrant method to the domain as part of a strategy pattern. How should I decide which InstanceScope to cache by? ...

System.NotSupportedException: Parent does not have a default constructor. The default constructor must be explicitly defined.

I'm working with Ninject 2.0.2 Ninject.Extensions.Interception with DynamicProxy2 Castle.Core Castle.DynamicProxy2 The injection worked fine. Then I inserted the wcf between controller and bl layers. For interaction between the layers I've used the Ninject.Extensions.Interception of Ian Davis. I've used the DynamicProxy2 for creatin...