Hi,
I'm curious as to what is good practice when it comes to certain scenarios involving nested types in .NET.
Lets say you have a Wheel class, and the Wheel class holds Bearing objects. A Bearing object only makes sense within a Wheel and you do not want to allow it to be created independently, so it would make sense to have the Bearing class nested inside the Wheel object. However, lets say you have a scenario where you now need to read a Wheel.Bearings property outside of the Wheel class. This would now require making the nested Bearing class public.
In this situation, what is the best option?
1 - Create a public Bearing class nested within the Wheel class
2 - Create an independent Bearing class which takes a Wheel object in its constructor
3 - Create a Wheel namespace and create an independent Bearing class inside this namespace.
4 - Something else?
UPDATE: I'm updating this with more details and to reflect some of the suggestions so far. ClassParent is the parent class, ClassChild is the child class. ClassChild is ALWAYS a child of ClassParent, it does not make sense to exist on its own. The problem is that ClassChild has a few properties that need to be exposed publically, while all the rest should only be called from ClassParent. An example is a ClassChild.Delete function which should not be exposed publically because it should only be called from ClassParent as ClassParent needs to perform appropriate cleanup and modifications.
After reviewing the suggestions, the design I've come up with looks a little unclean to me so I thought I'd ask for input. I have:
public class Parent
{
ChildNested childObj
public DeleteChild()
{
//expose this method publically
childObj.DeleteChild()
//other functionality
}
public Child GetChild()
{
//expose Child, not ChildNested publically
return childObj
}
private class ChildNested:Child
{
public Child()
{
Base.Child()
}
public DeleteChild()
{
Base.Delete()
}
}
public abstract class Child
{
protected Child()
{
}
protected Delete()
{
}
public PublicPropertyToExpose()
{
}
}