Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void RegisterFromServiceCollection(IServiceCollection services)
- {
- var serviceProvider = services.BuildServiceProvider();
- var delegateType = typeof(Delegate);
- foreach (var service in services)
- {
- if (delegateType.IsAssignableFrom(service.ServiceType))
- {
- Debug.Log($"Skipping type registration, because of delegate registration: {service.ServiceType}. Register this type manually");
- continue;
- }
- ScopeConcreteIdArgConditionCopyNonLazyBinder binding;
- if (!(service.ImplementationType is null))
- {
- binding = Container.Bind(service.ServiceType).To(service.ImplementationType);
- }
- else if (!(service.ImplementationFactory is null))
- {
- binding = Container.Bind(service.ServiceType).FromMethod(_ => service.ImplementationFactory(serviceProvider));
- }
- else
- {
- binding = Container.Bind(service.ServiceType).To(service.ServiceType);
- }
- switch (service.Lifetime)
- {
- case ServiceLifetime.Singleton:
- binding.AsSingle();
- break;
- case ServiceLifetime.Scoped:
- binding.AsCached();
- break;
- case ServiceLifetime.Transient:
- binding.AsTransient();
- break;
- default:
- throw new ArgumentOutOfRangeException($"Unknown service lifetime{service}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement