2011-04-07 82 views
0

您好我正在努力改進/重構一個領域模型,並試圖將邏輯從應用程序服務移動到我的領域模型。現在我陷入了一個NHibernate問題。流利Nhibernate映射 - 價值對象內的一對多?

該模型是一個WorkEvaluation類,它包含一個包含Question的Questionaire模板,它還包含一個QuestionWeight類的集合。問題在於WorkEvaluation類也有一個重要的屬性,它屬於WorkEvaluation中的QuestionWeight集合。這個概念是你通過回答很多問題來進行評估(在這個例子中不包括anserws),最後你應用一些修改答案分數的權重(百分比權重)。這意味着你可以提出一些更重要的問題和其他不太重要的問題。命中間隔也是您在計算TOTAL WorkEvaluation得分(包括權重修改)時使用的調整參數,結果爲例如:Totalscore = 100,Hitinterval 5%比我們得到95-105的總區間並可用於匹配其他評估。

足夠的背景。 我想將QuestionWeights和HitInterval的列表封裝在一個Value對象QuestionScoreTuning中,因爲它們屬於一起並且應該同時應用。 而且我還希望將一些業務邏輯添加到不屬於workEvaluation的QuestionScoreTuning中。 我如何映射我的流利Nhibernate價值對象(組件)具有一對多的集合和HitInterval和參考回來?這是我當前的代碼:

public class WorkEvaluation : DomainBase<long>, IAggregateRoot 
{ 
public void ApplyTuning(QuestionScoreTuning tuning) 
     { 
      QuestionScoreTuning = tuning; 
      //TODO Raise Domain Event WorkEvaluationCompleted - 
      // which should recalculate all group scores 
     } 
public QuestionScoreTuning QuestionScoreTuning { get; protected set; } 
} 

public class QuestionScoreTuning : ValueObject 
    { 
     private IList<QuestionWeight> _questionWeights; 

     public QuestionScoreTuning(IList<QuestionWeight> listOfWeights, long hitInterval) 
     { 
      _questionWeights = listOfWeights; 
      HitInterval = hitInterval; 
     } 

     public long HitInterval { get; protected set; } 

     protected override IEnumerable<object> GetAtomicValues() 
     { 
      return _questionWeights.Cast<object>(); 
     } 

     /// <summary> 
     /// A list of all added QuestionWeights for this WorkEvaluation 
     /// </summary> 
     public IList<QuestionWeight> QuestionWeights 
     { 
      get { return new List<QuestionWeight>(_questionWeights); } 
      protected set { _questionWeights = value; } 
     } 

     protected QuestionScoreTuning() 
     {} 
    } 

public class QuestionWeight : DomainBase<long>, IAggregateRoot 
{ 
    public QuestionWeight(Question question, WorkEvaluation evaluation) 
    { 
     Question = question; 
     WorkEvaluation = evaluation; 
    } 

    public Weight Weight { get; set; } 
    public Question Question { get; protected set; } 
    public WorkEvaluation WorkEvaluation { get; protected set; } 

    public override int GetHashCode() 
    { 
     return (Question.GetHashCode() + "|" + Weight).GetHashCode(); 
    } 

    protected QuestionWeight() 
    {} 
} 

流利的映射:

public class WorkEvaluationMapping : ClassMap<WorkEvaluation> 
    { 
     public WorkEvaluationMapping() 
     { 
      Id(x => x.ID).GeneratedBy.Identity(); 
      References(x => x.SalaryReview).Not.Nullable(); 
      References(x => x.WorkEvaluationTemplate).Column("WorkEvaluationTemplate_Id").Not.Nullable(); 
      Component(x => x.QuestionScoreTuning, m => 
                 { 
                  m.Map(x => x.HitInterval, "HitInterval"); 
                  m.HasMany(x => x.QuestionWeights).KeyColumn("WorkEvaluation_id").Cascade.All(); 
                 }); 

      } 
    } 

public class QuestionWeightMapping : ClassMap<QuestionWeight> 
    { 
     public QuestionWeightMapping() 
     { 
      Not.LazyLoad(); 
      Id(x => x.ID).GeneratedBy.Identity(); 
      Component(x => x.Weight, m => 
             { 
              m.Map(x => x.Value, "WeightValue"); 
              m.Map(x => x.TypeOfWeight, "WeightType"); 
             }); 
      References(x => x.Question).Column("Question_id").Not.Nullable().UniqueKey(
       "One_Weight_Per_Question_And_WorkEvaluation"); 
      References(x => x.WorkEvaluation).Column("WorkEvaluation_id").Not.Nullable().UniqueKey(
       "One_Weight_Per_Question_And_WorkEvaluation"); 
     } 
    } 

我所要完成的任務是QuestionWeights和HitInterval的集合移動到一個值對象(組件映射),因爲這些仍然會裏數據庫表WorkEvaluation。

P.S我看了一些示例解決方案DDDSample.net(埃裏克埃文斯DDD在c#中的例子),他們完成了這與行列類,它採取列表作爲ctor參數,並被映射爲一個貨運組件。區別在於該示例有一個valueobjects的列表,而Leg Leg具有對實體類的位置的引用。

希望也許有人知道如何做到這一點。在此先感謝... /Bacce

回答

0

好吧。我終於解決了它。現在我的WorkEvaluation對象可以與包含重量和命中間隔列表的QuestionScoreTuning對象(一個valueobject)一起使用。這真是太好了,如果有人想要更多有關在value對象中有集合的信息並將它們映射到流利的NH中,請在這裏詢問一下評論。我可以提供代碼示例...