Questions tagged [unity-container]

The Unity Application Block (i.e. Unity) is a lightweight, extensible dependency injection container for .NET with support for interception. DO NOT USE THIS TAG TO REFER TO THE UNITY GAME ENGINE! Use unity-game-engine instead (https://stackoverflow.com/tags/unity-game-engine/info)!

The Unity Application Block (i.e. Unity Container) is a lightweight, extensible dependency injection container for .NET. It also supports interception.

Unity targets both .NET CLR and Silverlight.

Installing Unity can most easily be done using its NuGet package:

Install-Package Unity

More information at:

Hello world in c#

interface IMessageWriter 
{
    void WriteMessage(string message);
}

class ConsoleWriter : IMessageWriter 
{
    public void WriteMessage(string message) 
    { 
        Console.WriteLine(message); 
    }
}

class HelloWorldService 
{
    private readonly IMessageWriter _writer;

    public HelloWorldService(IMessageWriter writer) 
    {
       _writer = writer;
    }

    public void Go() 
    {
       _writer.WriteMessage("Hello World!");
    }
}

using (var container = new UnityContainer()) 
{        
    container.RegisterType<IMessageWriter, ConsoleWriter>();

    var helloWorldService = container.Resolve<HelloWorldService>();

    helloWorldService.Go();    
}
3920 questions
38
votes
2 answers

How to inject dependencies into the global.asax.cs

How do I inject dependencies into the global.asax.cs, i.e. the MvcApplication class? Having previously used the Service Locator (anti-)pattern for dependency injection, I am trying to follow best practice advice in my latest MVC application by using…
magritte
  • 7,396
  • 10
  • 59
  • 79
37
votes
5 answers

Microsoft Unity. How to specify a certain parameter in constructor?

I'm using Microsoft Unity. I have an interface ICustomerService and its implementation CustomerService. I can register them for the Unity container using the following code: container.RegisterType(new…
Andrei M
  • 3,429
  • 4
  • 28
  • 35
37
votes
2 answers

How to registerType with a PARAMETER constructor?

How do I registertype with the container where the type doesn't have NO PARAMETER constructor. In fact my constructor accepts a string, and I normally pass in a string that represents a Path. So when I do resolve it automatically creates the new…
Martin
  • 23,844
  • 55
  • 201
  • 327
37
votes
3 answers

Dependency Injection in attributes

I am trying to inject a dependency into a custom AuthorizeAttribute as follows: public class UserCanAccessArea : AuthorizeAttribute { readonly IPermissionService permissionService; public UserCanAccessArea() : …
James
  • 1,979
  • 5
  • 24
  • 52
37
votes
3 answers

Base controller constructor injection in ASP.NET MVC with Unity

I have a base controller in my MVC 5 project which implements some shared functionality. This functionality requires some dependencies. I am using Unity 3 to inject these implementations into my controllers, a pattern which has worked fine until I…
36
votes
2 answers

How does Unity.Resolve know which constructor to use?

Given a class with several constructors - how can I tell Resolve which constructor to use? Consider the following example class: public class Foo { public Foo() { } public Foo(IBar bar) { Bar = bar; } public Foo(string…
stiank81
  • 25,418
  • 43
  • 131
  • 202
36
votes
1 answer

Proper way to Mock repository objects for unit tests using Moq and Unity

At my job we are using Moq for mocking and Unity for an IOC container. I am fairly new to this and do not have many resources at work to help me out with determining the best practices I should use. Right now, I have a group of repository interfaces…
Rob Packwood
  • 3,698
  • 4
  • 32
  • 48
35
votes
1 answer

Unity: Replace registered type with another type at runtime

We have a scenario where the user can choose between different hardware at runtime. In the background we have several different hardware classes which all implement an IHardware interface. We would like to use Unity to register the currently…
gehho
  • 9,049
  • 3
  • 45
  • 59
35
votes
7 answers

Web Api Start up Exceptions with IDependencyResolver implementation

I am developing a Web Api and I decided to use custom DependencyResolver. I refer this [Dependency Injection for Web API Controllers] article. Everything is working well so far in the terms of dependency injection into controllers. Code snippet of…
35
votes
4 answers

MVVM, Unity, Prism, MEF, Caliburn - What should I use?

Please help - I'm getting lost! I'm writing a small desktop application which has some controls and some screen. This should later be integrated with a small web site, also having some screens. The idea is to let the user edit videos and select…
Avi
  • 15,696
  • 9
  • 39
  • 54
33
votes
2 answers

How to create objects using a static factory method?

I know Unity can be configured to use a class' constructor to create an instance of a class (like below) but that's not what I want. container.RegisterType(); I would like to configure Unity to use a…
burnt1ce
  • 14,387
  • 33
  • 102
  • 162
32
votes
4 answers

Is MEF a dependency injection framework?

The recently announced managed extensibility framework (MEF) of .NET 4.0 - is it a dependency injection framework? Will Microsoft Unity from Patterns and Practices be obsolete in 4.0 ? How does MEF compare to a framework like Unity?
bitbonk
  • 48,890
  • 37
  • 186
  • 278
31
votes
5 answers

Unity Singleton Code

I'm new to Unity and am trying to write some Unity logic which initialises and register/resolves a singleton instance of the Email object so that it can be used across several other objects, one example below being OperationEntity. So when it's…
Bern
  • 7,808
  • 5
  • 37
  • 47
31
votes
2 answers

What are the InjectionMembers in RegisterType() calls for?

I've been working with Microsoft's Unity IOC container. There are a bunch of overloads for the RegisterType() method all looking similar to IUnityContainer RegisterType(Type t, params InjectionMember[] injectionMembers); I'm wondering when the…
Scott Bussinger
  • 2,767
  • 4
  • 28
  • 29
31
votes
4 answers

IoC in class library. Where to bootstrap

I'm using a class library that can be reused by other components. In this class library I'm using unity for dependency injection. For this class library I create a test project. The caller also gets a test project. One thing I'm uncertain about is…
Patrick
  • 2,730
  • 4
  • 33
  • 55