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

查看:92
本文介绍了在VB.NET中使用模块是否被认为是不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计新应用程序期间,我想知道是否使用带有属性的模块被认为是不好的做法.

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

一些示例代码:

Module modSettings

   public property Setting1 as string

   public property DatabaseObject as IDatabaseObject

End Module

以上代码仅是强调我的问题的示例.过去,这种结构在VB6中经常使用.过去,我在.NET项目中也使用了它.

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.

所以我想知道上面的代码真的 是不正确的做法.如果是这样,您将使用Module做什么?

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

推荐答案

Centro 正确的是,模块(或具有共享成员的NotInheritable类)是 最近等效于C#静态类.因此,从技术上讲,这没有什么错,因为它只是VB创建此类的一种方法.例如,您不能在VB中说Public Shared Class Settings,因为您不能将Shared关键字放在类上.

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.

如果在特定情况下需要使用Module,那么我不会将其称为坏习惯,但是对于松散耦合,可测试的代码,您可能不希望使用Module(或其他静态类等效项)作为设计选择.此外,尽管具有共享成员的NotInheritable类比仅说模块具有更多的描述性,但在至少一种情况下,必须使用模块代替.

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.

何时需要在VB.Net中使用模块?如果您想利用扩展方法,那么这是您唯一的选项,因为如上所述,您不能在VB.Net中创建共享(静态)类,也不能在NotInheritable Classes上使用扩展名.您必须使用以下模块:

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

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

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();
    }
}   

这篇关于在VB.NET中使用模块是否被认为是不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆