2

I have been looking to use the function ScriptHeader from the class Microsoft.SqlServer.Management.Smo.StoredProcedure. I found how to do it (was expecting to have a namespace folder there). But I'm just curious to understand the reason why the enum is defined inside a class like it. What is the advantage of doing it that way?

Server ServerConnect = new Server(new ServerConnection("database"));
StoredProcedure proc = ServerConnect.Databases["myDB"].StoredProcedures[0];
string header = proc.ScriptHeader(ScriptNameObjectBase.ScriptHeaderType.ScriptHeaderForCreateOrAlter);

The class definition is :

namespace Microsoft.SqlServer.Management.Smo {
  public class ScriptNameObjectBase : NamedSmoObject
  {
    protected internal ScriptNameObjectBase();

    public enum ScriptHeaderType
    {
        ScriptHeaderForAlter = 0,
        ScriptHeaderForCreate = 1,
        ScriptHeaderForCreateOrAlter = 2
    }
  }
}
1
  • @Amy I was looking for a more detailed answer. But Eric Lippert provide a good answer.
    – maestro2
    Mar 6, 2020 at 21:43

1 Answer 1

5

I'm just curious to understand the reason why the enum is defined inside a class like it. What is the advantage of doing it that way?

To my knowledge there is no particularly compelling advantage or disadvantage to that choice. Moreover, it is an unusual choice.

Microsoft's framework design guidelines discourage base class library providers from nesting public types in this way because it can be confusing.

This choice emphasizes to the developer using this class that the enumerated type is to be strongly associated with the operations of the containing class, and only that class. That is, it emphasizes "don't be using this enum for your own purposes; it's really an implementation detail of the outer class".

It's not clear to me why the authors of this class -- who obviously worked at Microsoft -- did not follow the Microsoft guidelines in this case. I think you'd have to ask them to know for sure.

Usually the reason why you nest one type in another is to make the inner type an actual implementation detail of the outer type. That is, hiding the inner type inside the outer class to avoid "polluting" the namespace of the outer class with a type that developers are never supposed to use. But in those cases you would mark the inner type as internal, protected or private.

Not the answer you're looking for? Browse other questions tagged or ask your own question.