开发者

When using an IoC container, how to pass dynamic data / objects to a class?

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开发者_JAVA技巧 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?


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.


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.


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;}
}


@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();
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜