0
class Program
{
    static void Main(string[] args)
    {
        Master m = new Master(37);
        Content c = new Content(m);
    }
}

I have two classes defined. The first as follow:

internal class Content 
{
    private Master _parentMaster;

    private Content _contentChildren;

    #region Constructor

    internal Content(Master master)
    {
        this._parentMaster = master;
    }
    internal Content(Master master, long contentId)
    {
        this._parentMaster = master;
        _getContent(contentId);
    }

    #endregion

    #region Private Methods

    private void _getContent(long contentId)
    {
        contentRepository.getContent(contentId, _parentMaster.MasterId);
    }

    #endregion

    #region Internal Methods

    internal void AddContent()
    {
        // code omitted
    }
    internal void UpdateContent()
    {
        // code omitted
    }

    internal void GetChildren()
    {
        // code imitted
    }
    internal void GetRootContent()
    {
        // code omitted
    }

    #endregion

    #region Properties

    // code omitted

    #endregion

}

and the second one:

public class Master
{
    private MasterEntities _master;
    private List<Master> _masters;

    #region Constructor

    internal Master()
    {
        _master = new MasterEntities();
    }
    internal Master(long masterId)
    {
        _getMaster(masterId);
    }

    #endregion

    #region Private Methods

    internal void _getMaster()
    {
            // code omitted
    }

    #region Properties

    internal long MasterId
    {
        get
        {
            return _master.id;
        }
        set
        {
            _master.id = value;
        }
    }
    // code omitted

    #endregion

}

Is this the correct way to implement the two classes? Or will be better to implement Nested Classes? If yes, how to get the same result stated in "Main" with nested classes?

6
  • 1
    Why not expose the Content as a property on Master? Commented Jul 20, 2012 at 15:06
  • @Aphelion That would be viable, but what if Master can have multiple Content? This is a 1 to Many set up, which I can see as appropriate. Commented Jul 20, 2012 at 15:20
  • 1
    @seekerOfKnowledge Good suggestion. It could be implemented as a IList<T>. Actually In that case I would recommend the Composite pattern. Commented Jul 20, 2012 at 15:22
  • 1
    Yes, I've got already the same idea. If i expose the Content Class in the Master I need to do something like this: Master M = new Master(34); M.Content = new Content(M); Is there a way to do not pass the master (M) in the Content Consctructor? Otherwise I do not understand why the reference is needed. Commented Jul 20, 2012 at 15:23
  • 1
    @LucaMattuz A possible solution is to make the setter for the master internal, and let the IList<T> implementation of the master manage the data relations. Commented Jul 20, 2012 at 15:38

2 Answers 2

1

Whether you have nested classes or not makes no big difference. It only changes the way you access the class type but does not change the behavoir of the classes. The nested class does not see the members of the enclosing class unless it has a reference to it.

Say, we have this nested class

public class Containing
{
    public class Nested
    {
    }
}

Now you can access the class from outside of Containing with

Containing.Nested obj = new Containing.Nested();

The containing class acts similar to a namespace. This is true for other nested types as well. I would only use nested types if the nested type is tighly bound to the containing class. A good example is an enum used only in a particular class.

public class SomeData
{
    public enum MyValueType
    {
        Aaa, Bbb, Ccc
    }

    public MyValueType MyValue { get; set; }
}

This could well be the case for your Content class. It's up to you to decide whether is makes your code more understandable and better structured or not.

Sign up to request clarification or add additional context in comments.

Comments

1

I generally only use nested classes if one class is entirely an implementation detail of the other--in which case I make the nested class private. If a class is required to be used outside of another class, I generally never have nest classes in that circumstances.

Exceptions might be enums that apply to another class, delegate signatures, or EventArgs-derived classes.

But, for the most part it's stylistic. If you find that having one class nested within another offers better readability or maintainability, then it's really a personal choice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.