Unity IOC Buildup vs Resolve?

后端 未结 2 1468
抹茶落季
抹茶落季 2021-01-03 22:03

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

相关标签:
2条回答
  • 2021-01-03 22:23

    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
    
    0 讨论(0)
  • 2021-01-03 22:27

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题