滥用类型重载在 C# 中创建样板代码

发布于 2024-07-15 22:50:12 字数 1321 浏览 8 评论 0原文

在我当前正在处理的一个项目中,我们添加了一个包装类来访问 HttpSessionState 对象。 问题在于当前的解决方案意味着您必须编写一些代码来包装功能。 我想出了以下解决方案

/// <typeparam name="TKey">Class used for generating key into session state storage.</typeparam>
/// <typeparam name="T">Type of object to store.</typeparam>
public static class SessionManager<TKey, T>
{
    static SessionManager()
    {
        _key = typeof(TKey).ToString();
    }

    private static readonly string _key;
    public static string Key
    {
        get { return _key; }
    }

     // Other functions ... (Set, IsSet, Remove, etc.)

}

现在您只需使用即可创建所需的存储

using StringStore= Test.SessionManager<System.Boolean, System.String>;
using StringStore2= Test.SessionManager<System.Version, System.String>;

StringStore.Set("I'm here");
StringStore2.Set("I'm also here");

该代码可以工作并且很好,因为您可以轻松创建包装类(单个 using 语句)并且一切都是静态的。 然而,代码有点滥用类型系统,所以也许它有点模糊? 在添加它之前,我想获得一些反馈,所以问题是:

如果您正在维护上述系统并遇到上面的代码,您会

  1. 追捕并杀死签入该文件的人吗?
  2. 对自己表现得聪明感到有点恼火,但就让它过去吧?
  3. 认为这是避免样板代码的好方法吗?

您是否愿意使用文本生成工具],例如 T4< /a>?

感谢您的回复,

麦兹

In a project I'm currently working on, we've added a wrapper class for accessing the HttpSessionState object. The trouble is that the current solution means you have to write some code to wrap the functionality. I came up with the following solution

/// <typeparam name="TKey">Class used for generating key into session state storage.</typeparam>
/// <typeparam name="T">Type of object to store.</typeparam>
public static class SessionManager<TKey, T>
{
    static SessionManager()
    {
        _key = typeof(TKey).ToString();
    }

    private static readonly string _key;
    public static string Key
    {
        get { return _key; }
    }

     // Other functions ... (Set, IsSet, Remove, etc.)

}

Now you can create the desired storage by merely using

using StringStore= Test.SessionManager<System.Boolean, System.String>;
using StringStore2= Test.SessionManager<System.Version, System.String>;

StringStore.Set("I'm here");
StringStore2.Set("I'm also here");

The code works and is nice in that you can easily create the wrapper class (single using statement) and everything is static. The code is however abusing the type system a bit, so maybe it is a bit to obscure? Before I added it I wanted to get some feedback, so here's the question:

If you we're maintaining said system and encountered the code above, would you

  1. Hunt down and kill whoever checked the file in?
  2. Be a bit annoyed by the attempt to be clever, but let it slide?
  3. Think it was a nice way of avoiding boilerplate code?

Would you prefer to use a text generation tool] like T4?

Thanks for any replies,

Mads

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

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

发布评论

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

评论(2

看海 2024-07-22 22:50:12

因此,您正在使用泛型为字典创建键。 我想说,出于几个不同的原因,这绝对不是一个好主意。

第一个原因是它违反了字典键背后的推理。 密钥应该对其所持有的价值具有一定的意义。 为什么要在 System.Boolean 下存储字符串? System.Boolean 是什么意思? 这是否意味着该字符串是 true 还是 false? 像这样的混乱使得代码更难支持。 我还怀疑键值用于将字符串转换到代码中的其他位置。 这显然是字典和泛型的错误混合。

第二个原因是会话在用户会话内共享。 因此,由两个不同的开发人员编写的两个完全不同的代码段正在访问这个共享位置。 如何阻止一个开发人员将 System.Boolean 识别为其代码中数据 A 的适当键类型,而另一开发人员将 System.Boolean 识别为其代码中数据 B 的键类型? 现在,第一个开发人员在访问此存储桶时期望 A,但得到 B。有意义的、唯一的密钥可以防止这种情况发生。

So, you're using generics to create a key for a dictionary. I'd say this is definitely not a good idea for a couple different reasons.

The first reason is that it violates the reasoning behind a dictionary key. A key should hold some significance to the value it holds. Why are you storing a string under System.Boolean? What does System.Boolean mean? Does it mean the string is either true or false? Confusion like this makes the code harder to support. I'm also suspicious that the key value is used in casting the string somewhere else in code. This is clearly a mis-mixing of dictionaries and generics.

The second reason is that the Session is shared within a user's session. So two completely different segments of code written by two different developers are accessing this shared location. What is to stop one developer from identifying System.Boolean as an appropriate key-type for data A in their code, and another using System.Boolean as a key-type for data B in their code? Now the first developer is expecting A when accessing this bucket, but gets B. A meaningful, unique key would prevent this from happening.

墨落画卷 2024-07-22 22:50:12

如果使用 System.Boolean 与 System.Version 只是为了区分不同类型以将单独的 _key 实例添加到系统中,那么我的响应将介于 #1 和 #2 之间。 至少对其进行注释并创建一些虚拟类型(可能只是空接口)来使用,而不是使用任意 .NET 类型。

我这样说是因为我的主要工作是过去 6 个多月的代码审查。 如果您与许多其他需要理解该代码的人一起工作,如果您至少不给读者一些线索,那么代码将很难理解和维护。

If the use of System.Boolean versus System.Version is merely to distinguish different types to get separate instances of _key into the system, my response would be somewhere between #1 and #2. At least comment it and create some dummy types (maybe just empty interfaces) to use instead of using arbitrary .NET types.

I say this as someone whose primary job has been code review for the past 6+ months. If you're working with a lot of other people who need to understand that code, it's going to be difficult to understand and maintain if you don't at least give the reader some clue.

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