public static IFactoryCollection AddHostedService <TService>(this IFactoryCollection collection) where TService : class, IHostedService { if (collection.AllowAddServiceToCollection <TService>()) { collection.Add(ServiceDescriptor.HostedService <TService>()); } return(collection); }
public static IFactoryCollection AddFirstLoader <TService>(this IFactoryCollection collection, bool singleton = true) where TService : class, IFirstLoader { if (collection.AllowAddServiceToCollection <TService>()) { collection.Add(ServiceDescriptor.FirstLoader <TService>(singleton)); } return(collection); }
public static IFactoryCollection AddTransient <TService, TImpl>(this IFactoryCollection collection) where TService : class where TImpl : class, TService { if (collection.AllowAddServiceToCollection <TService, TImpl>()) { collection.Add(ServiceDescriptor.Transient <TService, TImpl>()); } return(collection); }
public static IFactoryCollection AddSingleton <TService>(this IFactoryCollection collection) where TService : class { if (collection.AllowAddServiceToCollection <TService>()) { collection.Add(ServiceDescriptor.Singleton <TService>()); } return(collection); }
public bool Remove(IFactoryCollection collection) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } return(m_collections.Remove(collection.IdentifierType)); }
public void Add(IFactoryCollection collection) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } m_collections.Add(collection.IdentifierType, collection); }
public void GetGeneric() { var provider = new FactoryProvider(); provider.Add(new FactoryCollection <int>()); IFactoryCollection <int> collection = provider.Get <int>(); Assert.NotNull(collection); }
public TinyFactoryService(bool throwIfNotExist = false) { ThrowNotExist = throwIfNotExist; collections = new FactoryCollection(); ConfigureFactory(collections); collections.Build(); StartFirstLoaders(); StartHostedServices(); }
public bool TryGet <T>(out IFactoryCollection <T> collection) { if (m_collections.TryGetValue(typeof(T), out IFactoryCollection value) && value is IFactoryCollection <T> result) { collection = result; return(true); } collection = null; return(false); }
/// <summary> /// Gets builder from the provider by the specified factory identifier and builder identifier. /// <para> /// The type of the factory identifier represents the identifier of the factory collection. /// </para> /// </summary> /// <param name="provider">The factory provider.</param> /// <param name="factoryId">The identifier of the factory.</param> /// <param name="builderId">The identifier of the builder.</param> public static TBuilder GetBuilder <TBuilder, TFactoryId, TBuilderId>(this IFactoryProvider provider, TFactoryId factoryId, TBuilderId builderId) where TBuilder : IBuilder { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } IFactoryCollection <TFactoryId> collection = provider.Get <TFactoryId>(); var factory = collection.Get <IFactory <TBuilderId> >(factoryId); return(factory.Get <TBuilder>(builderId)); }
public static IFactoryCollection AddHostedService <TService>(this IFactoryCollection collection, TService instance) where TService : class, IHostedService { if (instance == null) { throw new ArgumentNullException("HostedService instance cannot be null"); } if (collection.AllowAddServiceToCollection <TService>(instance.GetType())) { collection.Add(ServiceDescriptor.HostedService <TService>()); } return(collection); }
public static IFactoryCollection AddSingleton <TService>(this IFactoryCollection collection, object instance) where TService : class { if (instance == null) { throw new ArgumentNullException("Singleton instance cannot be null"); } if (collection.AllowAddServiceToCollection <TService>(instance.GetType(), true)) { collection.Add(ServiceDescriptor.Singleton <TService>(instance)); } return(collection); }
internal static bool AllowAddServiceToCollection <TService>(this IFactoryCollection collection) { if (collection.IsReadOnly) { throw new InvalidOperationException("You cannot add to collection. Factory Collection is read-only"); } if (collection.FirstOrDefault(o => o.ServiceType.Equals(typeof(TService))) != null) { throw new Exception("This type of service has been registered to the factory before"); } var constructors = typeof(TService).GetConstructors(); if (constructors == null || constructors.Length == 0) { throw new Exception("This type of service has no public constructors"); } return(true); }
protected override void ConfigureFactory(IFactoryCollection collection) { collection.AddSingleton <IRepository, UserRepository>(); collection.AddTransient <IRepositoryConfig, Config>(); }
public bool TryGet(Type type, out IFactoryCollection collection) { return(m_collections.TryGetValue(type, out collection)); }
/// <summary> /// Factory configuration method /// </summary> /// <param name="collection"></param> protected abstract void ConfigureFactory(IFactoryCollection collection);
protected override void ConfigureFactory(IFactoryCollection collection) { collection.AddSingleton <InterfaceA, ClassA>(); collection.AddTransient <InterfaceB, ClassB>(); }