IoC:创建 Unity 扩展来覆盖依赖项?

发布于 2024-12-21 21:10:59 字数 472 浏览 2 评论 0原文

通过使用 Microsoft 的 Unity,我能够在解析实例时覆盖依赖项,例如:

var valueObjectThatOverridesAnyConfiguration = new object();

var container = new UnityContainer();
container.Resolve<ATypeWithConstructorArguments>(new DependencyOverride(typeof(object), valueObjectThatOverridesAnyPreviousConfiguration);

这将覆盖 UnityContainer 上的任何先前配置,并注入我在 DependencyOverride 上提供的实例。

有没有办法在容器级别中指定它?比如扩展什么的?我不想在下定决心的时候这么做!

谢谢!如果我让您感到困惑,请告诉我,我会提供更多信息。

By using Microsoft's Unity, I am able to override a dependency in the exact moment that I'm resolving an instance, for example:

var valueObjectThatOverridesAnyConfiguration = new object();

var container = new UnityContainer();
container.Resolve<ATypeWithConstructorArguments>(new DependencyOverride(typeof(object), valueObjectThatOverridesAnyPreviousConfiguration);

That will override any previous configuration on my UnityContainer and inject the instance I provided on the DependencyOverride.

Is there a way to specify it in a Container level ? Like an extension or something ? I don't want to do it at the resolve moment!

Thanks! Let me know if I'm being confuse to you, I'll provide more info.

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

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

发布评论

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

评论(1

倥絔 2024-12-28 21:10:59

你的意思是像这个这样的东西吗?听起来像你的问题的部分是从线程的中间开始的。

它允许您执行此操作

  var container = new UnityContainer();
  container.AddNewExtension<SemanticGroupExtension>();

  container.RegisterGroup<IVehicle, Car>("Car").Use<IWheel, CarWheel>().Use<IEngine, CarEngine>();
  container.RegisterGroup<IVehicle, Motorcycle>("Motorcycle").Use<IWheel, MotorcycleWheel>().Use<IEngine, MotorcycleEngine>();

  var car = container.Resolve<IVehicle>("Car");
  Assert.IsInstanceOfType(car.Wheel, typeof(CarWheel));
  Assert.IsInstanceOfType(car.Engine, typeof(CarEngine));

  var motorcycle = container.Resolve<IVehicle>("Motorcycle");
  Assert.IsInstanceOfType(motorcycle.Wheel, typeof(MotorcycleWheel));
  Assert.IsInstanceOfType(motorcycle.Engine, typeof(MotorcycleEngine));

可以在 TecX.Unity 项目中的此处找到源代码。

Do you mean something like this. The part that sounds like your problem starts in the middle of the thread.

It allows you to do this

  var container = new UnityContainer();
  container.AddNewExtension<SemanticGroupExtension>();

  container.RegisterGroup<IVehicle, Car>("Car").Use<IWheel, CarWheel>().Use<IEngine, CarEngine>();
  container.RegisterGroup<IVehicle, Motorcycle>("Motorcycle").Use<IWheel, MotorcycleWheel>().Use<IEngine, MotorcycleEngine>();

  var car = container.Resolve<IVehicle>("Car");
  Assert.IsInstanceOfType(car.Wheel, typeof(CarWheel));
  Assert.IsInstanceOfType(car.Engine, typeof(CarEngine));

  var motorcycle = container.Resolve<IVehicle>("Motorcycle");
  Assert.IsInstanceOfType(motorcycle.Wheel, typeof(MotorcycleWheel));
  Assert.IsInstanceOfType(motorcycle.Engine, typeof(MotorcycleEngine));

The sources can be found here inside the TecX.Unity project.

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