コード例 #1
0
        public void UseDependencies_can_be_used_to_set_dependencies_using_an_application_owned_container()
        {
            var applicationsContainer = new PocketContainer()
                .Register<IPaymentService>(_ => new CreditCardPaymentGateway(chargeLimit: 1));

            Configuration.Current
                         .UseDependencies(type =>
                         {
                             if (applicationsContainer.Any(reg => reg.Key == type))
                             {
                                 return () => applicationsContainer.Resolve(type);
                             }

                             return null;
                         });

            var order = new Order(new CreateOrder(Any.FullName()))
                .Apply(new AddItem
                       {
                           Price = 5m,
                           ProductName = Any.Word()
                       })
                .Apply(new Ship())
                .Apply(new ChargeAccount
                       {
                           AccountNumber = Any.PositiveInt().ToString()
                       });

            order.Events()
                 .Last()
                 .Should()
                 .BeOfType<Order.PaymentConfirmed>();
        }
        private static void AddFactoryToList <T>(
            PocketContainer container,
            Func <PocketContainer, T> factory)
        {
            // avoid re-entrancy which would result in a stack overflow
            if (recursionCounter.Value != 0)
            {
                return;
            }

            try
            {
                recursionCounter.Value++;

                // register IEnumerable<Func<PocketContainer, T>>
                container.TryRegister(c => c.Resolve <List <Func <PocketContainer, T> > >()
                                      .Select(f => f(c)));

                // register the registration list as a singleton
                container.TryRegisterSingle(c => new List <Func <PocketContainer, T> >());

                // resolve it and add the factory
                var registrations = container.Resolve <List <Func <PocketContainer, T> > >();

                registrations.Add(factory);
            }
            finally
            {
                recursionCounter.Value--;
            }
        }
コード例 #3
0
        internal static Func<PocketContainer, object> InMemoryEventSourcedRepositoryStrategy(Type type, PocketContainer container)
        {
            if (type.IsGenericType &&
                (type.GetGenericTypeDefinition() == typeof (IEventSourcedRepository<>) ||
                 type.GetGenericTypeDefinition() == typeof (InMemoryEventSourcedRepository<>)))
            {
                var aggregateType = type.GenericTypeArguments.Single();
                var repositoryType = typeof (InMemoryEventSourcedRepository<>).MakeGenericType(aggregateType);

                var streamName = AggregateType.EventStreamName(aggregateType);

                // get the single registered event stream instance
                var stream = container.Resolve<ConcurrentDictionary<string, IEventStream>>()
                                      .GetOrAdd(streamName,
                                                name => container.Clone()
                                                                 .Register(_ => name)
                                                                 .Resolve<IEventStream>());

                return c => Activator.CreateInstance(repositoryType, stream, c.Resolve<IEventBus>());
            }

            if (type == typeof(IEventStream))
            {
                return c => c.Resolve<InMemoryEventStream>();
            }

            return null;
        }
コード例 #4
0
        public CommandSchedulerResolver(PocketContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            schedulerResolversByAggregateTypeName = new Dictionary<string, Func<dynamic>>();

            Command.KnownTargetTypes.ForEach(aggregateType =>
            {
                var schedulerType = typeof (ICommandScheduler<>).MakeGenericType(aggregateType);

                schedulerResolversByAggregateTypeName.Add(
                    AggregateType.EventStreamName(aggregateType),
                    () => container.Resolve(schedulerType));
            });
        }
コード例 #5
0
 /// <summary>
 ///     Retrieves a service from the scope.
 /// </summary>
 /// <returns>
 ///     The retrieved service.
 /// </returns>
 /// <param name="serviceType">The service to be retrieved.</param>
 public object GetService(Type serviceType)
 {
     try
     {
         return(container.Resolve(serviceType));
     }
     catch (Exception exception)
     {
         if (IsFrameworkType(serviceType))
         {
             if ((exception is TargetInvocationException &&
                  exception.InnerException is ArgumentException) ||
                 exception is ArgumentException)
             {
                 return(null);
             }
         }
         if (exception is TargetInvocationException)
         {
             throw exception.InnerException;
         }
         throw;
     }
 }
コード例 #6
0
        internal static ICommandSchedulerDispatcher[] InitializeSchedulersPerAggregateType(
            PocketContainer container,
            GetClockName getClockName,
            ISubject<ICommandSchedulerActivity> subject)
        {
            var binders = AggregateType.KnownTypes
                                       .Select(aggregateType =>
                                       {
                                           var initializerType =
                                               typeof (SchedulerInitializer<>).MakeGenericType(aggregateType);

                                           dynamic initializer = container.Resolve(initializerType);

                                           return (ICommandSchedulerDispatcher) initializer.InitializeScheduler(
                                               subject,
                                               container,
                                               getClockName);
                                       })
                                       .ToArray();
            return binders;
        }