通过业务逻辑初始化 Linq to Sql 对象

发布于 2024-07-15 13:36:57 字数 581 浏览 7 评论 0原文

我想扩展 Linq To Sql 生成的类,以在创建新对象(=行)时进行初始化。

我想在创建父行时向子表添加行。

我希望使用 Oncreated (部分)方法做这样的事情:

partial class Product {
    partial void OnCreated() {
        // Fill default rows for FK relations
        this.Columns.Add(new ProductColumn {
            Name = "Name", ColumnType = 1
        });
        this.Columns.Add(new ProductColumn {
            Name = "Producer", ColumnType = 2
        });
    }
}

每次从构造函数中调用 OnCreated 。 如果在调用 OnCreated 之后从数据库加载对象也是如此。 而如果对象是从数据库加载的,则不想执行代码。

那么我可以在哪里向模型添加逻辑来初始化对象(-graph)?

I would like to extend a class generated by Linq To Sql to initialize when a new object (=row) is created.

I want to add rows to a child table when a parent row is created.

I was hoping to use the Oncreated (partial) method do something like this:

partial class Product {
    partial void OnCreated() {
        // Fill default rows for FK relations
        this.Columns.Add(new ProductColumn {
            Name = "Name", ColumnType = 1
        });
        this.Columns.Add(new ProductColumn {
            Name = "Producer", ColumnType = 2
        });
    }
}

The OnCreated is called every time from the constructor. So also if the object will be loaded from the database after the call to OnCreated. And if the object is loaded from the database, do not want to execute the code.

So where can I add logic to my model to initialize an object(-graph)?

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

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

发布评论

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

评论(4

赠我空喜 2024-07-22 13:36:57

Linq to SQL 生成的类中没有内置方法。 当从数据库调用时,它们专门调用 OnLoaded,但当不是从数据库调用时,它们不会调用任何内容。 我怀疑是因为没有办法确定......

我建议使用克里斯建议的工厂方法。

There's no built in method for it in the Linq to SQL generated classes. They call OnLoaded specifically when called from the DB, but they do not call anything when it's not. I suspect because there's no way to know for sure...

I'd recommend using the factory approach that Chris suggested.

执笏见 2024-07-22 13:36:57

使用 LINQ-to-SQL,实体很大程度上不知道父数据上下文 - 只有像EntitySet这样的延迟加载成员知道这个细节。 您可以通过查看 .IsDeferred 来识别数据上下文感知实体集(假设它被推迟) - 如果涉及数据上下文,则为 true,否则为 false否则 - 但还有其他问题:

  • LINQ-to-SQL 在构造函数(和 OnCreated)执行之后附加数据上下文(用于延迟执行)
  • 如果添加项目在 OnCreated/.ctor() 中手动执行,当 LINQ-to-SQL 尝试附加数据上下文时,它会中断
  • LINQ-to-SQL 使用默认构造函数,这意味着generics/new() 等也将共享此代码

所以不幸的是; 不,我认为您不能以任何明智的方式在 OnCreated 中执行此操作。 您可以:

  • 为此使用第二个构造函数,添加额外的项目,
  • 使用工厂方法,即正确构建项目的静态 .Create() 方法

(因为 LINQ-to-SQL 不会使用上述任何一个本身)

With LINQ-to-SQL, the entities are largely unaware of the parent data-context - only lazy-loaded members like EntitySet<T> know about this detail. You can identify data-context aware entity-sets by looking at .IsDeferred (assuming it is deferred) - this will be true if a data-context is involved, and false otherwise - but there are further problems:

  • LINQ-to-SQL attaches the data-context (for deferred execution) after the constructur (and OnCreated) has executed
  • If you add items manually in OnCreated/.ctor(), it breaks when LINQ-to-SQL attempts to attach the data-context
  • LINQ-to-SQL uses the default constructor, meaning that generics/new() etc will also share this code

So unfortunately; no, I don't think you can do this in OnCreated in any sensible way. You could:

  • have a second constructor for this use, that adds the extra items
  • use a factory method, i.e. a static .Create() method that builds items correctly

(since LINQ-to-SQL won't use either of the above itself)

時窥 2024-07-22 13:36:57

我认为在这种情况下你最好使用工厂来创建你的实例; 因此,当前任何地方都有如下代码:

Product p = new Product();

您将改为:

Product p = ProductFactory.CreateProduct();

并且您的 CreateProduct() 方法将类似于:

public Product CreateProduct()
{
    var p = new Product();
    p.Columns.Add(new ProductColumn {
        Name = "Name", ColumnType = 1
    });
    p.Columns.Add(new ProductColumn {
        Name = "Producer", ColumnType = 2
    });
    return p;
}

I think you'd be better off in this instance to use a Factory to create your instances; So anywhere you current have code like:

Product p = new Product();

You would instead have:

Product p = ProductFactory.CreateProduct();

And your CreateProduct() method would look something like:

public Product CreateProduct()
{
    var p = new Product();
    p.Columns.Add(new ProductColumn {
        Name = "Name", ColumnType = 1
    });
    p.Columns.Add(new ProductColumn {
        Name = "Producer", ColumnType = 2
    });
    return p;
}
埋葬我深情 2024-07-22 13:36:57

如果您正在使用设计器 - 我认为您是因为您正在使用部分类 - 您可以在设计器中添加关联,并且相应的列将自动添加到您的对象中。 MSDN 有关于如何使用设计器工作界面创建关联的参考。

If you are using the designer -- and I presume you are because you are using partial classes -- you can add the association in the designer and the appropriate columns will get added to your object automatically. MSDN has a reference on how to create associations using the designer work surface.

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