Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection (ninject) using strings, anti-pattern?

I have some code that is using ninject to inject dependencies, these dependencies are actual strings. Is this an anti-pattern to inject strings rather than creating a new object for example.

I.e. I wanted to inject Username and Password, would it actually be better to create a small class called credentials with 2 properies of Usernamd and Password and inject this ?

Injecting strings into constructors can be done via

kernel.Bind<IUser>().To<User>()
      .WithConstructorArgument(@"username", configuration.Username)
      .WithConstructorArgument(@"password", configuration.Password);

Is this code smell ?

Any ideas or improvement on what I am doing ?

like image 833
Martin Avatar asked Sep 11 '13 10:09

Martin


People also ask

Is dependency injection an anti pattern?

How come we (need to) use a runtime pattern for things we know at compile-time? if we properly registered the implementation for the respective interface. To fully use this pattern obviously MyClass has to be created as well by the DI system.

What is ninject dependency injection?

Ninject is a lightweight dependency injection framework for . NET applications. It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner.

What is dependency injection which pattern supports it?

Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

How do I inject a dependency in ninject?

Dependency Injection Using Ninject in .NET 1 Download Ninject. 2 Then we will load the Kernel. 3 Get the instance of the specific service that we want to inject. 4 Then inject the dependency. 5 Finally calling the method of DL.

What is ninject and how to use it?

Ninject is open-source and hosted on GitHub. Dependency Injection is defined as a design pattern that allows removing hard-coded dependencies from an application. How to remove Dependency Injection One way to remove a dependency is to use a framework such as Unity, Ninject, Windsor Castle, and many more that will help to remove the dependency.

What is the dependency injection anti-pattern?

It is a form of the Control Freak anti-pattern. Control Freak may initially seem a valuable approach to adding DI into legacy applications, but when applying the Dependency Injection pattern from the beginning such default constructor is redundant. Optional dependencies

How does ninject resolve the entire chain of dependencies?

The code is creating a Ninject Kernel that resolves our entire chain of dependencies. We tell Ninject to load the bindings from the executing assembly. This is the most common scenario. In this case, your Bindings class should live in one of the assemblies included in your executing project.


2 Answers

I would prefer to use ToMethod() here:

kernel.Bind<IUser>()
      .ToMethod(ctx => new User(configuration.Username, configuration.Password));

If the User constructor has other dependencies, then I would defer to @jgauffin's answer.

You could still use ToMethod() with Kernel:

kernel.Bind<IUser>()
      .ToMethod(ctx => new User(configuration.Username,
                                configuration.Password,
                                ctx.Kernel.Get<Foo>()));
like image 195
Ed Chapel Avatar answered Oct 20 '22 19:10

Ed Chapel


Is this code smell ?

Yes. Either create a ConfigurationRepository or create a factory/builder (two different design patterns) which creates the different services and then register that factory/builder in the container instead.

I got an issue with this code too:

kernel.Bind<IUser>().To<User>()
      .WithConstructorArgument(@"username", configuration.Username)
      .WithConstructorArgument(@"password", configuration.Password);

A IoC container is primarly not used to create domain entities but to create services/repositories/controllers etc. i.e. to create the objects which control the flow in your application.

like image 29
jgauffin Avatar answered Oct 20 '22 21:10

jgauffin