Пример #1
0
 public StorageBuilder Register(Type type)
 {
     return(Update(x =>
     {
         AssemblyRegistrar.RegisterIfNotExist(x.Assemblies, type.Assembly);
         IEnumerable <IService> services = ServicesGenerator.GenerateServices(type, Object.Assemblies, null);
         ServiceRegistrar.Register(Object.Services, services);
     }));
 }
Пример #2
0
 public MyClass(IServiceRegistrar registrar)
 {
     // register as a type mapping
     registrar.Register <IMyService, MyService>();
     // register as a single instance
     registrar.RegisterInstance <IMyService>(new MyService {
         Value = "Value"
     });
     // register as a delegate
     registrar.Register <IMyService>(locator => new MyService {
         Value = "Value"
     });
 }
Пример #3
0
        public void Bootstrap(IServiceRegistrar registrar)
        {
            registrar.Register <ITextLogFormatter, TextLogFormatter>();
            registrar.RegisterSingleton <IClock, SystemClock>();
            registrar.RegisterSingleton <IApplicationOptions, ApplicationOptions>();
            registrar.RegisterSingleton <ILogger, Logger>();

            if (Debugger.IsAttached)
            {
                registrar.RegisterSingleton <ILogDestination, DebugLogDestination>();
            }
        }
Пример #4
0
        public void Bootstrap(IServiceRegistrar registrar)
        {
            registrar.Register <IGameEngine, GameEngine>();
            registrar.Register <IDeckCreator, DeckCreator>();

            registrar.Register <ISystem, CardDetailsProviderSystem>();
            registrar.Register <ISystem, CardPreparationSystem>();

            registrar.Register <ISystem, PowerSystem>();
            registrar.Register <ISystem, ToughnessSystem>();

            registrar.Register <ISystem, ZombiesGainsPlusOnePlusOneCounterSystem>();
            registrar.Register <ISystem, CreatureKilledSystem>();
        }
 private static void AddConcretionsThatCouldBeClosed(this IServiceRegistrar serviceRegistrar, RegistrationScope registrationScope, Type @interface, List <Type> concretions)
 {
     foreach (var type in concretions
              .Where(x => x.IsOpenGeneric() && x.CouldCloseTo(@interface)))
     {
         try
         {
             serviceRegistrar.Register(@interface, type.MakeGenericType(@interface.GenericTypeArguments), registrationScope);
         }
         catch (Exception)
         {
         }
     }
 }
Пример #6
0
        public static void RegisterBuilders(IServiceRegistrar serviceRegistrar, params Assembly[] assemblies)
        {
            var queryBuilderType  = typeof(QueryBuilder <>);
            var assemblyTypes     = assemblies.SelectMany(a => a.GetTypes()).ToList();
            var queryBuilderTypes = assemblyTypes
                                    .Where(t => !t.IsAbstract && !t.IsInterface)
                                    .Where(t => t.BaseType.IsGenericType && t.BaseType.GetGenericTypeDefinition() == queryBuilderType)
                                    .ToList();

            foreach (var builderType in queryBuilderTypes)
            {
                serviceRegistrar.Register(typeof(IQueryBuilder <>).MakeGenericType(builderType.BaseType.GetGenericArguments().First()), builderType);
            }
        }
Пример #7
0
        public static void RegisterListeners(IServiceRegistrar serviceRegistrar, params Assembly[] assemblies)
        {
            var eventListenerType  = typeof(EventListener <>);
            var assemblyTypes      = assemblies.SelectMany(a => a.GetTypes()).ToList();
            var eventListenerTypes = assemblyTypes
                                     .Where(t => !t.IsAbstract && !t.IsInterface)
                                     .Where(t => t.BaseType.IsGenericType && t.BaseType.GetGenericTypeDefinition() == eventListenerType)
                                     .GroupBy(t => t.BaseType.GetGenericArguments().First())
                                     .ToList();

            foreach (var events in eventListenerTypes)
            {
                foreach (var listener in events)
                {
                    serviceRegistrar.Register(typeof(IEventListener <>).MakeGenericType(events.Key), listener);
                }
            }
        }
Пример #8
0
        public static void RegisterRepositories(IServiceRegistrar serviceRegistrar, params Assembly[] assemblies)
        {
            var assemblyTypes   = assemblies.SelectMany(a => a.GetTypes()).ToList();
            var repositoryTypes = assemblyTypes
                                  .Where(t => t.IsInterface)
                                  .Where(t => t.GetInterfaces()
                                         .Any(i => i == typeof(IReadRepository) ||
                                              (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IWriteRepository <>)))
                                         )
                                  .ToList();
            var repositories = assemblyTypes
                               .Where(t => !t.IsAbstract && !t.IsInterface && t.GetInterfaces().Any(i => repositoryTypes.Contains(i)))
                               .ToList();

            foreach (var repositoryType in repositories)
            {
                var implementedRepositories = repositoryType.GetInterfaces().Where(i => repositoryTypes.Contains(i));
                foreach (var implementedRepository in implementedRepositories)
                {
                    serviceRegistrar.Register(implementedRepository, repositoryType);
                }
            }
        }
Пример #9
0
        public static void RegisterHandlers(IServiceRegistrar serviceRegistrar, params Assembly[] assemblies)
        {
            var commandHandlerTypes = new[]
            {
                typeof(CommandHandler <>),
                typeof(CommandHandler <,>),
                typeof(CommandHandler <, ,>),
                typeof(CommandHandler <, , ,>)
            };
            var assemblyTypes   = assemblies.SelectMany(a => a.GetTypes()).ToList();
            var commandHandlers = assemblyTypes
                                  .Where(t => !t.IsAbstract && !t.IsInterface)
                                  .Where(t => t.BaseType.IsGenericType && commandHandlerTypes.Any(ct => t.BaseType.GetGenericTypeDefinition() == ct))
                                  .ToList();

            foreach (var handlerType in commandHandlers)
            {
                var commandTypes = handlerType.BaseType.GenericTypeArguments.ToList();
                foreach (var commandType in commandTypes)
                {
                    serviceRegistrar.Register(typeof(ICommandHandler <>).MakeGenericType(commandType), handlerType);
                }
            }
        }
Пример #10
0
 private static IServiceRegistrar Register <TService, TImplementation>(this IServiceRegistrar serviceRegistrar, RegistrationScope registrationScope) where TImplementation : TService
 => serviceRegistrar.Register(typeof(TService), typeof(TImplementation), registrationScope);
Пример #11
0
        /// <summary>
        /// Helper method use to differentiate behavior between request handlers and notification
        /// handlers. Request handlers should only be added once (so set addIfAlreadyExists to false)
        /// Notification handlers should all be added (set addIfAlreadyExists to true)
        /// </summary>
        /// <param name="serviceRegistrar">      </param>
        /// <param name="registrationScope">     </param>
        /// <param name="openRequestInterface">  </param>
        /// <param name="assembliesToScan">      </param>
        /// <param name="addIfAlreadyExists">    </param>
        private static IServiceRegistrar ConnectImplementationsToTypesClosing(this IServiceRegistrar serviceRegistrar,
                                                                              RegistrationScope registrationScope,
                                                                              Type openRequestInterface,
                                                                              IEnumerable <Assembly> assembliesToScan,
                                                                              bool addIfAlreadyExists)
        {
            if (!assembliesToScan.Any())
            {
                assembliesToScan = AppDomain.CurrentDomain.GetAssemblies();
            }

            var concretions = new List <Type>();
            var interfaces  = new List <Type>();

            foreach (var type in assembliesToScan.SelectMany(a => a.DefinedTypes).Where(t => !t.IsOpenGeneric()))
            {
                var interfaceTypes = Enumerable.ToArray <Type>(type.FindInterfacesThatClose(openRequestInterface));
                if (!interfaceTypes.Any())
                {
                    continue;
                }

                if (type.IsConcrete())
                {
                    concretions.Add(type);
                }

                foreach (var interfaceType in interfaceTypes)
                {
                    interfaces.Fill(interfaceType);
                }
            }

            foreach (var @interface in interfaces)
            {
                var exactMatches = concretions.Where(x => x.CanBeCastTo(@interface)).ToList();
                if (addIfAlreadyExists)
                {
                    foreach (var type in exactMatches)
                    {
                        serviceRegistrar.Register(@interface, type, registrationScope);
                    }
                }
                else
                {
                    if (exactMatches.Count > 1)
                    {
                        exactMatches.RemoveAll(m => !IsMatchingWithInterface(m, @interface));
                    }

                    foreach (var type in exactMatches)
                    {
                        serviceRegistrar.Register(@interface, type, registrationScope);
                    }
                }

                if ([email protected]())
                {
                    serviceRegistrar.AddConcretionsThatCouldBeClosed(registrationScope, @interface, concretions);
                }
            }

            return(serviceRegistrar);
        }
Пример #12
0
 /// <summary>
 /// Registers the by naming convention.
 /// </summary>
 /// <param name="registrar">The registrar.</param>
 /// <param name="predicate">The predicate.</param>
 /// <param name="assemblies">The assemblies.</param>
 public static void RegisterByNamingConvention(this IServiceRegistrar registrar, Predicate <Type> predicate, params Assembly[] assemblies)
 {
     RegisterByNamingConvention((serviceType, implementationType) => registrar.Register(serviceType, implementationType), predicate, assemblies);
 }
Пример #13
0
        public static void RegisterDependencies(IServiceRegistrar registrar)
        {
            var bootstrapper = BootstrapperLocator.FindBootstrapper();

            var typeCatalogue      = new TypeCatalogue();
            var assemblyFilesNames = bootstrapper.GetAssemblyFileNamesToScanForExtensions()
                                     ?? throw new InvalidOperationException(ExceptionMessages.GetAssemblyFileNamesToScanForExtensionsShouldntReturnNull);

            typeCatalogue.Load(assemblyFilesNames);
            var typeCatalogueInstanceProvider = new TypeCatalogueInstanceProvider(typeCatalogue);

            var argumentValueFormatters = typeCatalogueInstanceProvider.InstantiateAllOfType <IArgumentValueFormatter>();
            var dummyFactories          = typeCatalogueInstanceProvider.InstantiateAllOfType <IDummyFactory>();
            var fakeOptionsBuilders     = typeCatalogueInstanceProvider.InstantiateAllOfType <IFakeOptionsBuilder>();

            var implicitOptionsBuilderCatalogue = new ImplicitOptionsBuilderCatalogue(fakeOptionsBuilders);

            var methodInfoManager      = new MethodInfoManager();
            var argumentConstraintTrap = new ArgumentConstraintTrap();
            var expressionArgumentConstraintFactory = new ExpressionArgumentConstraintFactory(argumentConstraintTrap);

            var fakeManagerAccessor = new DefaultFakeManagerAccessor();

            var fakeObjectCreator = new FakeObjectCreator(
                FakeCallProcessorProviderFactory,
                new CastleDynamicProxyInterceptionValidator(methodInfoManager),
                new DelegateProxyInterceptionValidator());

            var callExpressionParser = new CallExpressionParser();

            var interceptionAsserter = new DefaultInterceptionAsserter(fakeObjectCreator);

            var argumentValueFormatter = new ArgumentValueFormatter(argumentValueFormatters);

            var fakeObjectCallFormatter = new DefaultFakeObjectCallFormatter(argumentValueFormatter, fakeManagerAccessor);

            var callWriter = new CallWriter(fakeObjectCallFormatter, new FakeCallEqualityComparer());

            var configurationFactory = new ConfigurationFactory(RuleBuilderFactory);

            registrar.Register <IExpressionCallMatcherFactory>(new ExpressionCallMatcherFactory(expressionArgumentConstraintFactory, methodInfoManager));

            registrar.Register(expressionArgumentConstraintFactory);

            registrar.Register <FakeAndDummyManager>(
                new FakeAndDummyManager(
                    new DummyValueResolver(new DynamicDummyFactory(dummyFactories), fakeObjectCreator),
                    fakeObjectCreator,
                    implicitOptionsBuilderCatalogue));

            registrar.Register <IArgumentConstraintManagerFactory>(new ArgumentConstraintManagerFactory());

            registrar.Register(new EventHandlerArgumentProviderMap());

            registrar.Register <SequentialCallContext.Factory>(SequentialCallContextFactory);

            registrar.Register <IStartConfigurationFactory>(
                new StartConfigurationFactory(ExpressionCallRuleFactory, configurationFactory, callExpressionParser, interceptionAsserter));

            registrar.Register <IFakeConfigurationManager>(
                new FakeConfigurationManager(configurationFactory, ExpressionCallRuleFactory, callExpressionParser, interceptionAsserter));

            registrar.Register <IFakeManagerAccessor>(fakeManagerAccessor);

            registrar.Register <ICallExpressionParser>(callExpressionParser);

            registrar.Register((StringBuilderOutputWriter.Factory)StringBuilderOutputWriterFactory);

            registrar.Register <IFakeObjectCallFormatter>(fakeObjectCallFormatter);

            StringBuilderOutputWriter StringBuilderOutputWriterFactory() =>
            new StringBuilderOutputWriter(argumentValueFormatter !);

            FakeManager FakeManagerFactory(Type fakeObjectType, object proxy, string?name) =>
            new FakeManager(fakeObjectType, proxy, name);

            IFakeCallProcessorProvider FakeCallProcessorProviderFactory(Type typeOfFake, IProxyOptions proxyOptions) =>
            new FakeManagerProvider(FakeManagerFactory, fakeManagerAccessor, typeOfFake, proxyOptions);

            ExpressionCallRule ExpressionCallRuleFactory(ParsedCallExpression callSpecification) =>
            new ExpressionCallRule(new ExpressionCallMatcher(callSpecification, expressionArgumentConstraintFactory, methodInfoManager));

            IFakeAsserter FakeAsserterFactory(IEnumerable <CompletedFakeObjectCall> calls, int lastSequenceNumber) =>
            new FakeAsserter(calls, lastSequenceNumber, callWriter, StringBuilderOutputWriterFactory);

            SequentialCallContext SequentialCallContextFactory() =>
            new SequentialCallContext(callWriter, StringBuilderOutputWriterFactory);

            RuleBuilder RuleBuilderFactory(BuildableCallRule rule, FakeManager fake) =>
            new RuleBuilder(rule, fake, FakeAsserterFactory);
        }
Пример #14
0
 /// <summary>
 /// Registers the by type match.
 /// </summary>
 /// <param name="registrar">The registrar.</param>
 /// <param name="basedOnType">Type of the based on.</param>
 /// <param name="predicate">The predicate.</param>
 /// <param name="assemblies">The assemblies.</param>
 public static void RegisterByTypeMatch(this IServiceRegistrar registrar, Type basedOnType, Predicate <Type> predicate, params Assembly[] assemblies)
 {
     RegisterByTypeMatch((serviceType, implementationType, name) => registrar.Register(serviceType, implementationType, name), basedOnType, predicate, assemblies);
 }
 /// <summary>
 /// Configures the consumer.
 /// </summary>
 /// <param name="r">The r.</param>
 /// <param name="type">The type.</param>
 protected virtual void ConfigureConsumer(IServiceRegistrar r, Type type)
 {
     r.Register <IMessageConsumer>(type, type.FullName);
 }
Пример #16
0
 public void Bootstrap(IServiceRegistrar registrar)
 {
     registrar.Register <ILogDestination, ConsoleLogDestination>();
 }
Пример #17
0
 /// <summary>
 /// Registers the by naming convention.
 /// </summary>
 /// <param name="registrar">The registrar.</param>
 /// <param name="predicate">The predicate.</param>
 public static void RegisterByNamingConvention(this IServiceRegistrar registrar, Predicate <Type> predicate)
 {
     RegisterByNamingConvention((serviceType, implementationType) => registrar.Register(serviceType, implementationType), predicate, new[] { GetPreviousCallingMethodAssembly() });
 }
Пример #18
0
 /// <summary>
 /// Registers the by type match.
 /// </summary>
 /// <typeparam name="TBasedOn">The type of the based on.</typeparam>
 /// <param name="registrar">The registrar.</param>
 /// <param name="assemblies">The assemblies.</param>
 public static void RegisterByTypeMatch <TBasedOn>(this IServiceRegistrar registrar, params Assembly[] assemblies)
 {
     RegisterByTypeMatch((serviceType, implementationType, name) => registrar.Register(serviceType, implementationType, name), typeof(TBasedOn), x => DefaultPredicate(registrar, x), assemblies);
 }
Пример #19
0
 public void Bootstrap(IServiceRegistrar registrar)
 {
     registrar.Register <IApplicationEntryPoint, ApplicationEntryPoint>();
 }
Пример #20
0
 /// <summary>
 /// Registers the default mediator.
 /// </summary>
 /// <param name="serviceRegistrar">  The service registrar. </param>
 /// <param name="registrationScope"> The registration scope. </param>
 /// <returns> The <see cref="IServiceRegistrar" /> </returns>
 public static IServiceRegistrar RegisterDefaultMediator(this IServiceRegistrar serviceRegistrar, RegistrationScope registrationScope)
 {
     registrationScope = RegistrationScope.Singleton.Restricted(registrationScope);
     return(serviceRegistrar
            .Register <IMediator, Mediator>(registrationScope));
 }
Пример #21
0
 /// <summary>
 /// Registers the by type match.
 /// </summary>
 /// <typeparam name="TBasedOn">The type of the based on.</typeparam>
 /// <param name="registrar">The registrar.</param>
 public static void RegisterByTypeMatch <TBasedOn>(this IServiceRegistrar registrar)
 {
     RegisterByTypeMatch((serviceType, implementationType, name) => registrar.Register(serviceType, implementationType, name), typeof(TBasedOn), x => DefaultPredicate(registrar, x), new[] { GetPreviousCallingMethodAssembly() });
 }
Пример #22
0
 /// <summary>
 /// Registers the by naming convention.
 /// </summary>
 /// <param name="registrar">The registrar.</param>
 /// <param name="assemblies">The assemblies.</param>
 public static void RegisterByNamingConvention(this IServiceRegistrar registrar, params Assembly[] assemblies)
 {
     RegisterByNamingConvention((serviceType, implementationType) => registrar.Register(serviceType, implementationType), x => DefaultPredicate(registrar, x), assemblies);
 }
Пример #23
0
 /// <summary>
 /// Registers the by type match.
 /// </summary>
 /// <param name="registrar">The registrar.</param>
 /// <param name="basedOnType">Type of the based on.</param>
 /// <param name="predicate">The predicate.</param>
 public static void RegisterByTypeMatch(this IServiceRegistrar registrar, Type basedOnType, Predicate <Type> predicate)
 {
     RegisterByTypeMatch((serviceType, implementationType, name) => registrar.Register(serviceType, implementationType, name), basedOnType, predicate, new[] { GetPreviousCallingMethodAssembly() });
 }