dependency-injection

Castle Windsor equivalent of StructureMap ObjectFactory.With<>().GetInstance<>()

With StructureMap one can do a resolution and force the container to use specific dependency instance provided at the time of resolution like so: ObjectFactory.With<ISomeDependency>(someDepedencyInstance).GetInstance<IServiceType>() Provided instance will be used in whole resolution chain, that is not only as a direct dependency of IS...

Using reference as class members for dependencies

Hi I am going back to C++ after spending some time in memory-managed languages, and I'm suddently kinda lost as to what is the best way to implement dependency injection. (I am completely sold to DI because I found it to be the simplest way to make test-driven design very easy). Now, browsing SO and google got me quite a number of opi...

Which Dependency Injection mechanism in Java EE 6 should I use?

Java EE 6 offers (at least) two Dependency Injection mechanisms: DI annoations from Java EE 5 like @EJB, @PersistenceContext, @Resource ... and the new JSR 330. Can I replace the "old" DI annotations with the more general JSR 330 annotations? Are there any benefits or drawbacks of the one or other approach? Which one would you use and w...

Creation Of Object That Uses Inversion Of Control

I'm creating a CSV reader (yes I know about Fast CSV Reader and FileHelpers). The CsvReader class uses the CsvParser class to parse the CSV file. I want to make the CsvReader class unit testable, so I want to be able to externally set the CsvParser class that is used (also, so you can create your own implementation). I also, don't want t...

Declarative Vs Bluprint Services in OSGi

What is the difference between Declarative and Bluprint Services in OSGi? as both are aim to achieve Dependency injection in osgi. Is blueprint services is alternative to declarative services? or bluprint services fills the limitations (if any) of declarative services? ...

Why are there 2 constructors in the Default AccountController provided by the MVC ?

Hello, here's the default AccountController.cs that's generated by the framework. public class AccountController : Controller { public IFormsAuthentication FormsAuth { get; private set; } public IMembershipService MembershipService { get; private set; } public AccountController() : this(null, null) { } ...

What are Dependency Injection & Spring Framework about ?

Possible Duplicates: What is dependency injection? What exactly is Spring for? I want to know What is Spring Framework? Why and when should one use it in Java Enterprise development? The answer would be "A dependency injection framework". All right, what advantages do we have when using dependency injection frameworks? The...

Is mixing constructor-based and setter-based injections a bad thing?

I have a class for products import from CSV file operation which requires about 7 parameters. This is an info which is definitely needed for importer. All of this parameters have the same life time. In the end we must have an Immutable Object. I was too scared to list all of them in constructor because of its affect to readability and ...

spring wiring, singleton versus prototype

just reading up on spring, and when using DI in spring, if you set the bean to be a singleton (default), then a single instance of the class will be 'dispensed', while prototype forces a new instance each time. What are the ramifications of having the same instance dispensed by the container each time? does that mean there will be share...

Constructor dependency injection with Servlet 3.0?

Since Servlet 3.0 it is possible to register Servlet instances programmatically with javax.servlet.ServletContext#addServlet. This class has also a createServlet method which analyses some annotations and performs dependency injection. I wonder if I need this method if I don't need the annotation processing. I'd like to have a servlet wi...

Does dependency injection increase my risk of doing something foolish?

I'm trying to embrace widespread dependency injection/IoC. As I read more and more about the benefits I can certainly appreciate them, however I am concerned that in some cases that embracing the dependency injection pattern might lead me to create flexibility at the expense of being able to limit risk by encapsulating controls on what ...

DI Framework: how to avoid continually passing injected depencies up the chain, and without using a service locator (specifically with Ninject).

I need a little more help to "get" how a DI framework like Ninject moves past the basics. Take the Ninject sample: class Samurai { private IWeapon _weapon; [Inject] public Samurai(IWeapon weapon) { _weapon = weapon; } public void Attack(string target) { _weapon.Hit(target); } } Without a DI framewo...

Why getting a 202 in two equal setup structuremap code paths

In the C# language, using StructureMap 2.5.4, targeting .NET Framework 3.5 libraries. I've taken the step to support multiple Profiles in a structure map DI setup, using ServiceLocator model with Bootstrapper activation. First setup was loading default registry, using the scanner. Now I like to determine runtime what Registry configur...

Spring.NET & Immediacy CMS (or how to inject to server side controls without using PageHandlerFactory for pages)

Is there any way to inject dependencies into an Immediacy CMS control using Spring.NET, ideally without having to use to ContextRegistry when initialising the control? Update, with my own answer The issue here is that Immediacy already has a handler defined in web.config that deals with all aspx pages, & so it's not possible add an ent...

Inject dependency into static field

I want to make template engine available in servlets. The concrete template engine implementation should be changeable behind the TemplateEngine interface. With usual dependency injection it could look like this: public abstract class BaseServlet extends HttpServlet { private TemplateEngine templateEngine; public void setTempl...

MEF Constructor Injection

I'm trying to figure MEF's Constructor Injection attribute. I have no idea how i tell it to load the parameters to the constructor. This is property i'm trying to load [ImportMany(typeof(BUsers))] public IEnumerable<BUsers> LoadBUsers { get; set; } Here is the code that i use to import the assemblies. try { va...

How is threadsafty guranteed with @PersistenceContext?

According to many examples it is possible to inject an EntityManager into @Stateless or @Singleton EJBs like this: @Stateless // or @Singleton public class MyRepository { @PersistenceContext private EntityManager em; ... } The EJB 3.1 Spec says that dependency injection is only performed at construction time, so that all call...

Constructor Injection in C#/Unity?

I'm using C# with Microsoft's Unity framework. I'm not quite sure how to solve this problem. It probably has something to do with my lack of understanding DI with Unity. My problem can be summed up using the following example code: class Train(Person p) { ... } class Bus(Person p) { ... } class Person(string name) { ... } Person dad...

Inject a EJB into a JSF converter with JEE6

I have a stateless EJB that acceses my database. I need this bean in a JSF 2 converter to retreive an entity object from the String value parameter. I'm using JEE6 with Glassfish V3. @EJB annotation does not work and gets a NPE, because it's in the faces context and it has not access to the EJB context. My question is: Is it still pos...

Inject into private, package or public field or provide a setter?

I see many Java examples using dependency injection with private fields without a public setter like this: public SomeClass { @Inject private SomeResource resource; } But that is a bad idea when the injection should be performed manually for example in unit tests. There are several possibilities to solve this: add a public sett...