实体包装器 - 自定义

发布于 2024-10-28 00:28:41 字数 1980 浏览 1 评论 0原文

我想找到一种解决方法来完成一个简单的解决方案,以便通过 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 技术交流群。

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

发布评论

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

评论(2

为你拒绝所有暧昧 2024-11-04 00:28:41

定义允许您执行此操作的接口。例如:

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

您需要在您的实体中“实现”此接口。例如,您可以修改 T4 模板生成实体或在部分类中实现它。这两个属性必须已在模型中定义,因此实现只是声明性的:

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

现在您可以定义 Store ,如下所示:

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

同样可以使用查询完成,但您可以定义自定义扩展方法:

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

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

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

其他方法是在您的自定义上下文上简单地定义 GetQuery:

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

我不是存储库模式的忠实粉丝,但通常您想要做的是接近通用存储库,因此请检查例如 这篇文章。这只是一些可以进一步扩展的示例。

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

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

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");

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
}

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.

醉殇 2024-11-04 00:28:41

是的,我只是想要一个通用的方法,同样我用数据集和数据表实现了..但似乎不可能实现....

嗯..但是,让我向你展示以下代码片段,动态关键字看起来充满希望.. ..也许我快要解决了..?

    public ObjectSet<dynamic> Retrieve(string entity, string context)
    {
        Type myObjectContextType = Type.GetType(context);            
        ConstructorInfo cs = myObjectContextType .GetConstructor(new Type[] { });
        dynamic myObjContext = cs.Invoke(new object[] { });

        Type t = Type.GetType(entity);
        ConstructorInfo xi = t.GetConstructor(new Type[] { });
        dynamic UserEntity = xi.Invoke(new object[] { });

        !problem here!
        ObjectSet<?????> os = myObjContext.UserEntity.Where(...) 

        return ...           

    }

我很惊讶..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..?

    public ObjectSet<dynamic> Retrieve(string entity, string context)
    {
        Type myObjectContextType = Type.GetType(context);            
        ConstructorInfo cs = myObjectContextType .GetConstructor(new Type[] { });
        dynamic myObjContext = cs.Invoke(new object[] { });

        Type t = Type.GetType(entity);
        ConstructorInfo xi = t.GetConstructor(new Type[] { });
        dynamic UserEntity = xi.Invoke(new object[] { });

        !problem here!
        ObjectSet<?????> os = myObjContext.UserEntity.Where(...) 

        return ...           

    }

I'm very surprised..EF is a great instruments to develope more efficiently ways but too little "generalizalbe"

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