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
16
votes
1 answer

Unity DI on a Windows Service, Is possible?

I am developing a Windows Service to do some periodical operations, can I use Unity to inject my classes from another library there? I want to use with the [Dependency] attribute on my services, registering the components on the entry point of the…
juan25d
  • 378
  • 1
  • 4
  • 17
16
votes
3 answers

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…
JP Richardson
  • 38,609
  • 36
  • 119
  • 151
16
votes
5 answers

How to use Repository Interface that uses Generics with Dependency Injection?

I am attempting to use the following Generic Repository Interface for DI and constructor injection: public interface IRepository : IDisposable where TEntity : class The problem is in order to define an instance of the Interface, I must…
atconway
  • 20,624
  • 30
  • 159
  • 229
16
votes
3 answers

Unity - Inject different classes for the same interface

I have one interface: IFoo Two classes implementing that interface: FooOne and FooTwo And two classes ClassOne and ClassTwo receiving an IFoo parameter in the constructor. How I configure unity so ClassOne receives a FooOne instance and ClassTwo…
pomber
  • 23,132
  • 10
  • 81
  • 94
16
votes
2 answers

Getting Started with Unity Framework

Could anyone recommend some good resources for getting started with the Unity framework? I've downloaded the source from Codeplex and got it to compile. So now I've got a set of compiled binaries, where do I go next? I understand the principles of…
Steve
  • 2,073
  • 4
  • 27
  • 39
15
votes
5 answers

How to Decouple IoC Framework Implementation

I've been learning IoC, Dependency Injection etc. and enjoying the process. The benefits of decoupling and programming to interfaces are, to me, a no-brainer. However, I really don't like binding myself to a specific framework like Unity or Autofac…
15
votes
2 answers

How can I pass in constructor arguments when I register a type in Unity?

I have the following type being registered in Unity: container.RegisterType, AzureTable>(); The definition and constructors for AzureTable are as follows: public class AzureTable : AzureTableBase, IInitializer…
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
15
votes
1 answer

What is the main difference between ContainerControlledLifetimeManager and HierarchicalLifetimeManager?

What is the general difference between ContainerControlledLifetimeManager and HierarchicalLifetimeManager? I know that ContainerControlledLifetimeManager represent singleton that is DI container will create a new instance for each type which needed…
15
votes
1 answer

Entity Framework using Repository Pattern, Unit of Work and Unity

Using a combination provided from this example and this implementation I am trying to create a solution that decouples the UnitOfWork class from the individual repositories, as they violate the Open-Closed Principle, since every time you added a new…
15
votes
2 answers

Unity Register For One Interface Multiple Object and Tell Unity Where to Inject them

Hi I have been having trouble trying to tell Unity that for an Interface if it has multiple implementations , I want it to inject them in different classes.Here is what I mean: Let's say I have an interface IProductCatalogService and two…
aleczandru
  • 5,319
  • 15
  • 62
  • 112
14
votes
3 answers

UnityContainer.Resolve or ServiceLocator.GetInstance?

It could seem a stupid question because in my code everything is working, but I've registered a singleton this way with my Unity container _ambientContainer: _ambientContainer.RegisterType(new…
14
votes
4 answers

Way to fill collection with Unity

I have two example classes class ClassToResolve { private List _coll; public ClassToResolve(List coll) { _coll = coll; } } class CollectionItem { //... } and I need to resolve…
14
votes
3 answers

New Prism Project - Use MEF or Unity?

I'm starting a new personal Prism 4 project. The Reference Implementation currently uses Unity. I'd like to know if I should use MEF instead, or just keep to Unity. I know a few discussions have mentioned that these two are different, and they do…
Jonas Arcangel
  • 2,085
  • 11
  • 55
  • 85
14
votes
7 answers

Unity application block 2.0 - The given assembly name or codebase was invalid

Interfaces (In the assembly named "Interfaces". In project :- Interfaces) namespace Interfaces { public interface IDoSomeWork1 { string DoSomeWork1(); } } namespace Interfaces { public interface IDoSomeWork2 { …
Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134
14
votes
3 answers

Unity Dependency Injection for WCF services

Can someone direct me to a good example of Unity Dependency Injection for WCF services? Any blog or msdn article will also help.
SVI
  • 1,631
  • 3
  • 22
  • 30