通过业务逻辑初始化 Linq to Sql 对象
我想扩展 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.
使用 LINQ-to-SQL,实体很大程度上不知道父数据上下文 - 只有像
EntitySet
这样的延迟加载成员知道这个细节。 您可以通过查看.IsDeferred
来识别数据上下文感知实体集(假设它被推迟) - 如果涉及数据上下文,则为 true,否则为 false否则 - 但还有其他问题:OnCreated
/.ctor()
中手动执行,当 LINQ-to-SQL 尝试附加数据上下文时,它会中断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:OnCreated
) has executedOnCreated
/.ctor()
, it breaks when LINQ-to-SQL attempts to attach the data-contextnew()
etc will also share this codeSo unfortunately; no, I don't think you can do this in
OnCreated
in any sensible way. You could:.Create()
method that builds items correctly(since LINQ-to-SQL won't use either of the above itself)
我认为在这种情况下你最好使用工厂来创建你的实例; 因此,当前任何地方都有如下代码:
您将改为:
并且您的 CreateProduct() 方法将类似于:
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:
You would instead have:
And your CreateProduct() method would look something like:
如果您正在使用设计器 - 我认为您是因为您正在使用部分类 - 您可以在设计器中添加关联,并且相应的列将自动添加到您的对象中。 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.