DSL 自定义构造函数 - 仅在创建时调用而不加载

发布于 2024-08-05 22:05:55 字数 781 浏览 12 评论 0原文

信息:VS2010、DSL Toolkit、C#

我的域类之一有一个自定义构造函数,它添加了一些子元素。我有一个问题,因为我只希望在创建域类元素时运行它,而不是每次打开图(调用构造函数)时运行

        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();
            }
        }
    }

Info: VS2010, DSL Toolkit, C#

I have a custom constructor on one of my domain classes which adds some child elements. I have an issue as I only want this to run when the domain class element is created , not every time the diagram is opened (which calls the construtors)

        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();
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷默言语 2024-08-12 22:05:55

看起来您正在从构造函数中初始化一些域类属性。最好通过创建 AddRule 来完成此操作。当 AddRules 所附加的域类的实例添加到模型中时,将调用 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 Tools。 YMMV。

It looks like you are initializing some domain class properties from within the constructor. This is best done by creating an AddRule. AddRules are invoked when an instance of the domain class to which they are attached is added to the model. For example :

[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();
  }
}

The AddRule then needs to be registered by overriding a function in your domain model class:

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

For more information about rules, have a look at the "How to: Create Custom Rules" topic in the VS SDK documentation.

Note: the solution is based on the VS 2008 DSL Tools. YMMV.

静水深流 2024-08-12 22:05:55

虽然不是正确的方法(Paul Lalonde 的答案是最好的),
您可以通过以下方式知道在任何给定时间模型是否正在序列化(= 加载):

this.Store.TransactionManager.CurrentTransaction!= null &&
this.Store.TransactionManager.CurrentTransaction.IsSerializing

Although not the correct approach (Paul Lalonde answer is the best),
here's how you may know, at any given time, if the model is being serialized (= loading):

this.Store.TransactionManager.CurrentTransaction!= null &&
this.Store.TransactionManager.CurrentTransaction.IsSerializing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文