“Public” nested classes or not

后端 未结 6 1763
离开以前
离开以前 2020-12-24 16:03

Suppose I have a class \'Application\'. In order to be initialised it takes certain settings in the constructor. Let\'s also assume that the number of settings is so many th

相关标签:
6条回答
  • 2020-12-24 16:06

    You can use namespaces to relate things that are... related.

    For example:

    namespace Diner
    {
        public class Sandwich
        {
            public Sandwich(Filling filling) { }
        }
    
        public class Filling { }
    }
    

    The advantage of this over using classes as if they were namespaces is that you can optionally use using on the calling side to abbreviate things:

    using Diner;
    
    ...
    
    var sandwich = new Sandwich(new Filling());
    

    If you use the Sandwich class as if it were a namespace for Filling, you have to use the full name Sandwich.Filling to refer to Filling.

    And how are you going to sleep at night knowing that?

    0 讨论(0)
  • 2020-12-24 16:15

    You might want to check out what Microsoft has to say on the topic. Basically it's a question of style I'd say.

    0 讨论(0)
  • 2020-12-24 16:23

    I think it's fine. This is basically the builder pattern, and using nested classes works pretty well. It also lets the builder access private members of the outer class, which can be very useful. For instance, you can have a Build method on the builder which calls a private constructor on the outer class which takes an instance of the builder:

    public class Outer
    {
        private Outer(Builder builder)
        {
            // Copy stuff
        }
    
        public class Builder
        {
            public Outer Build()
            {
                return new Outer(this);
            }
        }
    }
    

    That ensures that the only way of building an instance of the outer class is via the builder.

    I use a pattern very much like this in my C# port of Protocol Buffers.

    0 讨论(0)
  • 2020-12-24 16:23

    Another practical example that I have for a valid use of public nested classes is in MVC pattern when I use a viewmodel with an IEnumerable property. for example:

    public class OrderViewModel
    {
    public int OrderId{ get; set; }
    public IEnumerable<Product> Products{ get; set; }
    
    public class Product {
    public string ProductName{ get; set; }
    public decimal ProductPrice{ get; set; }
    }
    
    }
    

    I use it because I don't want Product class to be re-used outside because it is customized only for that specific viewmodel which contains it. But I can't make it private because the Products property is public.

    0 讨论(0)
  • 2020-12-24 16:26

    I primarily use nested classes for fine-tuning access to the nested and/or the container class.

    One thing to remember is that a nested class definition is basically a class member, and will have access to all the container's private variables.

    You can also use this to control usage of a specific class.

    Example:

    public abstract class Outer
    {
      protected class Inner
      {
      }
    }
    

    Now, in this case, the user (of your class) can only access the Inner class, if he implements Outer.

    0 讨论(0)
  • 2020-12-24 16:30

    I don't know if this is considered bad design or not, but I've got some search classes I make where a user calls the Run() method, passing in an object that holds search criteria. It then returns a collection of search result objects.

    These SearchCriteria and SearchResult classes have no utility outside of using them with the Search class. So I nest them under the Search class to show they go together.

    I have to make the nested classes public so the client of the Search class can make the SearchCriteria to pass into the Search class and so they can get the results of the Search.

    public class PersonSearch
    {
        public PersonSearchCriteria
        {
            string FirstName {get; set;}
            string LastName {get; set;}
        }
    
        public PersonSearchResult
        {
            string FirstName {get;}
            string MiddleName {get;}
            string LastName {get;}
            string Quest {get;}
            string FavoriteColor {get;}
        }
    
        public static List<PersonSearchResult> Run(PersonSearchCriteria criteria)
        {
            // create a query using the given criteria
    
            // run the query
    
            // return the results 
        }
    }
    
    
    public class PersonSearchTester
    {
        public void Test()
        {
            PersonSearch.PersonSearchCriteria criteria = new PersonSearch.PersonSearchCriteria();
            criteria.FirstName = "George";
            criteria.LastName  = "Washington";
    
            List<PersonSearch.PersonSearchResults> results = 
                PersonSearch.Run(criteria);
        }
    }
    
    0 讨论(0)
提交回复
热议问题