private ServiceDescriptor MapToExtensionDescriptor(TwinoServiceDescriptor descriptor) { ServiceLifetime lifetime; switch (descriptor.Implementation) { case ImplementationType.Scoped: lifetime = ServiceLifetime.Scoped; break; case ImplementationType.Singleton: lifetime = ServiceLifetime.Singleton; if (descriptor.Instance != null) { return(new ServiceDescriptor(descriptor.ServiceType, descriptor.Instance)); } break; default: lifetime = ServiceLifetime.Transient; break; } if (descriptor.ImplementationFactory != null) { return(new ServiceDescriptor(descriptor.ServiceType, descriptor.ImplementationFactory, lifetime)); } return(new ServiceDescriptor(descriptor.ServiceType, descriptor.ImplementationType, lifetime)); }
/// <summary> /// Adds a singleton service to the container. /// Service will be created with first call. /// </summary> public void AddSingleton(Type serviceType, Type implementationType) { TwinoServiceDescriptor descriptor = new TwinoServiceDescriptor(ImplementationType.Singleton, serviceType, implementationType); _items.Add(descriptor); _provider = null; }
internal void Build(IEnumerable <TwinoServiceDescriptor> services) { BuildItem(new TwinoServiceDescriptor(ImplementationType.Singleton, typeof(IServiceProvider), GetType(), this), services); BuildItem(new TwinoServiceDescriptor(ImplementationType.Singleton, typeof(ITwinoServiceProvider), GetType(), this), services); BuildItem(new TwinoServiceDescriptor(ImplementationType.Singleton, typeof(IServiceContainer), GetType(), this), services); foreach (TwinoServiceDescriptor descriptor in services) { //skip microsoft's options open generic registrations if (descriptor.ImplementationType.IsGenericType && descriptor.ImplementationType.IsGenericTypeDefinition) { continue; } if (OptionsHelper.IsConfigurationType(descriptor.ServiceType)) { TwinoServiceDescriptor optionsDescriptor = CreateOptionsItem(descriptor); BuildItem(optionsDescriptor, services); } else { BuildItem(descriptor, services); } } foreach (KeyValuePair <Type, BuiltServiceDescriptor> pair in _services) { FillParameterDescriptors(pair.Value, pair.Value); } }
/// <summary> /// Adds a singleton service with instance to the container. /// </summary> public void AddSingleton(Type serviceType, object instance) { Type implementationType = instance.GetType(); TwinoServiceDescriptor descriptor = new TwinoServiceDescriptor(ImplementationType.Singleton, serviceType, implementationType, instance); _items.Add(descriptor); _provider = null; }
/// <summary> /// Adds new Microsoft Extension service to service container /// </summary> public void Add(ServiceDescriptor item) { TwinoServiceDescriptor descriptor = MapServiceDescriptor(item); if (descriptor != null) { _items.Add(descriptor); } }
/// <summary> /// Adds a singleton service to the container. /// Service will be created with first call. /// </summary> public void AddSingleton(Type serviceType, Type implementationType, Delegate afterCreated) { TwinoServiceDescriptor descriptor = new TwinoServiceDescriptor(ImplementationType.Singleton, serviceType, implementationType) { AfterCreatedMethod = afterCreated }; _items.Add(descriptor); _provider = null; }
/// <summary> /// Adds a service pool to the container /// </summary> /// <param name="type">Implementation type</param> /// <param name="options">Options function</param> /// <param name="instance">After each instance is created, to do custom initialization, this method will be called.</param> private void AddPool <TService, TImplementation>(ImplementationType type, Action <ServicePoolOptions> options, Action <TService> instance) where TService : class where TImplementation : class, TService { ServicePool <TService, TImplementation> pool = new ServicePool <TService, TImplementation>(type, this, options, instance); TwinoServiceDescriptor descriptor = new TwinoServiceDescriptor(ImplementationType.Singleton, typeof(TService), typeof(TImplementation), //typeof(ServicePool<TService, TImplementation>), pool) { IsPool = true }; _items.Add(descriptor); _provider = null; }
/// <summary> /// Adds a service to the container /// </summary> public void AddScoped(Type serviceType, Type implementationType, Type proxyType, Delegate afterCreated) { if (_items.Any(x => x.ServiceType == serviceType)) { throw new DuplicateTypeException($"{serviceType.ToTypeString()} service type is already added into service container"); } TwinoServiceDescriptor descriptor = new TwinoServiceDescriptor(ImplementationType.Scoped, serviceType, implementationType) { ProxyType = proxyType, ProxyInstance = null, AfterCreatedMethod = afterCreated }; _items.Add(descriptor); _provider = null; }
private TwinoServiceDescriptor MapServiceDescriptor(ServiceDescriptor item) { if (item == null) { throw new NullReferenceException("Service descriptor is null"); } ImplementationType impl; switch (item.Lifetime) { case ServiceLifetime.Scoped: impl = ImplementationType.Scoped; break; case ServiceLifetime.Singleton: impl = ImplementationType.Singleton; break; default: impl = ImplementationType.Transient; break; } Type implementationType = item.ImplementationType; if (implementationType == null) { implementationType = item.ImplementationInstance != null ? item.ImplementationInstance.GetType() : item.ServiceType; } TwinoServiceDescriptor descriptor = new TwinoServiceDescriptor(impl, item.ServiceType, implementationType, item.ImplementationInstance) { MicrosoftServiceDescriptor = item, ImplementationFactory = item.ImplementationFactory }; return(descriptor); }
private TwinoServiceDescriptor CreateOptionsItem(TwinoServiceDescriptor descriptor) { var configFunc = descriptor.ImplementationFactory; Type optionsType = descriptor.ServiceType.GetGenericArguments()[0]; Type openGeneric = typeof(IOptions <>); Type optionsServiceType = openGeneric.MakeGenericType(optionsType); TwinoServiceDescriptor optionsDescriptor = new TwinoServiceDescriptor(descriptor.Implementation, optionsServiceType, optionsServiceType); optionsDescriptor.ImplementationFactory = prov => { dynamic configure = configFunc(this); dynamic options = Activator.CreateInstance(optionsType); object optionsInstance = Options.Create(options); configure.Configure(options); return(optionsInstance); }; return(optionsDescriptor); }
private BuiltServiceDescriptor BuildItem(TwinoServiceDescriptor descriptor, IEnumerable <TwinoServiceDescriptor> services) { ConstructorHelper ctorHelper = new ConstructorHelper(services); BuiltServiceDescriptor builtDescriptor = new BuiltServiceDescriptor(descriptor.Implementation, descriptor.ServiceType, descriptor.ImplementationType); builtDescriptor.Instance = descriptor.Instance; builtDescriptor.ImplementationFactory = descriptor.ImplementationFactory; builtDescriptor.IsPool = descriptor.IsPool; builtDescriptor.AfterCreatedMethod = descriptor.AfterCreatedMethod; //we need to find a ctor to create instance if (builtDescriptor.IsPool || builtDescriptor.Instance == null && builtDescriptor.ImplementationFactory == null) { ConstructorInfo constructorInfo = ctorHelper.FindAvailableConstructor(descriptor.ImplementationType); if (constructorInfo == null) { throw new IocConstructorException($"{descriptor.ServiceType.ToTypeString()} does not have available constructor"); } builtDescriptor.Build(constructorInfo); } _services.Add(descriptor.ServiceType, builtDescriptor); if (builtDescriptor.IsPool) { if (builtDescriptor.Instance is IServicePoolInternal pool) { pool.SetBuiltDescriptor(builtDescriptor); } } return(builtDescriptor); }