一对一映射类的嵌套类型或其他解决方案
我有一个类需要以某种方式封装一些功能。
我正在考虑一个嵌套类,并将该功能和一些状态放入其中。 这两个类之间的关系是一对一的。
访问外部类成员变量或方法的问题应该将它们声明为静态,但我不想要它。另一种解决方案是将外部类的引用传递给内部类。
解决我的问题的最佳做法是什么?
I have a class which needs some functionality to be encapsulated in some way.
I was thinking of a nested class, and put that functionality plus some states into it.
The relation between these two classes is one-to-one.
The problem it for accessing the outer class member variables or methods they should be declared static
and I don't want it. Another solution is passing a reference of the outer class to inner class.
What's the best practice for my problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然我不会说嵌套类是邪恶的,但它们肯定可能是“恶作剧”的,并且嵌套类可以解决的问题很少不能用不同的或更优雅的方式解决。
所以这实际上取决于您的具体情况,但需要考虑的一些事情是:
嵌套类是否会公开可见?由于消费者必须使用语法来引用嵌套类型,因此这些方法很笨重:OuterType+InnerType
源文件平均变得更大,并且更难以阅读和推理(尽管这可以通过策略性使用部分来缓解) 源文件平均变得更大,并且更难以阅读和推理(
Visual Studio 中的代码分析会大声抱怨公共嵌套类(框架设计指南人员认为它们不是良好的形式),因此如果您使用 FxCop,则需要例外。
如果您发布更多具体信息,我们也许能够提供更详细的指导。
While I wouldn't go as far as to say that nested classes are evil, they certainly can be "mischievous", and there are very few problems that a nested class will solve that couldn't be solved differently or more elegantly.
So it really depends on your specific situation, but some things to consider are:
Is the nested class going to be publicly visible? These are unwieldy due to the syntax consumers must use to refer to the nested type: OuterType+InnerType
Source files get larger on average, and are harder to read and reason about (although this may be mitigated by the strategic use of partial classes).
Code Analysis in Visual Studio will complain loudly about public nested classes (they're not considered good form by the framework design guidelines folks), so if you're using FxCop you'll need to make exceptions.
If you post some more specifics, we might be able to provide more detailed guidance.
大约 6 个月前,我们遇到了完全相同的问题,但原因不同。我们有大约 20 个“常规”类和一个巨大的木星大小的类,它做得太多了,需要分解。
除了父母之外,我们实际上还需要两个孩子,这两个孩子与父母都是一对一的关系。
第一次尝试(确实有效)使用了老式模式,在每个子级的构造函数中传递“this”的引用,然后使用 .Parent 方法向后导航。由于 GC 问题,这是一场噩梦,我们在短时间内寻找更好的解决方案。
最好的解决方案(至今仍在使用)是在需要查询父级的子级方法中简单地接受父级类类型的引用。这工作得非常好,GC 喜欢它,所有东西都按预期实例化和释放。代码更易于管理,也更有组织性,我们现在很高兴我们及时进行了投资。
因此,这将是我的建议:
子对象的方法仅在需要它的方法中接受对父类的引用。
它实际上很像 ADO.Net 的开发方式,具有独立的对象,在需要的方法中接受彼此的引用。
We had exactly the same issue about 6 months ago, but for a different reason. We had about 20 'regular' classes and one giant Jupiter sized class that was doing way, way, way too much and needed to be broken down.
We actually need TWO children, in addition to the parent, with both children in a 1-to-1 relationship to the parent.
The first attempt (which did work) used the old-school pattern of passing a reference of 'this' in the constructor of each child, and then using a .Parent method to navigate back up. That was a nightmare due to GC issues, and we looked for a better solution in short time.
The best solution (which is still used today) was to simply accept a reference of the parent's class type in methods of the children which needed to query the parent. This worked fantastically well, the GC liked it, and everything instantiated and freed right as expected. The code is more manageable, and a lot more organized, and we're now really glad we made the investment in time to do it.
So, that would be my recommendation:
With methods of the Child objects accepting a reference to the parent class only in methods that need it.
It's actually a lot like the way that ADO.Net is developed, with independent objects that accept references to each other in methods where they are needed.
然后它的工作原理如下:
但我不确定这是否是一个好主意?!
Then it works like this:
But I'm not sure if this is a good idea or not ?!