예제 #1
0
        public void GetNext()
        {
            var gn  = new StringIdGenerator();
            var len = 10000;
            var ids = new string[len];

            for (int i = 0; i < len; i++)
            {
                ids[i] = gn.GetNext() as string;
            }

            ids.Distinct().Count().ShouldBe(len);
        }
        private static void RegisterDependencies(IServiceCollection services, AnyServiceConfig config)
        {
            services.TryAddSingleton <IdGeneratorFactory>(sp =>
            {
                var stringGenerator = new StringIdGenerator();
                var f = new IdGeneratorFactory();
                f.AddOrReplace(typeof(string), stringGenerator);
                return(f);
            });

            services.TryAddSingleton(config);
            services.TryAddScoped(sp => sp.GetService <WorkContext>().CurrentEntityConfigRecord?.AuditSettings ?? config.AuditSettings);
            services.TryAddScoped(typeof(ICrudService <>), typeof(CrudService <>));

            // services.
            services.AddSingleton(config.EntityConfigRecords);
            //response mappers
            var mappers = config.EntityConfigRecords.Select(t => t.EndpointSettings.ResponseMapperType).ToArray();

            foreach (var m in mappers)
            {
                services.TryAddSingleton(m);
            }

            //mapper factory
            services.TryAddSingleton <IMapperFactory, DefaultMapperFactory>();
            //validator factory
            var validatorTypes = config.EntityConfigRecords.Select(t => t.CrudValidatorType).ToArray();

            foreach (var vType in validatorTypes)
            {
                foreach (var vt in vType.GetAllBaseTypes(typeof(object)))
                {
                    services.TryAddScoped(vt, vType);
                }
            }
            foreach (var ecr in config.EntityConfigRecords)
            {
                ValidateType <IFilterFactory>(ecr.FilterFactoryType);
                services.TryAddScoped(ecr.FilterFactoryType);

                var srv  = typeof(IModelPreparar <>).MakeGenericType(ecr.Type);
                var impl = ecr.ModelPrepararType.IsGenericTypeDefinition ?
                           ecr.ModelPrepararType.MakeGenericType(ecr.Type) :
                           ecr.ModelPrepararType;
                services.TryAddScoped(srv, impl);
            }
            services.TryAddScoped(typeof(IFilterFactory), sp =>
            {
                var wc     = sp.GetService <WorkContext>();
                var ffType = wc.CurrentEntityConfigRecord.FilterFactoryType;
                return(sp.GetService(ffType) as IFilterFactory);
            });

            services.TryAddScoped <WorkContext>();
            services.TryAddSingleton <IIdGenerator, StringIdGenerator>();
            services.TryAddTransient <IPermissionManager, PermissionManager>();
            services.TryAddTransient <ILogRecordManager, LogRecordManager>();

            services.TryAddScoped(sp =>
            {
                var wc = sp.GetService <WorkContext>();
                var ct = wc.CurrentType;
                //var ecrm = sp.GetService<EntityConfigRecordManager>();
                return(wc.CurrentEntityConfigRecord.EventKeys);
            });
            services.AddScoped(sp =>
            {
                var wc = sp.GetService <WorkContext>();
                var ct = wc.CurrentType;
                return(wc.CurrentEntityConfigRecord.PermissionRecord);
            });

            services.TryAddScoped(sp =>
            {
                var wc = sp.GetService <WorkContext>();
                var mt = wc.CurrentEntityConfigRecord?.EndpointSettings.ResponseMapperType ?? config.ServiceResponseMapperType;
                return(sp.GetService(mt) as IServiceResponseMapper);
            });

            var auditManagerType = config.AuditSettings.Disabled ?
                                   typeof(DummyAuditManager) :
                                   typeof(AuditManager);

            services.TryAddTransient(typeof(IAuditManager), auditManagerType);
            services.TryAddSingleton <IEventBus, DefaultEventsBus>();

            if (config.ManageEntityPermissions)
            {
                services.TryAddSingleton <IPermissionEventsHandler, DefaultPermissionsEventsHandler>();
            }
        }