在我的情况下使用扩展方法是否正确?

时间:2022-09-23 08:09:01

I use MVC4 and entity framework 5 in my project and I've lots of tables. As a rule of our project, we don't delete any records from database, each record has a isActive field and if that field is false, then we consider it deleted. I wanted to write an extension method to get active records and after some googling I wrote this:

我在我的项目中使用MVC4和实体框架5,而且我有很多表。作为我们项目的规则,我们不会从数据库中删除任何记录,每个记录都有一个isActive字段,如果该字段为false,那么我们认为它已被删除。我想写一个扩展方法来获取活动记录,经过一些谷歌搜索,我写了这个:

public static IQueryable<Company> GetAll(this IQueryable<Company> source)
{
   return source.Where(p => p.isActive);
}

Now I can use my extension method to get only active records like

现在我可以使用我的扩展方法来获取只有活动的记录

Context db = new Context();
db.Company.GetAll();

But let's say I've 50+ tables in my database, is it a good approach to write the same extension method for each of my tables. Is there a better way to write a only one GetAll() extension method for all of our tables? Actually I'm not even sure if is it right way to use extension methods for this instance?

但是,假设我的数据库中有50多个表,这是为每个表编写相同扩展方法的好方法。有没有更好的方法为我们的所有表编写一个GetAll()扩展方法?实际上我甚至不确定在这个实例中使用扩展方法是否正确?

Could somebody please help me and show me the right way? I appreciate if you help with code examples.

有人可以帮助我并告诉我正确的方法吗?如果您帮助代码示例,我将不胜感激。

2 个解决方案

#1


2  

It depends on how you are using Entity Framework, if you are depending on the normal generator (and I think you do), your case gets harder and I never had a similar case study. But, if you use normal POCO classes generator, you can use a base class let's call it CEntity which is a base class for each of your other classes (tables).

这取决于你如何使用实体框架,如果你依赖于正常的生成器(我认为你这样做),你的情况变得更难,我从来没有进行类似的案例研究。但是,如果你使用普通的POCO类生成器,你可以使用一个基类让我们称之为CEntity,这是你的每个其他类(表)的基类。

Are we there yet? No, to continue with this, I prefer to use the Repository Pattern, and you can make that repository generic (CEntity), for example:

我们到了吗?不,为了继续这一点,我更喜欢使用存储库模式,并且您可以使该存储库具有通用性(CEntity),例如:

public class Repository<CEntity> where CEntity : class
{
    public IQueryable<CEntity> GetAll()
    {
       return source.Where(p => p.isActive);
    }

}

And this is how to use it:

这是如何使用它:

Repository<Company> com = new Repository<Company>();
Repository<Employee> emp = new Repository<Employee>();

var coms = com.GetAll();    // will get all ACTIVE companies
var emps = emp.GetAll();    // will get all ACTIVE employees

This is off the top of my head, if you had any other problems, put them as comments, glad to help.

这是我的头脑,如果你有任何其他问题,把它们作为评论,很乐意提供帮助。

#2


1  

Just as a point of interest, this is exactly how I implement my data layers, and i think its awesome :)

就像一个兴趣点,这正是我实现我的数据层的方式,我认为它真棒:)

I also jam a repository in the middle as well but the general concept should work with or without.

我也在中间堵塞了一个存储库,但是一般的概念应该有或没有。

Here's some working code examples of how I use this method in my blog for some similar use cases.

这里有一些工作代码示例,说明我在博客中如何使用此方法来处理类似的用例。

https://github.com/lukemcgregor/StaticVoid.Blog/blob/master/Blog/Data/Entities/Post/PostRepositoryExtensions.cs

I've found that it makes some pretty elegant code while still not restricting what you can do too much. Like i said, i think this method is awesome and really recommend its usage.

我发现它制作了一些非常优雅的代码,同时仍然没有限制你可以做的太多。就像我说的,我认为这种方法很棒,真的推荐它的用法。

#1


2  

It depends on how you are using Entity Framework, if you are depending on the normal generator (and I think you do), your case gets harder and I never had a similar case study. But, if you use normal POCO classes generator, you can use a base class let's call it CEntity which is a base class for each of your other classes (tables).

这取决于你如何使用实体框架,如果你依赖于正常的生成器(我认为你这样做),你的情况变得更难,我从来没有进行类似的案例研究。但是,如果你使用普通的POCO类生成器,你可以使用一个基类让我们称之为CEntity,这是你的每个其他类(表)的基类。

Are we there yet? No, to continue with this, I prefer to use the Repository Pattern, and you can make that repository generic (CEntity), for example:

我们到了吗?不,为了继续这一点,我更喜欢使用存储库模式,并且您可以使该存储库具有通用性(CEntity),例如:

public class Repository<CEntity> where CEntity : class
{
    public IQueryable<CEntity> GetAll()
    {
       return source.Where(p => p.isActive);
    }

}

And this is how to use it:

这是如何使用它:

Repository<Company> com = new Repository<Company>();
Repository<Employee> emp = new Repository<Employee>();

var coms = com.GetAll();    // will get all ACTIVE companies
var emps = emp.GetAll();    // will get all ACTIVE employees

This is off the top of my head, if you had any other problems, put them as comments, glad to help.

这是我的头脑,如果你有任何其他问题,把它们作为评论,很乐意提供帮助。

#2


1  

Just as a point of interest, this is exactly how I implement my data layers, and i think its awesome :)

就像一个兴趣点,这正是我实现我的数据层的方式,我认为它真棒:)

I also jam a repository in the middle as well but the general concept should work with or without.

我也在中间堵塞了一个存储库,但是一般的概念应该有或没有。

Here's some working code examples of how I use this method in my blog for some similar use cases.

这里有一些工作代码示例,说明我在博客中如何使用此方法来处理类似的用例。

https://github.com/lukemcgregor/StaticVoid.Blog/blob/master/Blog/Data/Entities/Post/PostRepositoryExtensions.cs

I've found that it makes some pretty elegant code while still not restricting what you can do too much. Like i said, i think this method is awesome and really recommend its usage.

我发现它制作了一些非常优雅的代码,同时仍然没有限制你可以做的太多。就像我说的,我认为这种方法很棒,真的推荐它的用法。