实体包装 - 自定义 [英] Entity Wrapper - Custom

查看:130
本文介绍了实体包装 - 自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一个解决方法,以实现一个简单的解决方案,通过EF以使自动化某些操作。 我需要什么保存和检索过程中修改查询结果,在它的收购,但这个类可以使这项工作对于任何类型的实体。

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.

例如:我有一个MyTestDb。所以在我的C#项目创建一个新的实体模型(MyTEstDbModel.edmx),相对POCO类生成。 好了,兴趣点可能是实现一个新的自定义类类似以下内容:

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<..>
}
//....

最后,由任何一点程序,在code应该会出现类似以下内容:

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?

在此先感谢...

更新

现在,仅仅只有一个问题。我倒是筛选我的对象集通过检索方法类似以下内容:

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..

推荐答案

定义的接口,让您做到这一点。例如:

Define interfaces which will allow you to do this. For example:

public interface IEntity
{
  DateTime CreatedAt { get; set; }
  string CreatedBy { get; set; }
}

您需要落实这个接口在你的实体。例如,您可以要么修改T4模板生成的实体或部分类实现它。两个属性必须在模型中已经定义所以执行仅声明:

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:

public partial class MyEntity : IEntity // That's all
{ }

现在,你可以定义商店这样的:

Now you can define Store like:

public void Store<TEntity>(TEntity entity) where TEntity : IEntity
{
  ...
}

同样可以查询做,但你可以例如定义自定义扩展方法:

Same can be done with query but you can for example define custom extension method:

public static IQueryable<TEntity> GetUserEntities(this IQueryable<TEntity> query, string user) 
    where TEntity : IEntity 
{
   return query.Where(e => e.CreatedBy == user);
}

您只需定义您的查询,如:

You will simply define your query like:

var result = myEntityContext.MyEntities.GetUserEntities("someName");

另一种方法是定义简单GetQuery您的自定义背景:

Other approach is defining simply GetQuery on your custom context:

public IQueryable<T> GetQuery<T>() where T : IEntity
{
    var query = GetObjectSetSomehow;
    return query.ApplyGlobalConditions(); // Just another extension with your conditions
}

我不是存储库模式的一个大风扇,但一般你正在尝试做的是接近通用的存储库,以便检查,例如<一个href="http://stackoverflow.com/questions/4295975/repository-pattern-in-entity-framework-4-when-should-we-dispose/4298173#4298173">this帖子。这仅仅是一些例子,可以进一步延长。

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.

这篇关于实体包装 - 自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆