统一(依赖注入):如何在一个参数传递到RegisterType构造 [英] Unity (dependency injection): How to pass in a parameter to the constructor in RegisterType

查看:1709
本文介绍了统一(依赖注入):如何在一个参数传递到RegisterType构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮助?

我有一个WPF应用程序(不应该的问题),并在ONSTART我有我的自举的东西..它是这样..

I have a wpf app (shouldn't matter) and in the Onstart i have my bootstrap stuff.. Its like this..

        // Create unity container my service and repository
        container = new UnityContainer()
            .RegisterType<ISecurityRepository, SecurityRepository>()
            .RegisterType<ISecurityService, SecurityService>();



基本上ISecurityService希望我在ISecurityRepository通过,因此上面的失败。

Basically ISecurityService expects me to pass in a ISecurityRepository, hence the above fails.

但我有点糊涂了,我一定要创建一个新的IsecurityRespository,然后将它传递的,这会使对象,不是吗?

But i am little confused, do i have to create a new IsecurityRespository and then pass it in, this defeats the object doesn't it?

反正我说传递到SecurityService的ISecurityRepository从容器,但它尚未建成的?

Is there anyway i say "pass into SecurityService the ISecurityRepository from the container", but it hasn't been built yet?

任何想法?

推荐答案

您不必先创建实例。这一切都只是工作。 ,这是IOC容器的魔术

You don't have to create instances first. It all just works. That's the magic of IoC Containers.

例如:

public interface ISecurityService { }
public interface ISecurityRepository { }

public class SecurityService : ISecurityService
{
    public SecurityService(ISecurityRepository repository)
    {
        Console.WriteLine("SecurityService created");
        Console.WriteLine("Repository is " + repository);
    }

    public override string ToString()
    {
        return "A SecurityService";
    }
}

public class SecurityRepository : ISecurityRepository
{
    public SecurityRepository()
    {
        Console.WriteLine("SecurityRepository created");
    }

    public override string ToString()
    {
        return "A SecurityRepository";
    }
}

public class MyClassThatNeedsSecurity
{
    public MyClassThatNeedsSecurity(ISecurityService security)
    {
        Console.WriteLine("My class has security: " + security);
    }
}

class Program
{
    static void Main()
    {
        using (IUnityContainer container = new UnityContainer())
        {
            container.RegisterType<ISecurityRepository, SecurityRepository>()
                     .RegisterType<ISecurityService, SecurityService>();

            MyClassThatNeedsSecurity myClass =
                container.Resolve<MyClassThatNeedsSecurity>();
        }
    }
}

这会打印:

SecurityRepository created
SecurityService created
Repository is A SecurityRepository
My class has security: A SecurityService

您有多个选项,如创建前的实例(如你在跟进后显示)或延长注入依赖的生命周期,使他们没有重新他们需要每一次。但对于基础的情况下,这将正常工作。

You have a number of options, such as pre-creating your instances (as you showed in your follow-up post) or extending the lifetime of injected dependencies so that they're not recreated every time they're needed. But for the base case, this will work.

这篇关于统一(依赖注入):如何在一个参数传递到RegisterType构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆