实体包装器 - 自定义
我想找到一种解决方法来完成一个简单的解决方案,以便通过 EF 自动执行某些操作。 我需要的是在保存和检索过程中接管以修改查询结果,但此类将能够使其适用于任何类型的实体。
示例:我有一个 MyTestDb。因此,在我的 C# 项目中,我创建了一个新的实体模型 (MyTEstDbModel.edmx),并生成相对的 POCO 类。 好吧,一个兴趣点可能是实现一个新的自定义类,如下所示:
class Example
{
private ObjectContext _context;
private Example(ObjectContext obj) { _context = obj; }
public void Store(ObjectSet<???generic???> os)
{
// problem here: I dont't know the type contained in ObjectSet
// but if I Knew its type, I could make a work like this:
// -> foreach every instance in objectSet to check if exist some property
// via reflection, if i found them, then I set always the same values.
// Why this? Because all my db contains some common filed
// like (createdByUser, TimeToUpdate, and so on..), so it wold be boring
// setting all those fileds from any point of program.
}
public void Retrive(ObjectSet<???generic???> os)
{
// here problem too: all my queries will be filtered by one or more value
// fileds, but I cannot use lambaExpression cos I don't Know the type
// contained in objectSet<..>
}
//....
最后,通过程序的任何点,代码应如下所示:
Example obj = new Example(myEntityContext); //-> global
var result = myEntityContext.ObjectSetTyped.Where(..lambaExpression..condition)
result.someProperty = "...";
obj.Store(result); // store method will fill all other boring filed automatically.
任何人都可以给我一些关于我的问题的提示、帮助和建议吗?
提前致谢...
更新
现在,只是另一个问题。我将通过如下所示的检索方法来过滤我的 ObjectSet:
public void Retrieve<TEntity>(IQueryable<TEntity> ooo) where TEntity : IC
{
ooo = ooo.Where(p => p.Filed == "MyDefaultValue");
}
但是,从外部方法来看,objectSet 结果不受我的过滤器的影响。 为何如此..?
MyEntities ent = new...
MyWrapper wrap = new MyWrapper();
wrap.Retrieve(ent.Users);
//这里有问题->用户对象集总是相同的..
I would like find a workaround to accomplish a simple solution in order to automatize certain operation through EF.
What I need it's takeover during saving and retrieving process to modifying query result, but this class will be able to make that work for any type entities.
Example: I have a MyTestDb. So in my C# project I create a new entity model (MyTEstDbModel.edmx), with relative POCO class generation.
Well, a point of interest could be implementing a new custom class like following:
class Example
{
private ObjectContext _context;
private Example(ObjectContext obj) { _context = obj; }
public void Store(ObjectSet<???generic???> os)
{
// problem here: I dont't know the type contained in ObjectSet
// but if I Knew its type, I could make a work like this:
// -> foreach every instance in objectSet to check if exist some property
// via reflection, if i found them, then I set always the same values.
// Why this? Because all my db contains some common filed
// like (createdByUser, TimeToUpdate, and so on..), so it wold be boring
// setting all those fileds from any point of program.
}
public void Retrive(ObjectSet<???generic???> os)
{
// here problem too: all my queries will be filtered by one or more value
// fileds, but I cannot use lambaExpression cos I don't Know the type
// contained in objectSet<..>
}
//....
finally, by any point of program, the code should appear like following:
Example obj = new Example(myEntityContext); //-> global
var result = myEntityContext.ObjectSetTyped.Where(..lambaExpression..condition)
result.someProperty = "...";
obj.Store(result); // store method will fill all other boring filed automatically.
Can anyone give me some tips, help, suggestion about my issue?
Thanks in advance...
Update
Now, just only another problem. I'd to filter my ObjectSet through retrieve method like following:
public void Retrieve<TEntity>(IQueryable<TEntity> ooo) where TEntity : IC
{
ooo = ooo.Where(p => p.Filed == "MyDefaultValue");
}
But, from external method, not objectSet result is affect by my filter.
How so..?
MyEntities ent = new...
MyWrapper wrap = new MyWrapper();
wrap.Retrieve(ent.Users);
//problem here -> users objectSet is always the same..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义允许您执行此操作的接口。例如:
您需要在您的实体中“实现”此接口。例如,您可以修改 T4 模板生成实体或在部分类中实现它。这两个属性必须已在模型中定义,因此实现只是声明性的:
现在您可以定义
Store
,如下所示:同样可以使用查询完成,但您可以定义自定义扩展方法:
您只需定义您的查询如下:
其他方法是在您的自定义上下文上简单地定义 GetQuery:
我不是存储库模式的忠实粉丝,但通常您想要做的是接近通用存储库,因此请检查例如 这篇文章。这只是一些可以进一步扩展的示例。
Define interfaces which will allow you to do this. For example:
You need to "implement" this interface in your entities. You can for example either modify T4 template generating entities or implement it in partial class. Both properties must be already defined in the model so the implementation is only declarative:
Now you can define
Store
like:Same can be done with query but you can for example define custom extension method:
You will simply define your query like:
Other approach is defining simply GetQuery on your custom context:
I'm not a big fan of the repository pattern but generally what you are trying to do is close to generic repository so check for example this post. It is just some example which can be further extended.
是的,我只是想要一个通用的方法,同样我用数据集和数据表实现了..但似乎不可能实现....
嗯..但是,让我向你展示以下代码片段,动态关键字看起来充满希望.. ..也许我快要解决了..?
我很惊讶..EF 是一个很好的工具,可以更有效地开发方法,但“泛化”太少
yeah, I just want a generic approach, likewise I realized with dataset and datatable.. but it seems impossible to achieve..
..ehmm..however, let me show yuo following code snippet, dynamic keyword looks like something to hopeful.... maybe I'm colse to solution..?
I'm very surprised..EF is a great instruments to develope more efficiently ways but too little "generalizalbe"