Beispiel #1
0
        public virtual void Save(T t)
        {
            var agg = GetById(t.ID);

            agg.EnsureNotNull();

            var aggVersion = agg.Version;

            Monitor.Enter(_lockObj);

            using (UOW)
            {
                try
                {
                    //保存未执行的事件
                    foreach (var @event in agg.UnCommitEvents)
                    {
                        aggVersion++;
                        @event.Version = aggVersion;
                        EventService.SaveEvent(@event);
                    }

                    UOW.Commit();
                }
                catch
                {
                    UOW.RollBack();
                }
            }

            //发布消息,通知聚合根执行事件,并更改内存中的聚合根为最新状态
            EventService.Publish(agg.UnCommitEvents);

            Monitor.Exit(_lockObj);
        }