首页 \ 问答 \ 在VB.NET中使用模块被认为是不好的做法?(Are using modules in VB.NET considered bad practice?)

在VB.NET中使用模块被认为是不好的做法?(Are using modules in VB.NET considered bad practice?)

在设计一个新的应用程序时,我想知道如果使用具有属性的模块被认为是不好的做法。

一些示例代码:

Module modSettings

   public property Setting1 as string

   public property DatabaseObject as IDatabaseObject

End Module

上面的代码只是一个强调我的问题的例子。 在过去,这个结构在VB6中被使用了很多。 在过去,我也在.NET项目中使用它。

但是现在有了像依赖注入,可测试性和关注分离这样的流行语,上述结构闻起来很糟糕。 我无法真正描述为什么,但它只是感觉不对。 我必须承认,我对上述关键字还不是很熟悉。

所以我想知道上面的代码是不是一个坏习惯。 如果是这样,你会用什么Module


During the design of a new application I was wondering if using a module with properties is considered to be a bad practice.

Some example code:

Module modSettings

   public property Setting1 as string

   public property DatabaseObject as IDatabaseObject

End Module

The code above is just an example to emphasize my question. In the past, this structure was used a lot in VB6. In the past I used it as well in my .NET projects.

But nowadays with buzzwords like Dependency Injection, Testability and Separation of Concerns the above structure smells bad. I can't really describe why, but it just feels wrong. I must admit I'm not very familiar with the keywords above, yet.

So I'm wondering whether the above code really is a bad practice. If so, what would you use a Module for?

更新时间:2022-07-27 18:07

最满意答案

Centro是正确的,一个模块(或带有共享成员的不可继承类)与C#静态类最接近 。 所以从技术上讲,没有什么是错的,因为它只是VB创建这种类的方法之一。 例如,您不能在VB中说Public Shared Class Settings ,因为您无法将Shared关键字放在类上。

就其本身而言,如果某个特定的情况需要一个模块,我不会称之为不好的做法,否则一个模块(或其他静态类的等价物)可能不是您想要的具有松散耦合,可测试代码的设计选择。 此外,尽管带有共享成员的不可继承类比描述模块更具描述性,但至少有一种情况需要使用模块来代替。

你什么时候需要在VB.Net中使用模块? 如果你想利用扩展方法 ,那么这是你唯一的选择,因为如前所述,你不能在VB.Net中创建一个共享(静态)类,你也不能在不可继承类上使用扩展。 您必须按如下方式使用模块:

Imports System.Runtime.CompilerServices

Public Module StringExtensions
    <Extension()> _
    Public Function Remove( _
                        ByVal input As String, _
                        ByVal subStrings As String()) As String
        Return String.Join("", input.Split(subStrings, StringSplitOptions.None)).Trim()
    End Function
End Module

在C#中,您不能使用模块,并且必须使用静态类,如下所示:

public static class StringExtensions
{
    public string Remove(this string input, string[] subStrings)
    {
        return string.Join("", input.Split(subStrings, StringSplitOptions.None)).Trim();
    }
}   

Centro is right that a Module (or a NotInheritable Class with Shared members) is the closest equivalent to a C# static class. So technically, nothing is wrong with it as it's just one of VB's ways of creating this type of class. For example, you cannot say Public Shared Class Settings in VB as you cannot put the Shared keyword on a class.

On its own I wouldn't call it bad practice if a specific circumstance calls for a Module, but otherwise a Module (or other static class equivalents) likely is not the design choice you want for having loosely coupled, testable code. Additionally, while a NotInheritable Class with Shared members is more descriptive than just saying Module, there is at least one circumstance where a Module must be used instead.

When would you need to use Modules in VB.Net? If you want to take advantage of extension methods, then it's your only option since as mentioned, you cannot create a shared (static) class in VB.Net, neither can you use extensions on NotInheritable Classes. You must use a module as follows:

Imports System.Runtime.CompilerServices

Public Module StringExtensions
    <Extension()> _
    Public Function Remove( _
                        ByVal input As String, _
                        ByVal subStrings As String()) As String
        Return String.Join("", input.Split(subStrings, StringSplitOptions.None)).Trim()
    End Function
End Module

In C# you can't use modules and must use static classes as follows:

public static class StringExtensions
{
    public string Remove(this string input, string[] subStrings)
    {
        return string.Join("", input.Split(subStrings, StringSplitOptions.None)).Trim();
    }
}   

相关问答

更多
  • 如果您知道存储过程是什么但不确定参数是什么 - 使用SqlCommandBuilder.DeriveParameters方法获取参数数量以及它们的类型和名称的更好方法。 更新这是VB.NET中的一个基本用法示例 Dim oCommand As New SqlCommand("SpName", oConnObject) oCommand.CommandType = CommandType.StoredProcedure SqlCommandBuilder.DeriveParameters(oCommand) ...
  • 对于象你正在发展的游戏一样的象棋,使用绝对定位没有内在的错误。 正如你所说,相对定位和正常流量布局使得这种任务相当困难。 当然,如果您正在开发更标准的网站,例如提供一些公共服务的网站,绝对定位会覆盖浏览器的默认流布局,因此可以减少许多用户的可访问性。 在这种情况下,我会避免它。 话虽如此,绝对定位的一个较不知名的好处是它允许在 (相对定位的)父元素内的局部绝对定位。 解释:
    Module是与C# static类的VB对应。 当您的类专为辅助函数和扩展方法设计,并且您不想允许继承和实例化时 ,您将使用Module 。 顺便说一句,使用Module不是很主观,并没有被弃用 。 确实,在适当的时候你必须使用Module 。 .NET Framework本身做了很多次(例如System.Linq.Enumerable )。 要声明扩展方法,需要使用Module s。 Modules are VB counterparts to C# static classes. When your ...
  • 它们被称为Class文件。 右键单击要添加类的解决方案或文件夹,然后选择“ Add new item然后从窗口中选择“ Class 。 They are called Class files. Right-click on your solution, or folder you wish to add the class and choose Add new item then from the window choose Class.
  • 我的首选项是将控制器方法中的数据复制到ViewData或模型本身。 这实际上是控制器的责任 ,而不是视图。 我认为这不是一种不好的做法,但我可以看到它在后期导致维护头痛。 你并不是真的希望你的观点与外部状态联系在一起; 他们只应该绑定到model / viewmodel / viewdata。 My preference would be copy the data in your controller method to the ViewData or the model itself. That rea ...
  • 您可以将JavaScript添加到 。 您可以使用控件的ClientID附加JavaScript,也可以使用代码隐藏添加它 MyTextBox.Attributes.Add(“onKeyPress”,“myJavaScriptFunction()”); You can add JavaScript to . You can either attach the JavaScript using the ClientID of the control, ...
  • CORS不是不好的做法。 它受到所有主流浏览器的支持 ,越来越多的API支持它。 事实上,如果您的公共资源不在防火墙后面, 则可以安全地将Access-Control-Allow-Origin: *标头放在资源上。 但是CORS在服务器上的作用存在一些混淆。 CORS只应规定特定资源的跨源策略。 换句话说,CORS头只是表示是否允许来自不同来源的请求。 我认为混淆是因为服务器有时也使用CORS来决定安全策略。 CORS不安全。 如果服务器的资源需要受到某些用户的保护,则仅依靠Origin头来执行此操作是不安 ...
  • Centro是正确的,一个模块(或带有共享成员的不可继承类)与C#静态类最接近 。 所以从技术上讲,没有什么是错的,因为它只是VB创建这种类的方法之一。 例如,您不能在VB中说Public Shared Class Settings ,因为您无法将Shared关键字放在类上。 就其本身而言,如果某个特定的情况需要一个模块,我不会称之为不好的做法,否则一个模块(或其他静态类的等价物)可能不是您想要的具有松散耦合,可测试代码的设计选择。 此外,尽管带有共享成员的不可继承类比描述模块更具描述性,但至少有一种情况需 ...
  • 根据微软的说法: 在每个声明的参数中包含ByVal或ByRef关键字是一种很好的编程习惯。 如果您使用Visual Studio,如果您没有明确指定它,则默认插入ByVal 。 According to Microsoft: It is good programming practice to include either the ByVal or ByRef keyword with every declared parameter. And if you use Visual Studio, it de ...
  • Intellisense(和“Goto定义”)应该能够轻松找到事物的位置,但我总是在调用时使用更好的命名空间,只是为了清晰阅读。 然后很明显它是一个自定义函数,而不是你正在使用的类内置或本地内容。 也许我缺少一个细微的差别,但我倾向于使用共享类而不是模块来代替常见和自包含的任何代码 - 它似乎更容易跟踪我,它也会强制执行你的前置规则它,因为你不能从任何地方调用它而不给出命名空间来调用它。 Intellisense (and "Goto Definition") should make it trivial ...

相关文章

更多

最新问答

更多
  • 在javascript中创建类以创建对象并在Java中创建类和对象之间的区别(Difference between creating a class in javascript to create an object and creating an class and object in Java)
  • Facebook API:将身份验证详细信息从Javascript SDK发送到PHP SDK(Facebook API: Send authentication detail from Javascript SDK to PHP SDK)
  • 如何停止队列动画jquery?(How can I stop queue animation jquery?)
  • 使用C#的井字游戏中的人工智能(Artificial Intelligence in Tic-Tac-Toe using C#)
  • 多少流量可以共享虚拟主机(对于Python Django站点)支持?(How Much Traffic Can Shared Web Hosting (for a Python Django site) support?)
  • 带有CIFilters的CAShapeLayer(CAShapeLayer with CIFilters)
  • 如何在Angular 2中读取JSON #text(How to read in Angular 2 the JSON #text)
  • 如何在xml中读取自闭标签的属性?(How to read self closing tag's attribute in xml?)
  • 无法使用http put将图像上传到亚马逊S3(Cannot upload image to amazon s3 using http put)
  • 文件结束无限循环(end of file infinite while-loop)
  • 在cpp的模板(template in cpp)
  • 在构建库时,clang和clang ++有什么区别?(What's the difference between clang and clang++ when building a library?)
  • ng类中的表达式(expression inside ng-class)
  • 在PHP中获取随机布尔值true / false(Get random boolean true/false in PHP)
  • 管道的高效分块用于严格的字节串(Efficient chunking of conduit for strict bytestring)
  • Python ternary_operator(如果其他标志做了其他操作,则执行其他操作)(Python ternary_operator (do-somthing if flag else do-another))
  • Sencha Touch面具发布(Sencha Touch mask ondisclosure)
  • 验证脚本上的通知[重复](Notices on validation script [duplicate])
  • 朋友功能(friend function)
  • 基于角坐标平移和变换平面几何(Translate and transform plane geometry based on corner coordinates)
  • Rails:'如果在本地运行'条件javascript标记包括(Rails: 'if running locally' conditional javascript tag include)
  • 解压文件(Unzipping files)
  • 使用ui-router以角度加载变量状态(loading in variable states with ui-router in angular)
  • 创建Azure云服务需要多长时间?(how long does it take to create an Azure Cloud Service? How to view log information?)
  • 指向整数的指针数组(Array of pointers to integers)
  • Laravel服务提供商没有看到我的包的主要类(Laravel service provider does not see the main class of my package)
  • 这个关于VSS / RSS / PSS / USS的解释是否准确?(Is this explanation about VSS/RSS/PSS/USS accurate?)
  • 在Django-Admin中通过row-id排序显示项目(Ordering the display items by row-id in Django-Admin)
  • 如何使用cythonize启用`--embed`?(How to enable `--embed` with cythonize?)
  • 用于将文本多行设置的Excel脚本(Excel script for ereasing text multiple rows)