Skip to content

Instantly share code, notes, and snippets.

@vhenzl
Created January 6, 2014 15:14
Show Gist options
  • Save vhenzl/8284212 to your computer and use it in GitHub Desktop.
Save vhenzl/8284212 to your computer and use it in GitHub Desktop.
Důkaz, že BuildUp u Unity funguje a provede method injection i při konfiguraci InjectionMethod v registraci, nejen přes atributy.
namespace UnityBuildUp
{
public class IMyDependency
{ }
public class MyDependency : IMyDependency
{ }
public class MyService
{
[InjectionMethod]
public void Init1(IMyDependency myDependency1)
{
MyDependency1 = myDependency1;
}
public void Init2(IMyDependency myDependency2)
{
MyDependency2 = myDependency2;
}
public IMyDependency MyDependency1 { get; set; }
public IMyDependency MyDependency2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
container.RegisterType<IMyDependency, MyDependency>();
container.RegisterType<MyService>(new InjectionMethod("Init2", typeof(IMyDependency))); // konfigurace má přednost před atributy, takže [InjectionMethod] se neuplatní
//container.RegisterType<MyService>(); // takto (logicky) funguje pouze atribut
var s1 = container.Resolve<MyService>();
var s2 = new MyService();
container.BuildUp(s2.GetType(), s2);
Test(s1);
Test(s2);
Console.ReadLine();
}
static void Test(MyService service)
{
Console.WriteLine("MyDependency1 != null: {0}", service.MyDependency1 != null);
Console.WriteLine("MyDependency2 != null: {0}", service.MyDependency2 != null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment