基本上,我有这个类:
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.Files 返回延迟加载的集合并保持 StagedPromotion 的 Files 属性完好无损?
这个想法是创建一个版本控制系统,每个我都可以跟踪已提交/未提交的促销活动。