静态业务层和扩展方法

发布于 2024-10-31 08:03:33 字数 1499 浏览 5 评论 0原文

我刚刚发现了扩展方法,我非常喜欢它们,以至于我担心它们的良好用途...

我有一个 3 层的 Asp Net 应用程序。我的 DAL 基于实体框架,我的 UI 是 aspx 页面,我的业务逻辑曾经是带有 LINQ to 实体查询的公共类,如下所示:

public List<Site> GetAll()
{
     return db.Sites.Include(s=> s.City).ToList();
}

您需要像这样实例化和使用:

CSite objsite = new CSite();
List<Site> sites = objsite.GetAll();

然后我发现了 Iqueryable,所以这样我可以像这样“重用”我的查询:

public ObjectQuery<Site> GetAll()
{
     return db.Sites.Include(s=> s.City);
}

public IQueryable<Site> Filter(IQueryable<Site> query, string filterWord)
{
     return (from s in query
             s.Name.Contains(word)
             select s);
}

List<Site> sites = objsite.Filter(objsite.GetAll(),"filter word").ToList();

Filter() 方法只是在 GetAll() iqueryable 上应用一个 where 子句,这非常棒,直到我发现扩展方法,所以我可以像这样处理它:

public static IQueryable<Site> Filter(this IQueryable<Site> query, string word)
{
      return (from s in query
             s.Name.Contains(word)
             select s);
}

这甚至更好,因为现在我对我的查询有了智能感知,如下所示:

List<Site> sites = objsite.GetAll().Filter("filter word").ToList();

现在,这是我感到害怕的部分,因为三件事:

  1. 您认为这是n层应用程序的好方法吗?好的设计模式还是只是一个懒惰的解决方案?

  2. 考虑到扩展方法必须是静态类下的静态方法,我的业务层将全部是静态的,这是一个好方法吗?我应该将非扩展方法放在同一个静态类下吗? (例如 GetAll() 或 AddNew())

  3. 作为 Asp Net 应用程序,拥有所有这些静态内容是否很好?

非常感谢大家!

I've just discovered extension methods and I love them so much that I'm scared about their good use...

I have a 3-tier Asp Net app. My DAL is based on Entity Framework, my UI is aspx pages and my business logic used to be the common classes with LINQ to entities queries like this:

public List<Site> GetAll()
{
     return db.Sites.Include(s=> s.City).ToList();
}

that you need to instantiate and use like this:

CSite objsite = new CSite();
List<Site> sites = objsite.GetAll();

Then I discovered Iqueryable, so this way I can "re-use" my queries like this:

public ObjectQuery<Site> GetAll()
{
     return db.Sites.Include(s=> s.City);
}

public IQueryable<Site> Filter(IQueryable<Site> query, string filterWord)
{
     return (from s in query
             s.Name.Contains(word)
             select s);
}

List<Site> sites = objsite.Filter(objsite.GetAll(),"filter word").ToList();

The Filter() method just applies a where clause on GetAll() iqueryable and this is awesome until I discovered extension methods, so I can handle it like this:

public static IQueryable<Site> Filter(this IQueryable<Site> query, string word)
{
      return (from s in query
             s.Name.Contains(word)
             select s);
}

And this is even better because now I have intellisense for my queries like this:

List<Site> sites = objsite.GetAll().Filter("filter word").ToList();

Now, this is the part where I'm scared and is because of three things:

  1. Do you think this is a good approach to an n-tier app?, is this a good design pattern or just a lazy solution?

  2. Given the requirement of extension methods to be static methods under static classes, my business tier would be all static, is this a good approach?, should I put the non-extension methods under the same static class? (ex. GetAll() or AddNew())

  3. Being Asp Net app is it good to have all this static stuff?

Thanks a lot guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤独难免 2024-11-07 08:03:33

IQueryable 之上使用自定义扩展方法是很常见的。我认为这是重用查询部分的好方法。使用扩展方法应谨慎。扩展方法只是静态方法的语法糖。我的朋友有一个关于他同事的好故事,他最近发现了扩展方法并开始使用如下代码:

var order = 1.GetOrder(); // Extension on integer - this is really bad example

您的示例中的问题是静态业务层。业务层必须以某种方式接收上下文,并且必须根据请求处理上下文。 在请求之间共享上下文或为整个应用程序提供单一上下文是最好的方式有严重的麻烦。因此,除非您将上下文传递给每个方法,否则您还必须为每个请求创建业务类。

但您仍然应该在 IQueryalbe 上使用扩展。您可能会发现您的业务类只是 context.Query 的包装器,在这种情况下,它是不需要的附加层。

另请注意,这一切都适用于分层架构,但如果您的 n 层意味着真正的物理分离(每一层都在单独的服务器上),那么情况就不同了,因为您需要一些东西来允许您在一层上创建 linq 查询并在另一层上执行它(在另一台服务器上,因此查询和结果必须通过网络传输) - WCF 数据服务就是这样的工具。

It is quite common to use custom extension methods on top of IQueryable. I think it is a good way to reuse query parts. Using extension methods should be done with caution. Extension method is just a syntactic sugar around static method. My friend have a nice story about his collegue who recently discovered extension methods and started to use code like:

var order = 1.GetOrder(); // Extension on integer - this is really bad example

The problem in your example is static business layer. Business layer must receive context somehow and context must be handled per request. Sharing context among requests or having single context for whole application is the best way to have serious troubles. So unless you pass context to each method you must create business class per request as well.

But still you should use extensions on IQueryalbe. You will probably find out that your business class is just a wrapper around context.Query and in such case it is additional layer which is not needed.

Also be aware that this all works in layered architecture but if your n-tier means real physical separation (each tier on a separate server) then it is a different story because you need something which would allow you to creatinh a linq query on one tier and executing it on another tier (on another server so the query and the result must be transported over the network) - one such tool are WCF Data Services.

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