Is it nice to represent the inheritance hierarchy in a namespace structure?

I have a group of interconnected classes that are all overridden to create a specific implementation. I'm wondering if it's worth wrapping interconnected subclasses in a namespace.

As an example, consider the following namespaces and classes:

namespace Protocol
{
  public abstract class Message { }
  public abstract class Driver { }
}
namespace Protocol.Tcp
{
  public class TcpMessage : Message { }
  public class TcpDriver : Driver { }
}
namespace Protocol.Ftp
{
  public class FtpMessage : Message { }
  public class FtpDriver : Driver { }
}

What is the best way to structure namespaces? It seems inevitable to reveal inheritance in the namespace, since the base classes do not really belong in either the Protocol.Tcp namespace or the Protocol.Ftp namespace.

+3
source share
3 answers

I think you might be too worried!

Does it make sense logically? Do you know where to find your code in namespaces?

​​ , , , , , .

, ,

, :)

EDIT:

:

using System.Data;
using System.Data.Sql;

;)

+6

, # - - #.

, , , Message Driver , , , .

+1

If it were me, I would define 2 namespaces:

Protocol

and

Protocol.Driver

Splitting the namespace like this separates your "library code" from your "executable / test code." I also create my namespaces according to the directory structure; it will give logic to the structure of your programs and code files. (maybe you already did it ...)

0
source

Source: https://habr.com/ru/post/1696467/


All Articles