1

Closed

Two Registration without key possible.

description

I observed following behavior and felt its not really appropriate.
 
Current framework, allows to register with any key using
  1. IRegistration Register(Type type, Func<IIocContainer, object> func);
    It also allows to register with key using
  2. IRegistration Register<TType>(string name, Func<IIocContainer, TType> func) where TType : class;
     
    Now this allows end-user to register two mappings without using key, one with both methods. In implementation second method mentioned above its not validating if Key is empty or null, so it allows to have another registration without key. It is confusing why this feature is required. Since because of this there is some extra code which has been introduced into this package.
     
    I am not sure if I have explained things properly, but there is a issue.
Closed Jun 27, 2010 at 10:54 AM by mdennis
Misunderstanding of the role of keys when registering and resolving.

comments

mdennis wrote Jun 27, 2010 at 10:53 AM

Keys are used to distinguish between different implementation registrations. For example; lets say you have a pricing interface, IModule, and you have three different implementations you wish to use for Weekdays, Weekends, and Holidays.
You would register the three implemenations :
// --------------------------------------------------------------------
// The interface
public interface IPricing
{
decimal GetPrice(Product product);
}

// --------------------------------------------------------------------
// Register the IPricing implementations
public void PricingRegistration(Container c) // or use a reference to a application singleton
{
c.Register<IPricing>("Weekday", c => new WeekdayPricing());
c.Register<IPricing>("Weekend", c => new WeekendPricing());
c.Register<IPricing>("Holiday", c => new HolidayPricing());
}

// --------------------------------------------------------------------
// Using the IPricing instance to get the price for something
...
Product product = Catalog.GetProduct("Mountain Bike", "red; 22in;");
IPricing pricer = GetPricer(DateTime.Now, container);
decimal thePrice = pricer.GetPrice(product);
...
// --------------------------------------------------------------------
// Get the required IPricing Implementation
public IPricing GetPricer(DateTime date, Container c)
{
if (IsHoliday(date))
    return c.Resolve<IPricing>("Holiday");
else
{
    var dayOfWeek = date.DayOfWeek;
    switch (dayOfWeek) 
    {
        DayOfWeek.Sunday:
        DayOfWeek.Saturday:
            return c.Resolve<IPricing>("Weekend");
            break;

        default:
            return c.Resolve<IPricing>("Weekday");
            break;
    }
}
}

NOTE with the latest source you can register by Type instead of delegate so the PricingRegistration method could be written:
// --------------------------------------------------------------------
// Register the IPricing implementations
public void PricingRegistration(Container c) // or use a reference to a application singleton
{
c.Register<IPricing, WeekdayPricing>("Weekday");
c.Register<IPricing, WeekendPricing>("Weekend");
c.Register<IPricing, HolidayPricing>("Holiday");
}