C# 中丑陋的继承

发布于 2024-12-11 22:36:14 字数 493 浏览 2 评论 0原文

我有

public class ViewBaseBase : UserControl

然后我有

public class ViewBase<TPresenter, TIView> : ViewBaseBase

这当然会导致

public class AView : ViewBase<ConcretePresenter, IView>

推理,我想要一种非通用的方式来多态地访问 ViewBase,而不需要知道 TPresenter、TIView。

这对任何人来说都是“错误的”吗?我不知道,它有效,它似乎没有给我带来问题,但感觉有点“错误”。可能是 ViewBaseBase 名称,我可能可以使用一个接口,但实际上我很喜欢在该级别继承 UserControl,因为所有 ViewBase 都必须是 UserControl ...

I have

public class ViewBaseBase : UserControl

Then I have

public class ViewBase<TPresenter, TIView> : ViewBaseBase

Which leads of course to

public class AView : ViewBase<ConcretePresenter, IView>

The reasoning goes, I'd like a non generic way to access ViewBase polymorphically without knowing TPresenter, TIView.

Does this look "wrong" to anyone? I don't know, it works, it doesn't seem to be causing me problems, but it feels "wrong" some how. Possibly it's the ViewBaseBase name, I could probably use an interface instead, but actually I quite like having the UserControl inheritance at that level as all ViewBase must be a UserControl...

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

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

发布评论

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

评论(2

剩余の解释 2024-12-18 22:36:14

如果您希望以多态方式访问这些东西,除非您有一个非泛型绝对基类,否则您无能为力。您还说过每个 ViewBase 必须是 UserControl,但这并不意味着您不能使用接口。考虑一下:

public interface IViewBase
{
}


public abstract class ViewBase<IPresneter, TIView> : UserControl
                                                   , IViewBase
{
}


public class AView : ViewBase<ConcretePresenter, IView>
{
}

AView 现在是一个 UserControl 和一个 IViewBase。我个人认为这样比较干净。

There's not much you can do unless you have a non-generic absolute base class, if you wish to access this stuff polymorphically. You've also said that every ViewBase must be a UserControl, but that doesn't mean you can't use interfaces. Consider this:

public interface IViewBase
{
}


public abstract class ViewBase<IPresneter, TIView> : UserControl
                                                   , IViewBase
{
}


public class AView : ViewBase<ConcretePresenter, IView>
{
}

AView is now a UserControl and a IViewBase. I personally think this is cleaner.

ら栖息 2024-12-18 22:36:14
public class ViewBaseBase : UserControl

如果任何 ViewBaseBase 必须是 UserControl,那么这比接口更好,因为它强制执行此操作。
如果 ViewBaseBase 提供代码而不仅仅是签名(一切都是抽象的),那么这是唯一可能的方法,无需进行一些真正繁重的卷积,因为 C# 不提供多重继承。
如果 ViewBaseBase 只提供签名(一切都是抽象的)并且某些东西可能是 ViewBaseBase 而不是 UserControl (即使不在您的项目中),那么我会考虑一个接口。

层次结构的后续级别也有大致相同的逻辑。

就整体层次结构而言,它是否会妨碍您?如果是,那么可能值得以某种方式进行重构,但如果不是,那么我什至不会停下来思考它。毕竟,类层次结构的目的是让开发人员的生活更轻松,所以是否这样做才是最终关心的问题。

public class ViewBaseBase : UserControl

If any ViewBaseBase must be a UserControl, then this is preferable to an interface because it enforces this.
If ViewBaseBase provides code rather than just signatures (everything is abstract), then this is the only possible way to do things without some really heavy convolutions, because C# doesn't provide multiple inheritance.
If ViewBaseBase only provides signatures (everything is abstract) and something could conceivably be a ViewBaseBase without being a UserControl (even if not in your project), then I'd consider an interface.

Much the same logic comes at the subsequent levels of the hierarchy.

On the overall hierarchy, is it getting in your way in any case? If it is then it may be worth refactoring in one way or another, but if it isn't then I wouldn't even pause to think about it. After all, the purpose of the class hierarchy is to make the developers' lives easier, so whether or not it is doing that is the ultimate concern.

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