Ejemplo n.º 1
0
        public void Save(IAggregate aggregate)
        {
            Db4oAggregate dbAggregate = (from Db4oAggregate a in _database
                                         where a.Id == aggregate.Id
                                         select a).FirstOrDefault();

            int version = 0;
            if (dbAggregate == null)
            {
                dbAggregate = new Db4oAggregate { Id = aggregate.Id, Type = aggregate.GetType().Name, Version = 0 };
            }
            else
            {
                version = dbAggregate.Version;
            }

            IEnumerable<IDomainEvent> events = aggregate.GetChanges();

            foreach (IDomainEvent e in events)
            {
                version++;
                var db4oEvent = new Db4oEvent
                                    {
                                        AggregateId = dbAggregate.Id,
                                        Event = e,
                                        Version = version
                                    };
                _database.Store(db4oEvent);
            }

            dbAggregate.Version = version;
            _database.Store(dbAggregate);
        }