使用IoC容器时,如何将动态数据/对象传递给类?

发布于 2024-12-07 04:09:06 字数 574 浏览 1 评论 0原文

我有一个带有以下构造函数的 Order 类

public Order(IProduct product, short count)
{
this._product = product;
this._count = count;
}

,我正在尝试设置 Unity IoC 容器,显然要构造订单对象,我需要知道计数和产品,但这些是在运行时确定的;计数可以是任何值,产品可以是任何产品,例如胡萝卜、甜瓜等。

那么如何为此应用 IoC?

我认为的一种方法是我的构造函数只接受对象依赖项,然后使用新的 Initialize() 方法添加任何其他必需的属性:

public Order (IProduct product)
{
this._product = product;
}

public Initialize(short count)
{
this._count = count;
}

这样,无论谁创建 Order 对象,都必须随后调用 Initialize() 方法,以便其他属性将 IoC 容器无法处理的内容添加到其中。

这是您推荐/使用的方法吗?

I have an Order class with the below constructor

public Order(IProduct product, short count)
{
this._product = product;
this._count = count;
}

I'm trying to setup Unity IoC container and obviously to construct the order object I'd need to know the count and product but these are determined at run time; the count could be any value and product can be any product e.g. Carrot, Melon, etc.

So how to apply IoC for this?

One approach I thought was that my constructor only accepts object dependencies then any other required property to be added using a new Initialize() method:

public Order (IProduct product)
{
this._product = product;
}

public Initialize(short count)
{
this._count = count;
}

in this way whoever creates the Order object, has to call the Initialize() method afterwards so that other properties that can't be handled by the IoC container be added to it.

Is it the approach that you recommend/use?

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

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

发布评论

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

评论(4

少年亿悲伤 2024-12-14 04:09:06

对我来说,这似乎不适合 IoC 容器。据推测,订单可以在应用程序的整个生命周期中根据用户的行为定期实例化不同的产品(和数量),这表明需要为可能创建订单的每个上下文配置一个容器 - 也就是说,每个产品页面。

当您可以很少且尽早地(例如在应用程序启动时)就依赖关系做出决策时,IoC 容器的工作效果最佳。如果决定注入哪个依赖项总是与创建依赖对象同时发生,那么 IoC 容器只会增加不必要的复杂性。

This doesn't seem like an appropriate situation for an IoC container to me. Presumably Orders can be instantiated regularly with different Products (and counts) throughout the life of the application based on the behavior of the user, which suggests a container would need to be configured for each context in which Orders might be created - that is, for each Product page.

IoC containers work best when you can make decisions about dependencies seldom and early on, say at application start-up. If the decision on which dependency to inject always takes place at about the same time as the creation of the dependent object, then an IoC container just adds unnecessary complexity.

风流物 2024-12-14 04:09:06

使用 Unity,您应该能够设置 ParameterOverride 来传递额外的参数:

container.Resolve<IOrder>(new ParameterOverrides { { "Count", 1 } });

好的,或者创建一个工厂类:

class OrderFactory : IOrderFactory
{
    public OrderFactory ( IProduct product );
    public Order GetOrder (count)
    {
        return new Order ( product, count );
    }
}

使用容器来解析工厂。然后使用工厂来解决您的订单。

With Unity you should be able to set up a ParameterOverride to pass in your extra parameters :

container.Resolve<IOrder>(new ParameterOverrides { { "Count", 1 } });

Ok, alternatively create a factory class :

class OrderFactory : IOrderFactory
{
    public OrderFactory ( IProduct product );
    public Order GetOrder (count)
    {
        return new Order ( product, count );
    }
}

Use the container to resolve the factory. Then use the factory to resolve your orders.

天暗了我发光 2024-12-14 04:09:06

计数可能应该只是订单的属性。计数代表什么?订单行数还是产品数量?我不太确定你打算如何在这里实现 IoC 容器。

像这样的事情:

public class Order
{
  private IProduct _product;
  public Order(IProduct product)
  {
    _product = product;
  }

  public int Count {get;set;}
}

Count should probably just be a property of Order. What does count represent? The number of order lines or the quantity of product? I'm not quite sure how you plan on implementing and IoC container here.

Something like this:

public class Order
{
  private IProduct _product;
  public Order(IProduct product)
  {
    _product = product;
  }

  public int Count {get;set;}
}
九八野马 2024-12-14 04:09:06

@斯科特姆
这是我的 aspx 页面中的实际代码:

private IStoresRankingReportPresenter _presenter;

     protected void Page_Init(object sender, EventArgs e)
            {
                this._presenter = ServiceLocator.Current.GetInstance<IStoresRankingReportPresenter>();

                this._presenter.Initialize(this, this.Count);

                this._presenter.OnPageInit();
            }

@scottm
This is the actual code that I have in my aspx page:

private IStoresRankingReportPresenter _presenter;

     protected void Page_Init(object sender, EventArgs e)
            {
                this._presenter = ServiceLocator.Current.GetInstance<IStoresRankingReportPresenter>();

                this._presenter.Initialize(this, this.Count);

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