如何对 ASP.NET 类中的函数进行分组?

发布于 2024-07-29 11:21:40 字数 560 浏览 4 评论 0原文

我目前有一个名为“Customers”的 VB.NET 类,它的大小一直在稳步增长,现在我在其中有几十个函数。 有没有办法将函数维护在同一个类中? 因为他们仍然使用常见的私有方法,但按相似性对它们进行分组。

例如:

Class Customers
-GetData
---GetCustomerObject()
---GetCustomerFieldx()
-Lists
---GetSomeList()
---GetAnotherList()
-Maintenance
---AddCustomer()
---DeleteCustomer()
---UpdateCustomer()

更新:我想我不清楚我希望这个分组发生在哪里。 当我使用 Customer 类时,我希望分组几乎类似于 IntelliSense 中的命名空间/类。 我目前使用区域,但它们仅在查看代码时有帮助,而不是在使用类时有帮助。

I currently have a VB.NET class named "Customers" and it's been steadily growing in size and I now have a couple dozen functions in it. Is there a way of maintaining the functions in the same class? Since they still use common private methods, but group them by similarity.

For example:

Class Customers
-GetData
---GetCustomerObject()
---GetCustomerFieldx()
-Lists
---GetSomeList()
---GetAnotherList()
-Maintenance
---AddCustomer()
---DeleteCustomer()
---UpdateCustomer()

UPDATE: I guess I wasn't clear on where I wanted this grouping to occur. I want the groupings to be almost like namespaces/classes in IntelliSense when I use my Customer class. I currently use Regions but they only help when seeing the code, not when using the class.

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

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

发布评论

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

评论(6

随波逐流 2024-08-05 11:21:41

在我看来,客户类可能做得太多,需要分解以遵守单一职责原则。 这个想法是,一个类知道如何做好一件事,并且获取数据与使用所述数据执行业务逻辑是不同的责任。 我目前正在经历学习过程,试图将我的整体课程分解为更小、更有针对性的课程,因此我无法就如何做到这一点提供很好的建议,但网上有很多资源可以提供帮助:

< a href="https://stackoverflow.com/questions/488684/using-the-single-responsibility-principle-in-the-real-world">像这样

有一点谷歌的SOLID和鲍勃叔叔(罗伯特·C·马丁饰)的额外善良。

It seems to me that the Customers class may be doing too much and needs to be broken down to adhere to the Single Responsibility Principle. The idea is that a class knows how to do one thing well and getting data is a different responsibility to performing business logic with said data. I'm currently going through the learning process of trying to break down my monolithic classes into smaller, more targetted ones so I can't give great advice about how to do this, but there are lots of resources on the web to help:

like this

Have a bit of a google for SOLID and Uncle Bob (Robert C. Martin) for extra added goodness.

神经大条 2024-08-05 11:21:41

区域是我推荐的一种解决方案。 但您也可以使用部分类。

Region is one solution which I recommend. But you can also use partial classes.

国际总奸 2024-08-05 11:21:41

请记住,当您将类分解为更小的类时,您可以将它们放置在任意深度的命名空间系统中。 Project.Customer.Data 命名空间可以容纳 GetCustomerObject 和 GetCustomerField 类。

(无论如何,我正在尝试自学将 SOLID 应用到我的 VB.NET 工作中。这就是您最终实现的那种事情吗?)

And remember, when you do break the class up into much smaller classes, you have the ability to place them in an arbitrarily-deep namespace system. The Project.Customer.Data namespace could house the GetCustomerObject and GetCustomerField classes.

(As much as anything, I'm trying to teach myself to apply SOLID to my VB.NET work. Is this the kind of thing that you ended up implementing?)

水波映月 2024-08-05 11:21:40

我不建议在一般基础上对文件中的内容进行区域分组。

如果你的文件增长到这样的大小,你觉得你需要对事物进行分组才能重新获得控制权,那么这暗示你应该尝试将类重构为几个类,每个类负责原始类所做的子集,这样你就可以不会以 God 对象 的无法测试的混乱告终

I'd not recommend regions to group content in your files on a general basis.

If your file grows to such a size that you feel you need to group things to regain control then that is a hint you should try to refactor the class into several classes each responsible for a subset of what the original class did, so that you do not end up with the untestable mess that is the God object

我不会写诗 2024-08-05 11:21:40

我没有看到任何代码向您展示如何实现这些建议,因此我将在此处添加它。 您需要根据功能将代码分解为类。 然后将命名空间关键字添加到您创建的每个类中。 这是包含更多详细信息的 MSDN 文章 (http://msdn.microsoft .com/en-us/library/ms973231.aspx#assenamesp_topic3)。 这是伪代码。 然后,您可以使用projectname.namespace.classname.method引用代码。

namespace Customers
-Class GetData 
---GetCustomerObject() 
---GetCustomerFieldx()
end namespace

namespace Customers 
-Class Lists 
---GetSomeList() 
---GetAnotherList() 
end namespace

namespace Customers 
-Class Maintenance 
---AddCustomer() 
---DeleteCustomer() 
---UpdateCustomer()
end namespace 

I don't see any code showing you how to implement the suggestions, so I will add it here. You need to break the code into classes based on function. Then add the namespace keyword to each of the classes you make. Here is the MSDN article with more detail (http://msdn.microsoft.com/en-us/library/ms973231.aspx#assenamesp_topic3). Here is the psuedo code. Then you would reference the code by using projectname.namespace.classname.method.

namespace Customers
-Class GetData 
---GetCustomerObject() 
---GetCustomerFieldx()
end namespace

namespace Customers 
-Class Lists 
---GetSomeList() 
---GetAnotherList() 
end namespace

namespace Customers 
-Class Maintenance 
---AddCustomer() 
---DeleteCustomer() 
---UpdateCustomer()
end namespace 
潇烟暮雨 2024-08-05 11:21:40

在源文件中使用代码区域。 这为您提供了展开/折叠按钮,以便您可以隐藏文件的各个部分。

#Region "GetData"
    ... code ...
#End Region

#Region "Lists"
    ... code ...
#End Region

#Region "Maintenance"
    ... code ...
#End Region

Use code regions in your source files. This gives you expand/collapse buttons so you can hide sections of the file.

#Region "GetData"
    ... code ...
#End Region

#Region "Lists"
    ... code ...
#End Region

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