dependency-injection

How to avoid injecting dependencies into an object so that it can pass them on?

I am interested in applying dependency injection to my current project, which makes use of the MVC pattern. My controllers will call the models and therefore will need to inject the dependencies into the models. To do this, the controller must have the dependencies (such as a database object) in the first place. The controller doesn't ...

problem injecting Sessionscoped bean in Managed bean

I have a Session scoped bean @SessionScoped public class UserData implements Serializable { private String uid; public String getUid() { return uid; } public void setUid(final String uid) { this.uid = uid; } I'm setting a value in a SessionScoped bean in my stateless session bean public void setOperator(final Operator oper...

PHP: Classes to use functions from each other?

Hello I've been into this problem for a while already, and have asked some questions about it here Stackoverflow. I've got some advice, but I just can't understand it. Could someone provide me an example of classes working smoothly together. I have 3 (maybe more) classes: mysql user alerts As I said, could someone provide an exampl...

What's the difference between DI and factory patterns?

I have a class which depends on 3 classes, all 3 of which have other classes they rely on. Currently, I'm using a container class to build up all the required classes, inject them into one another and return the application. The simplified version of the container looks something like this: class Builder { private $_options; p...

Dependency Injection -Colloquial explanation

Recently I was asked to express the DI in colloquial explanation. I answered : 1) I am going to a hotel.I ordered food.The hotel management asks me to clean the plates and clean the tables.So here i am a client,I am responsible for managing the service (Instantiating,executing,disposing).But DI decouples such tasks so the service cons...

Handling dependencies with IoC that change within a single function call

We are trying to figure out how to setup Dependency Injection for situations where service classes can have different dependencies based on how they are used. In our specific case, we have a web app where 95% of the time the connection string is the same for the entire Request (this is a web application), but sometimes it can change. ...

How do I get google guice to inject a custom logger, say a commons-logging or log4j logger

Google guice has a built-in logger binding (http://code.google.com/p/google-guice/wiki/BuiltInBindings). But what if I want to use a commons-logging or log4j logger? Can I get guice to inject a Log created by LogFactory.getLog(CLASS.class) But having the same behavior as in built-in binding: The binding automatically sets the log...

Bad Design? Constructor of composition uses `this`

Example: class MyClass { Composition m_Composition; void MyClass() { m_Composition = new Composition( this ); } } I am interested in using depenency-injection here. So I will have to refactor the constructor to something like: void MyClass( Composition composition ) { m_Composition = composition; } Howe...

C++ and Dependency Injection in unit testing

Suppose I have a C++ class like so: class A { public: A() { } void SetNewB( const B& _b ) { m_B = _b; } private: B m_B; } In order to unit test something like this, I would have to break A's dependency on B. Since class A holds onto an actual object and not a pointer, I would have to ...

Registering NUnit DynamicMock Instances in a UnityContainer

I'm somewhat new to Unity and dependency injection. I'm trying to write a unit test that goes something like this: [Test] public void Test() { UnityContainer container = new UnityContainer(); DynamicMock myMock = new DynamicMock(typeof(IMyInterface)); container.RegisterInstance(typeof(IMyInterface), myMock.MockInstance); //...

How do I manage object disposal when I use IoC?

My case it is Ninject 2. // normal explicit dispose using (var dc = new EFContext) { } But sometimes I need to keep the context longer or between function calls. So I want to control this behavior through IoC scope. // if i use this way. how do i make sure object is disposed. var dc = ninject.Get<IContext>() // i cannot use this ...

Spring @PostConstruct function in a @Repository called multiple times

I have a DAO that I'm trying to inject into a couple different places: @Repository public class FooDAO { @Autowired private HibernateManager sessionFactory; @PostConstruct public void doSomeDatabaseStuff() throws DataAccessException { ... } } And my application-context.xml is a fairly simple context:co...

What is the proper way to access datastore in custom Model Binders?

How should I properly implement data access in my custom model binders? Like in controllers I use IContentRepository and then have it create an instance of its implementing class in constructor. So I have everything ready for incorporating IoC (DI) at a later stage. Now I need something similar in model binder. I need to make some DB ...

IoC library compatible with MonoTouch?

Still getting familiar with the limits of MonoTouch. Is there an IoC/DI library that can be used with MonoTouch. Something like Ninject ideally? ...

How to use Guice in Swing application

I have a Swing application that I would like to convert from spaghetti to using dependency injection with Guice. Using Guice to provide services like configuration and task queues is going great but I'm now starting on the GUI of the app and am unsure of how to proceed. The application is basically a JFrame with a bunch of tabs in a JTa...

Spring 2.5 managed servlets: howto?

Correct me if anything is wrong. As I understand, all Spring functionality, namely DI works when beans are got thru Spring Context, ie getBean() method. Otherwise, none can work, even if my method is marked @Transactional and I will create the owning class with a new operator, no transaction management will be provided. I use Tomca...

How do I write a Guice Provider that doesn't explicitly create objects?

Say I have a ClassWithManyDependencies. I want to write a Guice Provider for this class, in order to create a fresh instance of the class several times in my program (another class will depend on this Provider and use it at several points to create new instances). One way to achieve this is by having the Provider depend on all the depen...

What is the difference between Inversion of Control and Dependency injection in C++?

I've been reading recently about DI and IoC in C++. I am a little confused (even after reading related questions here on SO) and was hoping for some clarification. It seems to me that being familiar with the STL and Boost leads to use of dependency injection quite a bit. For example, let's say I made a function that found the mean of a ...

Is there anything like Unity for simple things that don't require an interface?

Perhaps I'm misapplying Unity, but here goes. I have a couple of applications, both of which load the same plugin assemblies. All assemblies require a library, and I want them to be able to access this library via Unity. However, in order to use Unity, or any other IoC framework, I'd have to write an interface for this library. I wil...

IoC from start to finish

I'm quite sure that IoC is the way to go for my application. There are a ton of articles and even questions here on SO that discuss the different containers. I've read several blogs today with partial examples. I am personally leaning towards starting with the CommonServiceLocator and Unity as two way to solve the same problem -- I ju...