コード例 #1
0
        // public TAggregate Create<TAggregate>(Guid? id = null)
        //     where TAggregate : AggregateRoot<Guid>
        // {
        //     id = id == null || id.Value == Guid.Empty ? Guid.NewGuid() : id;
        //
        //     var existStream = _eventStore.Any(id.Value);
        //     if(existStream)
        //         throw new DuplicatedRootException(id.ToString());
        //
        //     var createEvent = new AggregateCreated<Guid>(id.Value);
        //     var events = new List<IEvent>() { createEvent};
        //
        //     var stream = new EventStream(){ Version = INITIAL_VERSION, Events = events};
        //
        //     TAggregate aggregate = (TAggregate) Activator.CreateInstance(typeof(TAggregate), new object[] {stream});
        //
        //     aggregate.Changes.Add(createEvent);
        //     return aggregate;
        // }


        public override TAggregate Create <TAggregate>(Guid?id = null)
        {
            id = id == null || id.Value == Guid.Empty ? Guid.NewGuid() : id;

            var existStream = _eventStore.Any(id.Value);

            if (existStream)
            {
                throw new DuplicatedRootException(id.ToString());
            }

            var createEvent = new AggregateCreated <Guid>(id.Value);
            var events      = new List <IEvent>()
            {
                createEvent
            };

            var stream = new EventStream()
            {
                Version = INITIAL_VERSION, Events = events
            };

            TAggregate aggregate = (TAggregate)Activator.CreateInstance(typeof(TAggregate), new object[] { stream });

            aggregate.Changes.Add(createEvent);
            return(aggregate);
        }