Unity IOC Buildup vs Resolve?

痴心易碎 提交于 2019-11-30 00:37:57

问题


I was wondering when do I use buildup and when do I use resolve, when using the Unity IOC.

And when do I call teardown?

Thanks


回答1:


Resolve is used when you want the Unity container to construct the instance (a new one just when you need it or an pre-existing singleton), inject its dependencies and hand you the reference to the object.

BuildUp is used when you already have an instance of the object and want the container to just resolve and inject its dependencies.

Teardown is the opposite of BuildUp. You can pass your object to the Teardown method of the container to shut down / clean up / whatever you want. The existing container behavior does nothing at Teardown time, but extensions can be written to take advantage of this. You can also make your objects implement IDisposable, and the container will call Dispose() on your object when it is disposed itself.

IMyService service = container.Resolve<MyService>(); // New service or pre-existing singleton

IMyService service = GetMyService(); // Get the instance from some source
container.BuildUp(service); // Inject dependencies
container.Teardown(service); // Clean-up



回答2:


Having what Rafa said it becomes apparent that unlike Resolve() BuildUp() does not help with ctor injection. A common scenario is when you create your object somewhere outside, pass it as a parameter and build it inside. (Outside and inside relate to the body of a method.) In addition you may need to call Teardown() to clean the object and revert it to the state it was before passing as a parameter. Beware however, since Unity's built in Teardown() does nothing, it's kind of a placeholder suitable for overriding.

An exampe might be a printer object. After you create one you call it in a number of other methods each time injecting different header/footer:

public class Decorations
{
    public string Header { get; set; }

    public string Footer { get; set; }
}

public class Printer
{
    internal void Print(string message)
    {
        Console.WriteLine("HEADER: {0}", this.Decorations != null 
            && this.Decorations.Header != null 
                ? this.Decorations.Header 
                : string.Empty);
        Console.WriteLine(message);
        Console.WriteLine("FOOTER: {0}", this.Decorations != null 
            && this.Decorations.Footer != null 
                ? this.Decorations.Footer 
                : string.Empty);
    }

    [Dependency]
    public Decorations Decorations { get; set; }
}

public class ClassA
{
    public void Print(Printer printer, IUnityContainer container)
    {
        container.BuildUp(printer);
        printer.Print("Hello from ClassA");
        container.Teardown(printer);
    }
}

public class Program
{
    private static void Main(string[] args)
    {
        var printer = new Printer();

        var containerA = new UnityContainer();
        containerA.RegisterInstance(new Decorations { 
            Header = "I am HEADER from Decorations #1", 
            Footer = "I am FOOTER from Decorations #1" });
        var containerB = new UnityContainer();
        containerB.RegisterInstance(new Decorations { 
            Header = "--- I am HEADER from Decorations #2 ---",
            Footer = "--- I am FOOTER from Decorations #2 ---" });

        var a = new ClassA();

        a.Print(printer, containerA);
        a.Print(printer, containerB);

        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }
}


来源:https://stackoverflow.com/questions/2046408/unity-ioc-buildup-vs-resolve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!