Orchard IDependency / Autofac resolving

Topics: General, Writing modules
Jun 27, 2013 at 3:17 AM
I have a driver that looks like this:
    public class ProductPartDriver : ContentPartDriver<ProductPart>
    {
        public IProductService ProductService { get; set; }
        private readonly IContentManager _contentManager;
        private readonly IProductService _productService;
        private readonly IPriceProvider _priceProvider;

        public ProductPartDriver(IContentManager contentManager, IProductService productService, IPriceProvider priceProvider) {
            _priceProvider = priceProvider;
            _contentManager = contentManager;
            _productService = productService;
        }
....
The interface looks like this:
using Orchard;

namespace CV.Prototype.Pricing {
    public interface IPriceProvider : IDependency{
        decimal GetPrice();
    }
}
There are 3 implementations of IPriceProvider that look basically like this:
    public class DefaultPriceProvider : IPriceProvider
    {
        public decimal GetPrice()
        {
            return 1;
        }
Yet in the constructor Orchard resolves this quite randomly. Is it the case that constructor injection is only appropriate when there is a single implementation?
Developer
Jun 27, 2013 at 3:56 AM
If you have multiple registrations and inject just one - you'll get the one that has been registered last, which is quite hard to determine.
You'd better inject all instances using IEnumerable<IPriceProvider> and pick the one you need instead inside your ProductPartDriver.
If you need to have one and suppress other implementations, use OrchardSuppressDependencyAttribute.
Jun 27, 2013 at 5:49 AM
Thanks pszmyd, that's a great explanation.