/// <summary> /// /// </summary> /// <param name="item"></param> /// <returns></returns> public static ServiceDescriptor CreateServiceDescriptor(this ServiceDescriptorConfiguration item) { ServiceDescriptor returnValue = null; if (item.ShouldCreate()) { // // Get the service type. // Type serviceType = item.FindType(TypeSource.Service); // // Get the implementation type. // Type implementationType = item.FindType(TypeSource.Implemenation); // // Check if the implementation type has any dependency properties. // IEnumerable <DependencyInfo> dependencyProperties = DependencyAttribute.GetDependencyProperties(implementationType); // // Select the lifetime. // ServiceLifetime lifetime = item.Lifetime == "Scoped" ? ServiceLifetime.Scoped : item.Lifetime == "Singleton" ? ServiceLifetime.Singleton : ServiceLifetime.Transient; // // Create the descriptor. // if (item.Properties == null && !dependencyProperties.Any()) { // // Standard definition. // returnValue = ServiceDescriptor.Describe(serviceType, implementationType, lifetime); } else { // // Factory based definition. // returnValue = ServiceDescriptor.Describe(serviceType, sp => (new DependencyFactory(implementationType, item, dependencyProperties)).GetInstance(sp), lifetime); } } return(returnValue); }
/// <summary> /// /// </summary> /// <param name="item"></param> /// <returns></returns> public static ServiceDescriptor CreateDatabaseDescriptor(this DatabaseDescriptorConfiguration item) { ServiceDescriptor returnValue = null; if (item.ShouldCreate()) { // // Get the service type. // Type serviceType = null; Type implementationType = null; IEnumerable <DependencyInfo> dependenctProperties = new DependencyInfo[0]; try { serviceType = item.FindType(TypeSource.Service); implementationType = item.FindType(TypeSource.Implemenation); dependenctProperties = DependencyAttribute.GetDependencyProperties(implementationType); } catch { throw new DbContextNotFoundException(item.Context); } // // Get the factory type. // Type factoryType = null; try { factoryType = FindType(item.Factory); } catch { throw new DbContextDependencyFactoryNotFoundException(item.Factory); } // // Create the service descriptor. // returnValue = ServiceDescriptor.Describe(serviceType, (sp) => { // // Get the connection string. // IConfiguration configuration = sp.GetService <IConfiguration>(); string connectionString = configuration[item.ConnectionString]; // // Get the dependency factory and assign the properties. // IDependencyFactory dependencyFactory = (IDependencyFactory)ActivatorUtilities.CreateInstance(sp, factoryType, implementationType, item); DependencyFactory.AssignProperties(item.Properties, implementationType, dependencyFactory); // // Create the context and set the dependencies. // object instance = dependencyFactory.GetInstance(sp, connectionString); DependencyAttribute.SetDependencyProperties(sp, dependenctProperties, instance); // // Return the context instance. // return(instance); }, item.Lifetime == "Scoped" ? ServiceLifetime.Scoped : item.Lifetime == "Singleton" ? ServiceLifetime.Singleton : ServiceLifetime.Transient); } return(returnValue); }