是否有可能重载NHibernate创建的延迟集合?

时间:2013-02-07 16:06:51

标签: c# .net nhibernate

基本上,我有这个课程:

   public class Promotion : Entity<int>
        {
            public Promotion()
            {
                Created = DateTime.Now;
                this.files = new HashedSet<PromoImage>();
            }

            public Promotion(int id, string Name, DateTime created, DateTime from, DateTime to, ISet<PromoImage> files)
            {
                this.Id = id;
                this.Created = created;
                this.From = from;
                this.To = to;
                this.files = files;
            }

            public Promotion(string Name, DateTime created,DateTime from, DateTime to, ISet<PromoImage> files)
            {
                this.Created = created;
                this.From = from;
                this.To = to;
                this.files = files;
            }

            public virtual DateTime Created { get; protected set; }

            public virtual string Name { get; protected set; }

            public virtual DateTime? From { get; protected set; }

            public virtual DateTime? To { get; protected set; }

            private ISet<PromoImage> files;

            public IEnumerable<PromoImage> Files
            {
                get
                {
                    return files;
                }
            }

        public virtual bool AddPromoImage(PromoImage newPromoImage)
        {
            if (newPromoImage != null && files.Add(newPromoImage))
            {
                return true;
            }
            return false;
        }

        public virtual bool RemovePromoImage(PromoImage promoImage)
        {
            if (promoImage != null && files.Remove(promoImage))
            {
                return true;
            }
            return false;
        }

            public virtual bool ChangePhotoPositions(IEnumerable<Row> rows)
            {
                foreach (var row in rows)
                {
                    List<PromoImage> photos = Files.Where(p => row.PhotoIdAndPosition.Any(q => q.First == p.Id)).ToList();
                    int totalWidth = 0;
                    int lastHeight = photos[0].Image.Height;
                    bool allPhotosHaveSameHeight = true;
                    foreach (var photo in photos)
                    {
                        allPhotosHaveSameHeight = lastHeight == photo.Image.Height;
                        totalWidth += photo.Image.Width;
                        lastHeight = photo.Image.Height;
                    }
                    if (totalWidth > 734 && !allPhotosHaveSameHeight)
                    {
                        return false;
                    }
                    else
                    {
                        foreach (var photo in photos)
                        {
                            var newPosition = row.PhotoIdAndPosition.Single(p => p.First == photo.Id).Second;
                            photo.Row = row.Level;
                            photo.Position = newPosition;
                        }
                    }
                }
                return true;
            }

            public virtual bool SetPromotionDateRange(DateTime from, DateTime to)
            {
                if (from > DateTime.Now)
                {
                    if (from > to)
                    {
                        From = from;
                        To = to;
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                    return false;
            }
        }

此类将Promotion对象的任何更改存储为持久事件:

 public class StagedPromotion:Promotion
    {
        public StagedPromotion():base()
        {
            Changes = new HashedSet<Change>();
        }

        public virtual DateTime? CommitedWhen { get; protected set; }

        public virtual ISet<Change> Changes { get; protected set; }

        public virtual Promotion WorkingPromotion 
        {
            get
            {
                Promotion promotion = new Promotion(this.Name, this.Created, this.From.Value, this.To.Value, this.files);
                foreach (var change in Changes)
                {
                    change.ExecuteChange(promotion);
                }
                return promotion;
            }
        }
        public virtual bool ChangePhotoPositions(IEnumerable<Row> rows)
        {
            var promo = WorkingPromotion;
            if (promo.ChangePhotoPositions(rows))
            {
                Change change = new ChangeRowAndPosition(new HashedSet<Row>(rows.ToList()));
                Changes.Add(change);
                return true;
            }
            else
                return false;
        }

        public virtual bool SetPromotionDateRange(DateTime from, DateTime to)
        {
            var promo = WorkingPromotion;
            if (promo.SetPromotionDateRange(from, to))
            {
                Change change = new ChangePromotionDate(from, to);
                Changes.Add(change);
                return true;
            }
            else
                return false;
        }

        public virtual bool AddPromoImage(PromoImage newPromoImage)
        {
           //todo
        }

        public virtual bool RemovePromoImage(PromoImage promoImage)
        {
           //todo
        }

        public virtual IEnumerable<PromoImage> Files
        {
            get
            {
                var files =  base.Files;
                //replace each PromoImage object with a StagedPromoImage Decorator                
                //which registers any changes to the PromoImage as persistent events
            }
        }
    }

NHibernate是否可以通过base返回。修复了延迟加载的集合并保持StagedPromotion的Files属性不变?

我们的想法是创建一个版本控制系统,每个版本都可以跟踪提交/未提交的促销活动。

1 个答案:

答案 0 :(得分:0)

你将base.Files称为nhibernate会管理的东西,但没有办法按原样映射它。为了让nhibernate提供一个惰性集合,该属性必须是虚拟的,所以你不能拥有你的阶段性促销文件属性而不覆盖基本属性。