예제 #1
0
 public void SetUp()
 {
     BootStrapper.BootUp(this, null);
     _service = NcqrsEnvironment.Get <ICommandService>();
     _service.Execute(new CreateNewNote());
     _rand = new Random(DateTime.Now.Millisecond);
 }
예제 #2
0
        public static void BootUp()
        {
            // initialize the config system
            Config.Config.LoadFromXml(Config.Config.DefaultPath);

            NcqrsEnvironment.SetDefault <ReadModel.IReadModelStore>(InitializeReadModelStore());
        }
예제 #3
0
파일: MainTests.cs 프로젝트: zonaid/ncqrs
        public void LoadItUp()
        {
            IList <Guid> ids = new List <Guid>();

            for (int i = 0; i < 5; i++)
            {
                Guid id = Guid.NewGuid();
                ids.Add(id);

                NcqrsEnvironment.Get <ICommandService>().Execute(new Env.CreateNoteCommand()
                {
                    NoteId   = id,
                    NoteText = "Hello world " + i
                });
            }


            foreach (Guid id in ids)
            {
                NcqrsEnvironment.Get <ICommandService>().Execute(new Env.ChangeNoteCommand()
                {
                    NoteId      = id,
                    NewNoteText = "Hello solar system"
                });
            }

            foreach (Guid id in ids)
            {
                NcqrsEnvironment.Get <ICommandService>().Execute(new Env.ChangeNoteCommand()
                {
                    NoteId      = id,
                    NewNoteText = "Hello galaxy"
                });
            }
        }
예제 #4
0
        protected override void When()
        {
            ExecutedCommand = WhenExecuting();
            var cmdService = NcqrsEnvironment.Get <ICommandService>();

            cmdService.Execute(ExecutedCommand);
        }
        public void Map(MapsToAggregateRootMethodAttribute attribute, ICommand command, IMappedCommandExecutor executor)
        {
            var commandType = command.GetType();

            ValidateCommandType(commandType);

            var match = GetMatchingMethod(attribute, commandType, attribute.MethodName);

            Action <AggregateRoot, ICommand> action =
                (agg, cmd) =>
            {
                var parameter = match.Item2.Select(p => p.GetValue(cmd, null));
                match.Item1.Invoke(agg, parameter.ToArray());
            };

            Action executorAction = () => executor.ExecuteActionOnExistingInstance(GetAggregateRootId, GetAggregateRootType, action);

            if (commandType.IsDefined(typeof(TransactionalAttribute), false))
            {
                var transactionService = NcqrsEnvironment.Get <ITransactionService>();
                transactionService.ExecuteInTransaction(executorAction);
            }
            else
            {
                executorAction();
            }
        }
예제 #6
0
        private void RecordGivenEvents()
        {
            var store            = NcqrsEnvironment.Get <IEventStore>();
            var givenEventStream = ConvertGivenEvents();

            store.Store(givenEventStream);
        }
예제 #7
0
 protected override void SetupDependencies()
 {
     base.SetupDependencies();
     GenerateEventSourceId();
     RecordGivenEvents();
     CommandService = NcqrsEnvironment.Get <ICommandService>();
 }
예제 #8
0
        public void Map(MapsToAggregateRootConstructorAttribute attribute, ICommand command, IMappedCommandExecutor executor)
        {
            var commandType = command.GetType();

            ValidateCommandType(commandType);

            var match = GetMatchingConstructor(attribute, commandType);
            Func <ICommand, AggregateRoot> create = (c) =>
            {
                var parameter = match.Item2.Select(p => p.GetValue(c, null));
                return((AggregateRoot)match.Item1.Invoke(parameter.ToArray()));
            };

            Action executorAction = () => executor.ExecuteActionCreatingNewInstance(create);

            if (commandType.IsDefined(typeof(TransactionalAttribute), false))
            {
                var transactionService = NcqrsEnvironment.Get <ITransactionService>();
                transactionService.ExecuteInTransaction(executorAction);
            }
            else
            {
                executorAction();
            }
        }
예제 #9
0
 public void Send(object payload)
 {
     _message.Payload    = payload;
     _message.SenderId   = _senderId;
     _message.SenderType = GetType();
     _message.MessageId  = NcqrsEnvironment.Get <IUniqueIdentifierGenerator>().GenerateNewId();
     _sendAction(_message);
 }
예제 #10
0
        protected override void InitializeEnvironment()
        {
            var store = new MsSqlServerEventStore(GetConnectionString());

            NcqrsEnvironment.SetDefault <ISnapshotStore>(store);
            NcqrsEnvironment.SetDefault <IEventStore>(store);
            NcqrsEnvironment.SetDefault <IUnitOfWorkFactory>(new UnitOfWorkFactory());
        }
예제 #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EventSource"/> class.
        /// </summary>
        protected EventSource()
        {
            InitialVersion = 0;

            var idGenerator = NcqrsEnvironment.Get <IUniqueIdentifierGenerator>();

            EventSourceId = idGenerator.GenerateNewId();
        }
예제 #12
0
파일: Event.cs 프로젝트: vikuznetsov/ncqrs
        /// <summary>
        /// Initializes a new instance of the <see cref="Event"/> class.
        /// </summary>
        public Event()
        {
            var idGenerator = NcqrsEnvironment.Get <IUniqueIdentifierGenerator>();
            var clock       = NcqrsEnvironment.Get <IClock>();

            EventIdentifier = idGenerator.GenerateNewId();
            EventTimeStamp  = clock.UtcNow();
            EventVersion    = DefaultVersion;
        }
 public void Public_event_handlers_should_be_mapped()
 {
     using (var work = NcqrsEnvironment.Get <IUnitOfWorkFactory>().CreateUnitOfWork(Guid.NewGuid()))
     {
         var target = new TheAggregateRoot();
         target.ApplyEvent(new EventForPublicMethod());
         target.OnEventForPublicMethodInvokedCount.Should().Be(1);
     }
 }
예제 #14
0
 public void SetUp()
 {
     _aggregateRootId = Guid.NewGuid();
     _eventStore      = MockRepository.GenerateMock <IEventStore>();
     NcqrsEnvironment.SetDefault(_eventStore);
     _receivingStrategy = MockRepository.GenerateMock <IReceivingStrategy>();
     _sut = new MessageService();
     _sut.UseReceivingStrategy(new ConditionalReceivingStrategy(x => true, _receivingStrategy));
 }
예제 #15
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonEventFormatter"/> class
        /// with a given type resolver.
        /// </summary>
        /// <param name="typeResolver">The <see cref="IEventTypeResolver"/> to use
        /// when resolving event types/names.</param>
        /// <exception cref="ArgumentNullException"><paramref name="typeResolver"/> is <value>null</value>.</exception>
        public JsonEventFormatter(IEventTypeResolver typeResolver)
        {
            Contract.Requires <ArgumentNullException>(typeResolver != null, "typeResolver");

            _typeResolver = typeResolver;
            _serializer   = new JsonSerializer {
                ContractResolver = NcqrsEnvironment.Get <IContractResolver>()
            };
        }
예제 #16
0
        /// <summary>
        /// Used by WCF to enumerate all types inheriting from <see cref="CommandBase"/>,
        /// so that instances of them can be sent to a service operation that expects a command.
        /// </summary>
        /// <returns>An enumeration of all classes in the current AppDomain inheriting from <see cref="CommandBase" /></returns>
        public static IEnumerable <Type> GetKnownTypes()
        {
            var knownCommandsEnumerator = NcqrsEnvironment.Get <IKnownCommandsEnumerator>();

            if (knownCommandsEnumerator == null)
            {
                throw new InvalidOperationException("No Ncqrs.Commanding.IKnownCommandsEnumerator implementation registered with the NcqrsEnvironment.");
            }
            return(knownCommandsEnumerator.GetAllCommandTypes());
        }
예제 #17
0
        public void It_should_initialize_with_a_new_id_given_by_the_generator_from_the_environment()
        {
            var generator = MockRepository.GenerateMock <IUniqueIdentifierGenerator>();

            NcqrsEnvironment.SetDefault <IUniqueIdentifierGenerator>(generator);

            var theAggregate = new MyAggregateRoot();

            generator.AssertWasCalled(g => g.GenerateNewId());
        }
예제 #18
0
 public void Process(object message)
 {
     using (var work = NcqrsEnvironment.Get <IUnitOfWorkFactory>().CreateUnitOfWork())
     {
         var incomingMessage     = ReceiveMessage(message);
         var targetAggregateRoot = GetReceiver(work, incomingMessage);
         targetAggregateRoot.ProcessMessage(incomingMessage);
         work.Accept();
     }
 }
예제 #19
0
        public void Constructing_a_new_event_base_it_should_call_the_GenerateNewId_method_from_the_generator_that_has_been_set_in_the_environment()
        {
            var generator = MockRepository.GenerateMock <IUniqueIdentifierGenerator>();

            NcqrsEnvironment.SetDefault <IUniqueIdentifierGenerator>(generator);

            var mock = MockRepository.GenerateStub <Event>();

            generator.AssertWasCalled(g => g.GenerateNewId());
        }
예제 #20
0
        public void When_get_is_called_when_the_environmemt_is_not_configured_defaults_should_still_be_returned()
        {
            NcqrsEnvironment.Deconfigure();

            var defaultClock = new DateTimeBasedClock();

            NcqrsEnvironment.SetDefault <IClock>(defaultClock);

            NcqrsEnvironment.Get <IClock>().Should().Be(defaultClock);
        }
예제 #21
0
        public void Setting_a_default_should_multiple_times_should_not_throw_an_exception()
        {
            var defaultFoo    = MockRepository.GenerateMock <IFoo>();
            var newDefaultFoo = MockRepository.GenerateMock <IFoo>();

            NcqrsEnvironment.SetDefault <IFoo>(defaultFoo);
            NcqrsEnvironment.SetDefault <IFoo>(newDefaultFoo);
            NcqrsEnvironment.SetDefault <IFoo>(defaultFoo);
            NcqrsEnvironment.SetDefault <IFoo>(newDefaultFoo);
        }
예제 #22
0
        protected override void ExecuteInContext(IUnitOfWorkContext context, AddInvoiceItem command)
        {
            ITaxRepository taxRepo = NcqrsEnvironment.Get <ITaxRepository>();
            Tax            tax     = taxRepo.FindByCode(command.TaxCode, command.OwnerId);

            Invoice invoice = context.GetById <Invoice>(command.InvoiceId, command.KnownVersion);

            invoice.AddInvoiceItem(command.ItemId, command.Description, command.Quantity, command.Price, command.DiscountInPercent, tax, command.OwnerId, command.UserName);
            context.Accept();
        }
예제 #23
0
        public static void Configure()
        {
            if (NcqrsEnvironment.IsConfigured)
            {
                return;
            }
            var cfg = new Configuration();

            NcqrsEnvironment.Configure(cfg);
        }
예제 #24
0
        public Note(Guid id, string text) : base(id)
        {
            var clock = NcqrsEnvironment.Get <IClock>();

            this.ApplyEvent(new NoteAdded
            {
                Id           = id,
                Text         = text,
                CreationDate = clock.UtcNow()
            });
        }
        public void Methods_marked_as_no_event_handler_should_not_be_mapped()
        {
            using (var work = NcqrsEnvironment.Get <IUnitOfWorkFactory>().CreateUnitOfWork(Guid.NewGuid()))
            {
                var target = new TheAggregateRoot();

                Action act = () => target.ApplyEvent(new EventForNoEventHandlerMethod());

                act.ShouldThrow <EventNotHandledException>();
            }
        }
        public void Method_with_a_wrong_method_name_should_not_be_mapped()
        {
            using (var work = NcqrsEnvironment.Get <IUnitOfWorkFactory>().CreateUnitOfWork(Guid.NewGuid()))
            {
                var target = new TheAggregateRoot();

                Action act = () => target.ApplyEvent(new EventForMethodWithWrongMethodName());

                act.ShouldThrow <EventNotHandledException>();
            }
        }
예제 #27
0
        public static void BootUp()
        {
            var config = new StructureMapConfiguration(c =>
            {
                c.For <ICommandService>().Use(BuildCommandService);
                c.For <IEventStore>().Use(BuildEventStore);
                c.For <IEventBus>().Use(BuildEventBus);
            });

            NcqrsEnvironment.Configure(config);
        }
예제 #28
0
        public static void BootUp()
        {
            var config = new StructureMapConfiguration(cfg =>
            {
                cfg.For <ICommandService>().Use(InitializeCommandService);
                cfg.For <IEventBus>().Use(InitializeEventBus);
                cfg.For <IEventStore>().Use(InitializeEventStore);
            });

            NcqrsEnvironment.Configure(config);
        }
예제 #29
0
        public static void Start()
        {
            NcqrsEnvironment.SetDefault <IEventStore>(new TableOnlyStore("MainTest"));
            //NcqrsEnvironment.SetDefault<IEventStore>(new MsSqlServerEventStore(@"Server=.\SQLExpress;Initial Catalog=MyNotesEventStore;Integrated Security=SSPI"));
            CommandService c = new CommandService();

            c.RegisterExecutorsInAssembly(typeof(CreateNoteCommand).Assembly);


            NcqrsEnvironment.SetDefault <ICommandService>(c);
        }
예제 #30
0
        public Note(Guid noteId, String text) : base(noteId)
        {
            var clock = NcqrsEnvironment.Get <IClock>();

            ApplyEvent(new NewNoteAddedEvent
            {
                NoteId       = noteId,
                Text         = text,
                CreationDate = clock.UtcNow()
            });
        }