dependency-injection

How do I correctly use Unity to pass a ConnectionString to my repository classes?

I've literally just started using the Unity Application Blocks Dependency Injection library from Microsoft, and I've come unstuck. This is my IoC class that'll handle the instantiation of my concrete classes to their interface types (so I don't have to keep called Resolve on the IoC container each time I want a repository in my controll...

How to collect and inject all beans of a given type in Spring XML configuration

One of the strongest accents of the Spring framework is the Dependency Injection concept. I understand one of the advices behind that is to separate general high-level mechanism from low-level details (as announced by Dependency Inversion Principle). Technically, that boils down to having a bean implementation to know as little as possi...

How do I use constructor dependency injection to supply Models from a collection to their ViewModels?

I'm using constructor dependency injection in my WPF application and I keep running into the following pattern, so would like to get other people's opinion on it and hear about alternative solutions. The goal is to wire up a hierarchy of ViewModels to a similar hierarchy of Models, so that the responsibility for presenting the informati...

Best way to mock out external web services while testing a web application in development

Currently I am working on an application which depends on a lot of external web services. A few of them are authorize.net and chargify. When testing(manual testing) things other than the integration with these web services, I replace these web service dependencies with fake versions of them which don't really do anything. The way I am...

Using StructureMap, when a default concrete type is defined in one registry, can it be redefined in another registry?

In the project I'm working on I have a StructureMap registry for the main web project and another registry for my integration tests. During some of the tests I wire up the web project's registry, so that I can get objects out of the container for testing. In one case I want to be able to replace a default concrete type from the web reg...

Validation without ServiceLocator

Hi, I am getting back again and again to it thinking about the best way to perform validation on POCO objects that need access to some context (ISession in NH, IRepository for example). The only option I still can see is to use Service Locator, so my validation would look like: public User : ICanValidate { public User() {} // We n...

Is it possible to use Dependency Injection/IoC on an ASP.NET MVC FilterAttribute ?

Hi folks, I've got a simple custom FilterAttribute which I use decorate various ActionMethods. eg. [AcceptVerbs(HttpVerbs.Get)] [MyCustomFilter] public ActionResult Bar(...) { ... } Now, I wish to add some logging to this CustomFilter Action .. so being a good boy, I'm using DI/IoC ... and as such wish to use this pattern for my cus...

Grails: Dynamically inject service in domain class

I need to inject a service based on domain property, so far I came up with the following: ApplicationHolder.application.getServiceClass("package.${property}Service").clazz but loading it this way doesn't inject it's dependent services. Am I doing it wrong? ...

How do I set up Array/List dependencies in code with Castle Windsor?

Hi, I have the following classes: class Repository : IRepository class ReadOnlyRepository : Repository abstract class Command abstract CommandImpl : Command { public CommandImpl(Repository repository){} } class Service { public Service (Command[] commands){} } I register them in code as follows: var container = new Contai...

Is this ok in a DI world ?

Hello Stackers :) I have implemented the Unity DI in a project of mine, but I have, what I believe is a simple question. My DataContext: public partial class AuctionDataContext : DataContext { public Table<AuctionItem> AuctionItems; ... } Some code for inserting an AuctionItem in the database. Please notice how I cast the inte...

Dynamic domain methods missing from grails service when injected into java service in grails app.

I had the idea that I would write my GroovyDao as a grails service. Next I would write a MyJavaService in java and locate it in the java sources dir in my grails app. MyJavaService contains a instance reference to groovyDao for spring injection. I would wire up in resources.groovy the MyJavaService with a groovyDao = ref("GroovyDao")....

Passing constructor arguments when using StructureMap

Hello, I'm using StructureMap for my DI. Imagine I have a class that takes 1 argument like: public class ProductProvider : IProductProvider { public ProductProvider(string connectionString) { .... } } I need to specify the "connectionString at run-time when I get an instance of IProductProvider. I have conf...

Passing ASP.NET User by Dependency Injection

In my web application I have various components that need to access the currently authenticated user (HttpContext.User). There are two obvious ways a component can access this: 1) Accessing getting the User from HttpContext.Current 2) Passing the user around in constructors Is not ideal because it makes testing difficult and ties ap...

Castle Windsor: Inject NameValueCollection vs. Dictionary

I've already done many configs where dictionaries are passed into services in the <parameters> block. But what I find myself needing right now is to build a NameValueCollection (allowing multiple entries with the same key) or a Collection of KeyValuePair objects. The reason for this is im not using this dictionary to look up b when giv...

Castle windsor registration

interface IUserService class LocalUserService : IUserService class RemoteUserService : IUserService interface IUserRepository class UserRepository : IUserRepository If I have the following interfaces and classes, where the IUserService classes have a dependency on IUserRepository. I can register these components by doing something lik...

How do I obtain a new stateful session bean in a servlet thread?

I'm experimenting with EJB3 I would like to inject a stateful session bean into a servlet, so that each user that hits the servlet would obtain a new bean. Obviously, I can't let the bean be an instance variable for the servlet, as that will be shared. And apparantly injecting local variables isn't allowed. I can use the new operator...

How do I configure StructureMap to use a generic repository?

I have an interface IGenericRepository<TEntity> where TEntity : IEntity and an implementation GenericRepository<TEntity> where TEntity : Entity. I'm trying to inject a specific IGenericRepository<Section> into a class using StructureMap: ObjectFactory.Initialize(x => { x.For(typeof(IGenericRepository<>)).Use(typ...

NHibernate does not update entity when repository is passed by constructor

Hi everybody, I am developing with NHibernate for the first time in conjunction with ASP.NET MVC and StructureMap. The CodeCampServer serves as a great example for me. I really like the different concepts which were implemented there and I can learn a lot from it. In my controllers I use Constructur Dependency Injection to get an insta...

DI: Injecting ActionFilterAttribute implementation (ASP.NET MVC)

I was wondering if it is possible to inject a particular ActionFilterAttribute implementation using a IoC container. For example, imagine you create a TransactionAttribute class [Transaction] You use this to decorate action which should be wrapped in a transaction in the persistence layer. But implementation details of the attribute w...

How to override the behavior of Spring @Autowired

Hi a little background: I am Using Spring 2.5, and specifically spring IOC and annotations. I am using @Autowired in my code (the Autowiring is done by type) and use @Component for exposing Classes to the Automatic wiring. The situation described bellow arose while i tried to test my code. now to the problem: Note: i use a differe...