Unity IOC 构建与解析?

发布于 2024-08-18 01:28:47 字数 75 浏览 5 评论 0原文

我想知道使用 Unity IOC 时何时使用 buildup,何时使用解析。

我什么时候调用拆卸?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身以后 2024-08-25 01:28:47

当您希望 Unity 容器构建实例(仅在需要时创建一个新实例或预先存在的单例)、注入其依赖项并向您提供对该对象的引用时,请使用 Resolve。

当您已经拥有对象的实例并希望容器仅解析并注入其依赖项时,请使用 BuildUp。

拆解与构建相反。您可以将对象传递给容器的 Teardown 方法来关闭/清理/执行任何您想要的操作。现有的容器行为在拆卸时不执行任何操作,但可以编写扩展来利用这一点。您还可以让您的对象实现 IDisposable,容器将在您的对象自行释放时调用 Dispose()。

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

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
隔岸观火 2024-08-25 01:28:47

根据 Rafa 的说法,很明显,与 Resolve() 不同,BuildUp() 对 ctor 注入没有帮助。一种常见的情况是,当您在外部某处创建对象时,将其作为参数传递并在内部构建它。 (外部和内部与方法的主体相关。)此外,您可能需要调用 Teardown() 来清理对象并将其恢复到作为参数传递之前的状态。但请注意,由于 Unity 内置的 Teardown() 不执行任何操作,因此它是一种适合覆盖的占位符。

一个例子可能是打印机对象。创建一个后,您可以在每次注入不同的页眉/页脚时在许多其他方法中调用它:

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();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文