Ejemplo n.º 1
0
        public void Instance()
        {
            var provider = _dependencies
                           .AddInstance(new JConverter())
                           .BuildProvider();

            var first  = provider.GetService <JConverter>();
            var second = provider.GetService <JConverter>();

            Assert.Same(first, second);
        }
Ejemplo n.º 2
0
 public static DependencyCollection AddInstance(
     this DependencyCollection dependencies,
     Type contract,
     object instance)
 {
     return(dependencies.AddInstance(new[] { contract }, instance));
 }
Ejemplo n.º 3
0
 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);
 }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        public static DependencyCollection AddFileServer(this DependencyCollection dependencies, string?path = null)
        {
            if (!dependencies.Contains <HttpServer>())
            {
                AddServer(dependencies);
            }

            dependencies.AddInstance <IHttpRequestHandler>(new FileRequestHandler(path));
            return(dependencies);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        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));
            }
        }
Ejemplo n.º 12
0
        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);
        }
Ejemplo n.º 13
0
 public static DependencyCollection AddLogWriter(this DependencyCollection dependencies, ILogWriter logWriter)
 {
     dependencies.AddInstance(logWriter);
     return(dependencies);
 }
Ejemplo n.º 14
0
 public static DependencyCollection AddPool <T>(this DependencyCollection dependencies, T[] buffer)
     where T : class
 {
     dependencies.AddInstance <IPool <T> >(new Pool <T>(buffer));
     return(dependencies);
 }