BuildUp 无法解决依赖关系
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白你的示例代码。您自己新建
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 mapIAlarm
to the implementing classAlarm
. Then you inject the container into yourClient
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 resolveICar
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 theDependencyAttribute
there and it works.But I cannot recommend that approach either. DI attributes should not polute your code.