dependency-injection

Loose coupling of static stuff

I have a class, ClassA that uses a client I wrote to send text messages, TextClient, to send some text messages via a call to the static method TextClient.Send(string text, string destination) // where destination is a phone number However, I also have a mail client class, MailClient, which sends emails with the same signature: MailC...

How to create annotation-configured beans with an existing instance of @Configuration?

Assume we have a simple @Configuration: @Configuration public class FooBarConfiguration { @Bean public Foo createFoo() { return new FooImpl(); } @Bean @Autowired public Bar createBar(Foo foo) { return new BarImpl(foo); } } This class can be used with AnnotationConfigApplicationContext to p...

Structuremap and references, what is best practices?

I am trying to use structure map for the first time. I have used ioc containers before, but always with xml config. As structure map uses config through code (I know it can be done in xml as well, but most examples are using the config through code) I am running into some issues with references. Let's work with the following example (no...

What are the basic principles of designing under Dependency Injection pattern

hi all, I'm new to the all DI pattern idea and i have some basic design doubts. im using Unity application blocks 2.0 as my DI framwork. To the questions : Say I have an interface for HW devices named IDevice. And some HW listener that receives such IDevice. Now say you have several HW Devices that implement IDevice and Several Liste...

Scala: reconciling type classes with dependency injection

There seems to be a lot of enthusiasm among Scala bloggers lately for the type classes pattern, in which a simple class has functionality added to it by an additional class conforming to some trait or pattern. As a vastly oversimplified example, the simple class: case class Wotsit (value: Int) can be adapted to the Foo trait: trait F...

Does Dependency Injection (DI) rely on Interfaces?

This may seem obvious to most people, but I'm just trying to confirm that Dependency Injection (DI) relies on the use of Interfaces. More specifically, in the case of a class which has a certain Interface as a parameter in its constructor or a certain Interface defined as a property (aka. Setter), the DI framework can hand over an inst...

Swappable data layer using spring ioc

I have DAO's for each table, that all implement an interface. Now I want to be able to (potentially) swap database layer implementations. So my strategy is to create a DaoFactory, and this DaoFactory will be specific to a particular implemenation of my DAO's. So I will have DAO's for hibernate. Then a DaoHibernateFactory that will loo...

Wrapping an API to support dependency injection

Hi, I am interacting with an API that just has static functions, and cannot be opened up and changed. public class WindowsNativeGraphAPI { public static IEnumerable<IGraphData> GetGraphData(); public static bool DeleteGraphData(IGraphData data); } I would like to be able to pass the API into a function or ...

When and where to use dependency injection with Guice?

Hi all, I've recently studied about Guice in a University course, and have seen the Google I/O video about it. In the video, they claim to use it in every Google project, including Wave, etc. I was wondering - is Guice really that ubiquitous? Is it really a must-know-must-use for programmers in Java? Should I always use it over a factor...

How to turn Spring @Autowired required property to false for test?

I've been using the @Required annotation up to now to ensure DI of my beans in a Spring configured application. To enable the annotation, you need to declare a RequiredAnnotationBeanPostProcessor bean in your configuration. In your test configuration you just don't declare it, so that if some beans are not needed you don't have to have...

Automatically set property after component creation with Autofac

Here is the example code: public interface IService<TEntity> { IContext Context { get; set; } //unimportant methods bool Validate(TEntity entity); void Add(TEntity enttity); } public class UsersController : Controller { private IService<User> _service; public MyController(ISe...

Is it possible to name a dependency with StructureMap?

I would like to be able to inject named dependencies into a class using StructureMap if that is at all possible. The main reason I want this right now is for connection string injection. I may be doing this the wrong way, but here's how I've got it (just need to add injection now): psuedo: public class MyServiceClass string conne...

ASP.NET: dependency injection and roles

I have a page using an injected BLL service: a simple service returning a set of objects with a function like this: public IMyService { List<Foo> All(); } There is a default implementation for normal users. Now, i need that users in administrative role can view more objects, with another implementation of the service. Where can i con...

How to Inject ManagedBeans in JSF2.0

Hi folks, I have got Controller-ManagedBeans and Model-ManagedBeans (like MVC-Pattern). Here my Controller: @ManagedBean @RequestScoped public class ProjectController { private ProjectData projectBean; //The Model-ManagedBean (see below)! private IProjectService projectService; //My Service-Layer public ProjectCon...

What is a good analogy to understand IoC and DI?

What is a good analogy to understand IoC and DI? ...

Inversion of Control & Dependency Injection in the .NET Framework

Is there any specific example/instance of DI being applied as an architectural principle or design pattern in the .NET Framework itself? Do any (or many) of the types in the framework/BCL conform to IoC? The type names and a brief illustration/explanation based in C# would be great! This would compund the need for DI infused design...

Can an IOC Container AssemblyLoad Event / Static Injection?

I anticipate having various assemblies that each contain a static class, with a static property, that might need to be "set" the moment the assembly they are in is loaded. This other post has an example of a static class that could benefit from such a procedure, such as when integration or unit tests are applied. I wonder if there is an...

NHibernate constructor injection

I have a domain type, MyDomainType, mapped to a db table using NHibernate. I'd like an instance of MyInjectedType to be injected into the constructor of MyDomainType when each instance is initialised by NHibernate, so that the injected type instance is ready and available for use within MyDomainType instances retrieved from the database....

IoC Container Hurdle for ASP.Net MVC Newb

Hi, I must admit that I'm new to ASP.Net MVC and I'm currently researching all the best practices on how to start my new project. So far I have understood the concepts of the Repository Pattern and Unit of Work and I have gone onto Dependency Injection and Inversion of Control (IoC). I have been looking into this for the last 2 days an...

Ninject dependency injection into ASP.NET MVC controller where repository type is known only at runtime

I've set up DI with Ninject in my ASP.NET MVC application like this Bind<IRepository>().To<XmlDefaultRepository>().WhenInjectedInto(typeof(PageController)).WithConstructorArgument("contentType", ContentType.Page); Bind<IRepository>().To<XmlDefaultRepository>().WhenInjectedInto(typeof(WidgetController)).WithConstr...