基本的に、私はこのクラスを持っています:
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 プロパティをそのままにしておくことは可能ですか?
アイデアは、コミットされた/コミットされていないプロモーションを追跡できるバージョン管理システムを作成することです。