constructor-injection

Constructor Injection, design for testability

I have this code (you probably can ignore that it is Swing code), but I usually end up with too many arguments in my constructor. Should I use model bean class and then pass that object in the constructor? public BrowserFrame(final JTextField url, final JTextArea response, final JTextField command, final JButton actionButton) { th...

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...

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...

Testable Java Code: using model beans with a constructor

According to Misko Hevery that has a testability blog. Developers should avoid 'holder', 'context', and 'kitchen sink' objects (these take all sorts of other objects and are a grab bag of collaborators). Pass in the specific object you need as a parameter, instead of a holder of that object. In the example blow, is this code smell? Sh...

Spring: Inject static member (System.in) via constructor

I wrote some sort of console client for a simple application. To be more flexible, I thought it would be nice to only depend on java.io.Input-/OutputStream, instead of accessing System.in/out directly. I renamed the class ConsoleClient to StreamClient, added setters and made sure that the instance fields are used instead of System.in/ou...

How to instantiate objects of classes that have dependencies injected?

Let's say I have some class with dependency injected: public class SomeBusinessCaller { ILogger logger; public SomeBusinessCaller(ILogger logger) { this.logger = logger; } } My question is, how do I instantiate an object of that class? Let's say I have an implementation for this, called AppLogger. After I say ObjectFa...

Spring constructor injection of SLF4J logger - how to get injection target class?

I'm trying to use Spring to inject a SLF4J logger into a class like so: @Component public class Example { private final Logger logger; @Autowired public Example(final Logger logger) { this.logger = logger; } } I've found the FactoryBean class, which I've implemented. But the problem is that I cannot get any information a...

JAXB constructor injection

I would like to know how I can make the JAXB compiler make certain elements in my XML schema be declared as final in the java class definition and I would also like to be able to control the different constructors, as in I would like a constructor that could create the object with a full list of parameters contained in the class as well ...

Using [ImportingConstructor] to import calling object into constructor parameter using MEF

I am in the process of converting some of my code to MEF from a proprietary system that sort of does the same thing as MEF, and I have a question about how I would accomplish the following problem that I recently ran into. I have a typical entity object that looks something like this: public class Account { [Import] public IAc...

GlassFish, CDI and Constructor Injection

Is constructor injection supported in GlassFish 3.1's implementation of CDI for managed beans? I have a @Singleton EJB into which I want to inject another managed bean (contained in the same EJB module) using constructor injection. Field injection does work. But with constructor injection I get a NullPointerException from AbstractSinglet...

Unity Application Block - Constructor injection in configuration file

How can I specify that constructor with no parameter should be used while creating the object? I know how to do it for the parameterized one but cannot find any help for the parameter less constructor. I know how to do this through code but need solution for doing it through configuration. ...

Ninject 2.0: Passing different parameters depending on implementation

Hi everybody, I have just started to work with Ninject 2.0 with ASP.NET MVC 2. So, I have an interface IMongoRepository and class MongoRepository. MongoRepository receives a parameter string collection. Depending on the collection I want to use, I pass in a different value in parameter for MongoRepository. I hope I am phrasing this co...

How to use Ninject in constructor injection of a type in an external assembly

I am loading a type from an external assembly and want to create an instance of the type. However, this type/class is setup for constructor injection by objects currently being managed/bound by Ninject. How can I use Ninject to create an instance of this type and inject any constructor dependencies? Below is how I get this type. Assem...

DI, Constructor Injection, Modules, design patterns

Hello, I'm facing some architectural problems on the project I'm involved in. The project is an ASP.NET MVC 2 application that relies on DI and especially on constructor injection with Unity. The application is divided into several modules (each module is a set of assembies) that exposes services to other modules. Those services are reg...

Ninject, Providers and Activator.CreateInstance

I'm fairly new to Ninject, but I have successfully managed to use it for DI using a custom provider. The binding is initialised as follows kernel = new StandardKernel(); kernel.Bind<IPatientRecordLocator>().ToProvider<PatientRecordLocatorProvider>(); and in the custom provider I call Activator.CreateInstance like so protected over...