Unity新手问题

发布于 2024-07-26 04:51:07 字数 1373 浏览 3 评论 0原文

我是第一次尝试使用 Unity,我想我可能已经贪多嚼不烂了。 我们有一个 n 层应用程序,它有一个具有多个抽象类型的基础库,然后在其之上有几个具有具体类型的业务场景特定库。 例如:抽象类型 Lead 有两种实现,一种在名为 NewAutomotiveLead 的 NewAutomotiveLibrary 中,一种在名为 AutomotiveFinanceLead 的 AutomotiveFinanceLibrary 中。 在基础库中,我们有一组适配器,可以对 Lead 等基本类型执行逻辑。

我第一次尝试使用 Unity 返回一个接口 ILeadDuplication,当我在 ILeadDuplication 上调用解析并传递“NewAutomotive”或“AutomotiveFinance”的字符串值时,该接口在解析时返回 NewAutomotiveLeadDuplication 或 AutomotiveFinanceLeadDuplication 的实例(在容器上调用 RegisterType 时映射的名称)。 像这样:

  using (IUnityContainer container = new UnityContainer())
  {
    container
      .RegisterType<ILeadDuplication, AutomotiveFinanceLeadDuplication>("AutomotiveFinance")
      .RegisterType<ILeadDuplication, NewAutomotiveLeadDuplication>("NewAutomotive");

    ILeadDuplication dupe = container.Resolve<ILeadDuplication>("AutomotiveFinance");
    Console.WriteLine(dupe.Created);
  }

注意:这是为了说明,因为库不知道有关 ILeadDuplication 的 concreate 类的任何信息,实际注册需要在配置文件中完成。

虽然这一切都很有效,但我需要更进一步。 调用解析时,我需要能够传入 Lead 类型的参数,该参数是 NewAutomotiveLead 或 AutomotiveFinanceLead 的基本类型。

我需要知道 Unity 是否有可能以某种方式神奇地查找特定于具体实例 AutomotiveFinanceLead 的属性,例如 Lead 上不存在的“GrossMonthlyIncome”,并将其分配给新创建的 AutomotiveFinanceLeadDuplication 实例属性 GrossMonthlyIncome。

我实际上希望能够对基础库中的 ILeadDuplication 实例执行一组通用逻辑,即使生成的实例和映射的属性对其不熟悉。

谢谢!

I'm attempting to use Unity for the first time and I think I might have bitten off more than I can chew. We have a n-tier application that has a base library with several abstract types and then several business scenario specific libraries on top of it w/ concrete types. For ex: The abstract type lead has two implementations, one in a NewAutomotiveLibrary called NewAutomotiveLead and one in an AutomotiveFinanceLibrary called AutomotiveFinanceLead. Within the base library we have a set of adapters that perform logic on the base types like Lead.

I'm trying to use Unity for the first time to return an interface ILeadDuplication that when resolved, returns either an instance of NewAutomotiveLeadDuplication or AutomotiveFinanceLeadDuplication when I called resolve on ILeadDuplication and pass either a string value of either "NewAutomotive" or "AutomotiveFinance" (names mapped when RegisterType was called on the container). Like so:

  using (IUnityContainer container = new UnityContainer())
  {
    container
      .RegisterType<ILeadDuplication, AutomotiveFinanceLeadDuplication>("AutomotiveFinance")
      .RegisterType<ILeadDuplication, NewAutomotiveLeadDuplication>("NewAutomotive");

    ILeadDuplication dupe = container.Resolve<ILeadDuplication>("AutomotiveFinance");
    Console.WriteLine(dupe.Created);
  }

NOTE: This is for illustration, because the library doesn't know anything about the concreate classes for ILeadDuplication the actually registration would need to be done in the config file.

While this all works great, I need to take it a step further. When calling resolve, I need to be able to pass in an argument of type Lead which is the base type for either NewAutomotiveLead or AutomotiveFinanceLead.

I need to know if it's possible that Unity somehow magically look a property specific to the concrete instance AutomotiveFinanceLead such as "GrossMonthlyIncome" that doesn't exist on Lead and assign it to a new created AutomotiveFinanceLeadDuplication instances property GrossMonthlyIncome.

I'd effectively like to be able to perform a generic set of logic against instances of ILeadDuplication in the base library even thought the instances being generated and properties being mapped are unfamiliar to it.

Thanks!

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

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

发布评论

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

评论(2

回忆凄美了谁 2024-08-02 04:51:07

不太确定你想要什么,但我的猜测是你想在构建时传递依赖项。 这可以通过多种方式完成,但这里有一个例子。

[InjectionConstructor]
public class MyClass([Dependency("Named")] MyOtherClass other)
{

}

为了使其正常工作,名为“Named”的 MyOtherClass 依赖项必须通过配置或通过 RegisterType 方法添加到容器中。

如果我偏离了基地,请告诉我,也许我可以进一步帮助您...我在不同情况下使用 Unity 进行了大量工作,因此只要我知道您想要完成什么,我就可以帮助您。

Not quite sure what you are after but my guess is that you want to pass in a dependency at the construction time. This can be done in numerous ways but here´s an example.

[InjectionConstructor]
public class MyClass([Dependency("Named")] MyOtherClass other)
{

}

in order for that to work the MyOtherClass dependency named "Named" has to be in the container either via configuration or by adding it via the the RegisterType method.

Let me know if I´m off base and maybe I can help you further...I have worked quite a lot with Unity in different situations so as long as I know what you want to accomplish I might be able to help you out.

浅唱ヾ落雨殇 2024-08-02 04:51:07

我不明白为什么你希望容器承担这个责任。 但我认为你可以这样做:

就在调用 Resolve() 获取 ILeadDuplication 的新实例之前,您可以注册要使用的 Lead 实例和要使用的 GrossMonthlyIncome 值:

  Lead lead = new NewAutomotiveLead()
  container.RegisterInstance<Lead>(lead);
  container.RegisterInstance<int>("GrossMonthlyIncome", lead.GrossMonthlyIncome);
  ILeadDuplication dupe = container.Resolve<ILeadDuplication>("AutomotiveFinance");

如果具体类型具有名为的依赖项“GrossMonthlyIncome”,该值将被注入那里。 如果具体类型的构造函数采用 Lead 实例,则该实例将被注入到该构造函数参数中。

I don't understand why you want the container to have that responsibility. But I think you could do it this way:

Just before you call Resolve() to get an new instance of ILeadDuplication, you could register the instance of Lead to use and the value of GrossMonthlyIncome to use:

  Lead lead = new NewAutomotiveLead()
  container.RegisterInstance<Lead>(lead);
  container.RegisterInstance<int>("GrossMonthlyIncome", lead.GrossMonthlyIncome);
  ILeadDuplication dupe = container.Resolve<ILeadDuplication>("AutomotiveFinance");

If the concrete type has a dependency named "GrossMonthlyIncome", the value will be injected there. IF the concrete type's constructor takes a Lead instance, the instance will be injected in that constructor parameter.

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