The correct namespace for many concrete abstract class implementations

I am developing a .NET library, and I am looking for some feedback regarding the organization of classes and namespaces.

Suppose I have an abstract Message class that is in the root namespace of Foo . Message has many specific implementations for different types of messages. Given that there will be many specific implementations (say 20 or even more), it is expected that these specific types will live in a separate namespace, something like Foo.Messages ?

I'm worried about cluttering the namespace with too many specific Message classes that users of my library rarely need to use. What do you expect from using the library?

change

Also, if I group specific classes in Foo.Messages , should there be an abstract abstract class there or should it stay in Foo ?

+4
source share
3 answers

Kind of a loaded question, but if the problem has too many classes that make it difficult to detect, is part of the solution, maybe for some of these classes internal rather than public ? If users never instantiate them directly, this may be part of the solution to your interference problem.

It really depends a lot on the nature of the API you are developing, and to some extent on the taste. Often there is one primary class or set of classes that your users will use in most cases, for example, NLog Logger or AutoMapper Mapper , or NoRM Mongo , or the class factory as a whole. Those that should be in front, in the center and discoverable, and you can make a convincing example that it is best to have this in the root namespace.

For other classes (those that are more related to implementation and less about the API), it is obvious that you want to be organized, but you can play with a pretty free hand and organize as you think, because the parts of the API that users should find are are most noticeable. Foo.Messages seems like a perfectly reasonable way to get started. On the other hand, if 90% of your classes are subclasses of Message , but there is an important difference between server messages and client messages, or Purple messages and Plaid messages, perhaps Foo.Server or Foo.Plaid are the right types of namespaces for you.

0
source

If users of your library will not create your specific implementation classes (they go through the factory template for receiving instances and only access instances through the abstract base type), consider making your specific implementation classes internal. This will avoid them from completion options user code.

Grouping all the concrete implementation classes in the Foo.Messages namespace sounds great. You don’t want to go too far - creating a large number of namespaces for minor things is a huge annoyance for the user.

+5
source

If you are creating a library and using an abstract message class with many specific implementations, it can often be a good idea to hide all concrete implementations from the library user and provide the acess class to the Factory class, which will return the abstract message (return type, the actual return object will be concrete, of course), if this is normal for library clients, which is often encapsulated as 20 or even more classes and provides access to the required functions in a large and convenient way.

0
source

All Articles