【发布时间】:2016-03-12 18:41:12
【问题描述】:
我的目标是这样的:
// Defines members, for the "change-log" of the API,
// if the interface has changed, the API has a new major version.
// For "automatically generating changelogs" (for major versions) of the API
interface IApp
{
static string Name { get; set; }
}
// Internal class, not for usage outside of the dll
internal static class AppConfig
{
internal static bool IsPublished;
}
// Public available members from the API
public static class App : AppConfig, IApp
{
public static string Name { get; set; }
}
现在,上面的结构有一些错误,基于 C# 语言:
- 接口不能有静态成员
- App类是静态的,不能继承静态类
- AppConfig 是静态的,因此无法继承
- App 类是静态的,所以不能有接口
我目前的“解决方案”:
public static partial class App
{
internal static bool IsPublished;
}
public static partial class App
{
public static string Name { get; set; }
}
我想将合同/接口添加到...所以,我可能会以类似的方式结束,“包装”APP:
public static class App
{
private static _App app;
static App()
{
app = new _App();
}
public static string Name { get { return app.Name; } }
}
internal interface _IApp
{
string Name { get; set; }
}
internal class _App : _AppConfig, _IApp
{
public string Name { get; set; }
}
internal class _AppConfig
{
internal static bool IsPublished;
}
这是冗长、乏味和无聊的。三个需要更新的地方插入一个新成员:接口、_App-class(实现)和静态 App-class(针对 API 用户)。
- 我想要实现两件事:合约、接口,它定义了从一个版本到另一个版本的所有主要更改(读取接口、打印到更改日志)。
- 将不应供 API 用户使用的东西设为私有(内部...)。
问题?以前有人做过类似的事情,你是怎么解决的?或者说服我忘记基于接口的变更日志的想法......因为接口需要非静态对象,而我想要静态对象(至少在这个特定对象上,它是静态的!)。
PS:自动取款机。我将 API 的所有公共对象/成员读入日志,现在是“更改日志”。但是从一个新的 API 开始,想做一些……不同的事情。 :)
编辑:注意;我关心对象在“另一面”的外观,这是一件重要的事情。 API 的用户,调用 App 成员,应该像这样简单(直截了当): System.Windows.App.Name; 这意味着“外部类”(或者你想怎么看)是一个静态对象。
最后一点:我有几个(12-15)个这种“类型”的对象,所以我想要一个优雅的结构,所有对象都命名相似,所以如果你了解一个对象,你就会知道所有对象。含义:如果一个对象有一个接口,那么所有其他对象也有一个。如果一个对象被命名为“AppConfig”,您可以赌上另一个对象也有一个名为“OtherConfig”的类。 :)
【问题讨论】:
-
为什么是静态的?是否要防止创建多个实例?您可以使用单例并使类成为非静态类。
-
我不想要单例模式。从“API 用户的角度”来看,该类必须是静态的。除了“包装”解决方案之外,或多或少是单例模式......
-
你必须使用 public static IApp TheApplication = new App();
-
公共静态 IApp TheApplication = new App();所以你是说我必须忍受这个?呵呵...我关心用户如何调用我的方法:System.Windows.App.Name;猜猜最后一个将_App“包装”到“App”静态对象中的例子是要走的路……
标签: c# inheritance interface static