我想找一个解决方法来完成一个简单的解决方案,以通过EF自动化某些操作。 我需要它在保存和检索过程中接管修改查询结果,但这个类将能够使任何类型实体的工作。Entity Wrapper - 自定义
例如:我有一个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.
谁能给我一些提示,帮助,关于我的问题的建议?
在此先感谢...
更新
现在,仅仅只有一个问题。我倒是过滤我的对象集通过检索方法类似以下内容:
public void Retrieve<TEntity>(IQueryable<TEntity> ooo) where TEntity : IC
{
ooo = ooo.Where(p => p.Filed == "MyDefaultValue");
}
但是,从外部方法,而不是对象集的结果是我的过滤器的影响。 怎么这么..?
MyEntities ent = new...
MyWrapper wrap = new MyWrapper();
wrap.Retrieve(ent.Users);
//这里的问题 - >用户对象集始终是相同的..
Thnaks你很支持,但真正的问题恰恰是这样的:我不” t想要(由于任何原因)既不会修改T4模板也不会自动生成通过我的接口继承的实体容器。所有我想要的是创建我的新类允许上述操作生成任何类型的实体.. – Bit 2011-03-31 12:12:52
MOREOVER,请注意,并非我所有的数据库的表都包含相同的属性,有人拥有“property1 - property2”,其他只能拥有“property1”接口实现将失败。 – Bit 2011-03-31 12:24:13
在这种情况下,祝你好运。如果你想通用的方法,你需要一些基础接口或类来开始。您使用反射提出的解决方案可能是性能杀手,并且是在代码审查之后得到一些非常糟糕的反馈的最佳方式。不要懒惰,如果你有多个不同的属性集,你应该有多个方法。 – 2011-03-31 19:28:42