2009-09-22 1 views
0

정보 : VS2010, 나는 몇 가지 하위 요소를 추가 내 도메인 클래스 중 하나에 사용자 정의 생성자가 DSL 툴킷, C#을DSL 사용자 정의 생성자 - 전용 전화로드되지 만들 때

. 난 단지 이것이 당신이 일부 도메인 클래스의 속성을 초기화하는 것 같습니다

 public Entity(Partition partition, params PropertyAssignment[] propertyAssignments) 
     : base(partition, propertyAssignments) 
    { 
     if (SOMETHING_TO_STOP_IT_RUNNING_EACH_TIME) 
     { 
      using (Transaction tx = Store.TransactionManager.BeginTransaction("Add Property")) 
      { 
       Property property = new Property(partition); 
       property.Name = "Class"; 
       property.Type = "System.String"; 
       this.Properties.Add(property); 
       this.Version = "1.0.0.0"; // TODO: Implement Correctly 
       tx.Commit(); 
      } 
     } 
    } 

답변

2

도메인 클래스 요소가 생성 될 때합니다 (construtors를 호출) 그림이 열릴하지 때마다 실행하려면 나는이 문제를 가지고 생성자 내에서. 이것은 AddRule을 작성하는 것이 가장 좋습니다. AddRules는 첨부 된 도메인 클래스의 인스턴스가 모델에 추가 될 때 호출됩니다. 예를 들면 :

[RuleOn(typeof(Entity), FireTime = TimeToFire.TopLevelCommit)] 
internal sealed partial class EntityAddRule : AddRule 
{ 
    public override void ElementAdded(ElementAddedEventArgs e) 
    { 
    if (e.ModelElement.Store.InUndoRedoOrRollback) 
     return; 

    if (e.ModelElement.Store.TransactionManager.CurrentTransaction.IsSerializing) 
     return; 

    var entity = e.ModelElement as Entity; 

    if (entity == null) 
     return; 

    // InitializeProperties contains the code that used to be in the constructor 
    entity.InitializeProperties(); 
    } 
} 
AddRule 다음 도메인 모델 클래스에 함수를 재정 의하여 등록 될 필요가

: 만들기 : 규칙에 대한 자세한 내용

public partial class XXXDomainModel 
{ 
    protected override Type[] GetCustomDomainModelTypes() 
    { 
    return new Type[] { 
     typeof(EntityAddRule), 
    } 
    } 
} 

는 "어떻게하기를 보라 사용자 지정 규칙 "항목을 VS SDK 설명서를 참조하십시오.

참고 : 솔루션은 VS 2008 DSL 도구를 기반으로합니다. YMMV.

+0

감사합니다 Paul, 당신의 대답에 대해. 나는 지금 어떤 테스트를 할 것입니다! –

+0

매우 효과적입니다. DSL에서 배워야 할 것이 많지만 노력이 필요하다는 것을 알고 있습니다. –

+0

반 측면 질문. 다이어그램이 생성 될 때 (Project> Add Item) 생성자를 사용해야한다면 여기에서와 같은 패턴을 사용해야합니까? 감사 –

0

하지 올바른 접근 방식 (=로드) 모델이 연재되고있는 경우, 여기, 주어진 시간에, 아시 방법 (폴 라 론데의 대답은 최고입니다)하지만 :

this.Store.TransactionManager.CurrentTransaction!= null && 
this.Store.TransactionManager.CurrentTransaction.IsSerializing 
관련 문제