public void Instance() { var provider = _dependencies .AddInstance(new JConverter()) .BuildProvider(); var first = provider.GetService <JConverter>(); var second = provider.GetService <JConverter>(); Assert.Same(first, second); }
public static DependencyCollection AddInstance( this DependencyCollection dependencies, Type contract, object instance) { return(dependencies.AddInstance(new[] { contract }, instance)); }
public static DependencyCollection AddPool <T>(this DependencyCollection dependencies, Func <T> builder, int capacity = 10) where T : class { dependencies.AddInstance <IPool <T> >(new Pool <T>(capacity, builder)); return(dependencies); }
public static DependencyCollection CreateQueryProcessor <TQuery, TResult>(this DependencyCollection dependencies, Func <TQuery, TResult> processor) where TQuery : IQuery <TResult> { var instance = new ActionQueryProcessor <TQuery, TResult>(processor); return(dependencies.AddInstance <IQueryProcessor <TQuery, TResult> >(instance)); }
public static DependencyCollection CreateCommandProcessor <TCommand>(this DependencyCollection dependencies, Action <TCommand> processor) where TCommand : ICommand { var instance = new ActionCommandProcessor <TCommand>(processor); return(dependencies.AddInstance <ICommandProcessor <TCommand> >(instance)); }
public static DependencyCollection AddMetricsCounter <TSender>(this DependencyCollection dependencies, ICounter <TSender> counter) { var contracts = new[] { typeof(ICounter), typeof(ICounter <TSender>) }; dependencies.AddInstance(contracts, counter); return(dependencies); }
public static DependencyCollection AddFileServer(this DependencyCollection dependencies, string?path = null) { if (!dependencies.Contains <HttpServer>()) { AddServer(dependencies); } dependencies.AddInstance <IHttpRequestHandler>(new FileRequestHandler(path)); return(dependencies); }
public static DependencyCollection AddMetricsCounter <TSender>(this DependencyCollection dependencies, string name, string description, params ICounterLabel[] labels) { var contracts = new[] { typeof(ICounter), typeof(ICounter <TSender>) }; var counter = new Counter <TSender>(name, description, labels); dependencies.AddInstance(contracts, counter); return(dependencies); }
public static DependencyCollection AddDefaultLogEnrichers(this DependencyCollection dependencies, string dateTimeFormat = "s") { var contracts = new[] { Typeof <ILogEnricher> .Raw }; dependencies .AddInstance(contracts, new TimeStampEnricher(dateTimeFormat)) .AddInstance(contracts, new LogLevelEnricher()) .AddInstance(contracts, new SenderEnricher()); return(dependencies); }
private static bool AddInstance( DependencyCollection dependencies, object instance, Type[] genericInterfaces) { var type = instance.GetType(); var contracts = ReflectionUtils.GetGenericInterfaceImplementations(type, genericInterfaces); if (contracts.Length == 0) { return(false); } dependencies.AddInstance(contracts.ToArray(), instance); return(true); }
private static void AddSingleton(DependencyCollection dependencies, ServiceDescriptor descriptor) { if (descriptor.ImplementationInstance != null) { dependencies.AddInstance(descriptor.ServiceType, descriptor.ImplementationInstance); } else if (descriptor.ImplementationFactory == null) { dependencies.AddSingleton(descriptor.ServiceType, descriptor.ImplementationType); } else { var contracts = new[] { descriptor.ServiceType }; var resolver = BuildDelegateResolver(descriptor); dependencies.Add(new SingletonDependency(contracts, resolver)); } }
public static DependencyCollection AddECS(this DependencyCollection dependencies) { dependencies .AddSingleton <IEntityState, EntityState>(); //actors dependencies .AddFactory(new FilterFactory <IActorContext>(typeof(IActorFilter))) .AddFactory(new GroupFactory <IActorContext>(typeof(IActorGroup))) .AddFactory(new SingleFactory <IActorContext>(typeof(SingleActor <>))) .AddInstance <IActorContext>(new ActorContext()) .AddSingleton <IActorFactory, ActorFactory>(); // assets dependencies .AddFactory(new FilterFactory <IAssetContext>(typeof(IAssetFilter))) .AddFactory(new GroupFactory <IAssetContext>(typeof(IAssetGroup))) .AddFactory(new SingleFactory <IAssetContext>(typeof(SingleAsset <>))) .AddSingleton <IAssetContext, AssetContext>() .AddSingleton <AssetFactory>(); // components dependencies .AddSingleton <IComponentFactory, ComponentFactory>(); // systems dependencies .AddFactory <ISystemService, SystemService>(systemService => systemService.DependedLifetime()) .AddFactory(new SystemPipelineFactory()); // source dependencies .AddInstance(new SourceDescriptions()) .AddSingleton(typeof(IEntitySourceContext <>), typeof(EntitySourceContext <>)) .EnsureJsonEnabled(); return(dependencies); }
public static DependencyCollection AddLogWriter(this DependencyCollection dependencies, ILogWriter logWriter) { dependencies.AddInstance(logWriter); return(dependencies); }
public static DependencyCollection AddPool <T>(this DependencyCollection dependencies, T[] buffer) where T : class { dependencies.AddInstance <IPool <T> >(new Pool <T>(buffer)); return(dependencies); }