Unity (dependency injection): How to pass in a parameter to the constructor in RegisterType

别来无恙 提交于 2019-12-08 19:49:23

问题


Can anyone help?

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

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

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?

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

Any ideas?


回答1:


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

Example:

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

This will print:

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.




回答2:


here is some more information. The constructor of my class is

    public SecurityService(ISecurityRepository repository)
        : base(repository)
    {

    }

After playing around a little bit, i managed to do the following but this causes me to create instances FIRST ... It seems to work.., but its an alternative.

        // Create unity container my service and repository
        ISecurityRepository securityRepository = new SecurityRepository();
        ISecurityService securityService = new SecurityService(securityRepository);

        container = new UnityContainer();
        container.RegisterInstance<ISecurityRepository>(securityRepository);
        container.RegisterInstance<ISecurityService>(securityService);


来源:https://stackoverflow.com/questions/1483634/unity-dependency-injection-how-to-pass-in-a-parameter-to-the-constructor-in-r

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