Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registering a service with and without a factory always defaults to without #519

Open
knodel12 opened this issue Jan 3, 2020 · 0 comments

Comments

@knodel12
Copy link

knodel12 commented Jan 3, 2020

The main issue is that when you register a factory alongside a default implementation, resolving either always results in the default implementation.

  • Version Information
    LightInject 5.0.3.0

  • Platform
    .NET Framework 4.7.2

  • An example that shows the issue.

var container = new ServiceContainer();
container.Register<IPushNotificationSender, FirebasePushNotificationsSender>(new PerContainerLifetime());
container.Register<IPushNotificationSender, NotificationsWebPushSender>
    (DeviceMessageTypes.Chrome.ToString(), new PerContainerLifetime());
container.Register<IPushNotificationSender, NotificationsWebPushSender>
    (DeviceMessageTypes.FireFox.ToString(), new PerContainerLifetime());
container.Register<IPushNotificationSender, NotificationsGcmPushSender>
    (DeviceMessageTypes.GCM.ToString(), new PerContainerLifetime());
container.Register<IPushNotificationSender, NotificationsApnPushSender>
    (DeviceMessageTypes.APN.ToString(), new PerContainerLifetime());
container.Register<DeviceMessageTypes, IPushNotificationSender>((factory, value) => factory.GetInstance<IPushNotificationSender>(value.ToString()));

var factory = container.GetInstance<Func<DeviceMessageTypes, IPushNotificationSender>>();
var mainImpl = container.GetInstance<IPushNotificationSender>();

var factoryImpl = factory(DeviceMessageTypes.APN);
if (factoryImpl.GetType().Name == mainImpl.GetType().Name)
{
    // These shouldn't be the same...  
    // The factory should resolve to the factory function in the registration.
    Console.WriteLine("Expected factory to resolve to different type.");
    Console.WriteLine($"Expected Type: {typeof(NotificationsApnPushSender).Name}");
    Console.WriteLine($"Resolved Type: {factoryImpl.GetType().Name}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant