简体   繁体   English

创建一个由许多子类组成的类库

[英]Create a class library consisting of many sub-classes

This is a follow-up to my previous question Stop my "Utility" from giving errors between different architectures , suppose I am trying to create a class library that looks something like this: 这是我之前的问题的后续问题。 阻止我的“实用程序”在不同的体系结构之间给出错误 ,假设我正在尝试创建一个看起来像这样的类库:

- Class Utility (Parent class)
     ... Utility functions and methods
         (EG: Public Sub Sub1() )

  - Class Utility_Web
       ... Functions and methods only related to Web / Web-Controls
         (EG: Public Sub Web_Sub1() )

  - Class Utility_WinForms
       ... Functions and methods only related to Winforms / WinForm-Controls
         (EG: Public Sub WinForm_Sub1() )

Now, what I would like to be able to do is to just add the Utility dll as a reference to any of my projects and be able to access the functions and methods from ALL 3 of these classes by simply typing in, for example: 现在,我想要做的是只需添加Utility dll作为我的任何项目的参考,并且只需键入即可访问所有这3个类中的函数和方法,例如:

Utility.Sub1
Utility.WebSub1
Utility.WinFormSub1

In other words, not having to type : 换句话说, 不必输入

Utility.Utility_Web.Websub1

And making it so that the end-programmer doesn't need to know the internal structure of this utility, they can reference all it's methods / functions with just the Utility. 并且使得最终程序员不需要知道该实用程序的内部结构,他们可以仅使用Utility.来引用它的所有方法/函数Utility. nomenclature. 命名。

How would I go about doing that? 我该怎么做呢? Is this where NameSpaces come into effect? 这是NameSpaces生效的地方吗? Inheritance ? Inheritance Partial Classes ? Partial Classes Modules rather than classes? Modules而不是类?

There doesn't seem to be any reason for these methods to be in separate classes if they are going to be accessed using the same class name. 如果要使用相同的类名访问这些方法,似乎没有任何理由将它们放在单独的类中。

If you want to split the code across many source files for organizational purposes, you can use partial classes . 如果要为了组织目的在多个源文件中拆分代码,可以使用部分类

This seems like an excellent instance where you'd want to use partial classes, all using the same Utility namespace. 这似乎是一个很好的实例,你想要使用部分类,所有这些都使用相同的Utility命名空间。 That would allow you to access the methods with Utility.WebSub1 and reduce a step. 这将允许您使用Utility.WebSub1访问方法并减少步骤。

A class named Utility is a bad class from the start. 名为Utility的类从一开始就是一个坏类。 What is its utility? 它的用途是什么? What does it help you do? 它对你有什么帮助? How many other people are going to name classes Utility? 还有多少人会命名类实用程序?

Name your classes for what they do, associate them in the namespaces where they make logical and functional sense. 为您的类命名您的类,将它们关联到它们具有逻辑和功能意义的命名空间中。

Let's say that you are making a set of static methods that help out with a class that represents a Month . 假设你正在创建一组静态方法来帮助代表Month的类。 Why not put the methods into Month ? 为什么不将方法放入Month If you're writing methods to transform data from one representation to another, name it that way (ie, MonthDataTranslation ). 如果您正在编写将数据从一个表示转换为另一个表示的方法,请将其命名为(即MonthDataTranslation )。

Don't worry about typing on the part of your clients or your client code. 不要担心客户端或客户端代码的输入。 Intellisense and the C# using statement mitigate that a great deal and given the choice between a badly named, nebulous Utility class and a long, well-named class, I will pick the latter every single time. Intellisense和C#using语句减轻了很多,并且在一个名称错误,模糊的Utility类和一个名字很长的类之间做出选择,我会每次都选择后者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM