BuildUp 无法解决依赖关系

发布于 2025-01-06 17:14:02 字数 1985 浏览 1 评论 0原文

我正在尝试将 Dependency 属性与 RegisterInstance 一起使用。我读过如果我使用 RegisterInstance 我必须在实例上使用 BuildUp 方法。不幸的是它在我的代码中不起作用。它不解析 Alarm 属性。当我在没有 RegisterInstance 的情况下使用代码时,一切正常,但我需要使用 RegisterInstance。我错过了什么吗?

编辑

var container = new UnityContainer();
container.RegisterType<IAlarm, Alarm>();

ICar car = new Car();
container.RegisterInstance<ICar>(car);
var carFromContainer = container.BuildUp<ICar>(container.Resolve<ICar>());
var alarm = carFromContainer.Alarm;

public interface ICar
{
    IAlarm Alarm { get; set; }
}

public class Car : ICar
{
    private IAlarm _alarm;

    [Dependency]
    public IAlarm Alarm
    {
        get { return _alarm; }
        set { _alarm = value; }
    }
}

public interface IAlarm
{
    void Launch();
}

public class Alarm : IAlarm
{
    public void Launch()
    {
        Console.WriteLine("Alarm !");
    }
}

旧示例

var container = new UnityContainer();
container.RegisterType<IAlarm, Alarm>();
ICar car = new Car();
container.RegisterInstance<ICar>(car);

//container.RegisterType<ICar, Car>();

var client = new Client(container);
var alarm = client.Car.Alarm;
client.Car.Alarm.Launch();

public class Client
{
    public ICar Car { get; set; }
    public Client(UnityContainer container)
    {
        //Car = container.Resolve<ICar>();
        Car = container.BuildUp<ICar>(container.Resolve<ICar>());
    }
}


public interface ICar
{
    IAlarm Alarm { get; set; }
}

public class Car : ICar
{
    private IAlarm _alarm;

    [Dependency]
    public IAlarm Alarm
    {
        get { return _alarm; }
        set { _alarm = value; }
    }
}

public interface IAlarm
{
    void Launch();
}

public class Alarm : IAlarm
{
    public void Launch()
    {
        Console.WriteLine("Alarm !");
    }
}

I'm trying to use Dependency attribute with using RegisterInstance. I've read that If I use RegisterInstance I must use BuildUp method on a instance. Unfortunately it doesn't work in my code. It doesn't resolve Alarm property. When I use my code without RegisterInstance everything is ok, but I need use RegisterInstance. Do I miss something?

EDIT

var container = new UnityContainer();
container.RegisterType<IAlarm, Alarm>();

ICar car = new Car();
container.RegisterInstance<ICar>(car);
var carFromContainer = container.BuildUp<ICar>(container.Resolve<ICar>());
var alarm = carFromContainer.Alarm;

public interface ICar
{
    IAlarm Alarm { get; set; }
}

public class Car : ICar
{
    private IAlarm _alarm;

    [Dependency]
    public IAlarm Alarm
    {
        get { return _alarm; }
        set { _alarm = value; }
    }
}

public interface IAlarm
{
    void Launch();
}

public class Alarm : IAlarm
{
    public void Launch()
    {
        Console.WriteLine("Alarm !");
    }
}

Old example

var container = new UnityContainer();
container.RegisterType<IAlarm, Alarm>();
ICar car = new Car();
container.RegisterInstance<ICar>(car);

//container.RegisterType<ICar, Car>();

var client = new Client(container);
var alarm = client.Car.Alarm;
client.Car.Alarm.Launch();

public class Client
{
    public ICar Car { get; set; }
    public Client(UnityContainer container)
    {
        //Car = container.Resolve<ICar>();
        Car = container.BuildUp<ICar>(container.Resolve<ICar>());
    }
}


public interface ICar
{
    IAlarm Alarm { get; set; }
}

public class Car : ICar
{
    private IAlarm _alarm;

    [Dependency]
    public IAlarm Alarm
    {
        get { return _alarm; }
        set { _alarm = value; }
    }
}

public interface IAlarm
{
    void Launch();
}

public class Alarm : IAlarm
{
    public void Launch()
    {
        Console.WriteLine("Alarm !");
    }
}

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

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

发布评论

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

评论(1

残花月 2025-01-13 17:14:02

我不明白你的示例代码。您自己新建Car并将其注册到容器中。然后,您告诉容器将 IAlarm 映射到实现类 Alarm。然后,您将容器注入到您的 Client 中,这是强烈不鼓励的做法。决不应该在应用程序的组合根之外引用 DI 容器。然后你(ab)使用你的容器作为ServiceLocator(这被认为是反-现代应用程序架构中的模式)来解析 ICar 并将一些值注入到属性中。

我建议您重新考虑您的设计,而不是寻找您面临的技术问题的解决方案。


更新

更改ICar的接口定义并将DependencyAttribute放在那里并且它可以工作。

但我也不推荐这种方法。 DI 属性不应污染您的代码

I don't understand your sample code. You new up the Car yourself and register it with the container. Then you tell the container to map IAlarm to the implementing class Alarm. Then you inject the container into your Client which is something that is strongly disencouraged. DI containers should never be referenced outside the Composition root of your application. And then you (ab)use your container as a ServiceLocator (which is considered an anti-pattern in modern application architecture) to resolve ICar and inject some value into a property.

I would recommend you re-think your design instead of looking for a solution to the technical issue you face.


UPDATE

Change your interface definition for ICar and put the DependencyAttribute there and it works.

But I cannot recommend that approach either. DI attributes should not polute your code.

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