Separate projects or multiple class files … namespace best practice in C#

后端 未结 5 1058
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 03:32

I\'m creating a library for use with an application that I am building. I am building a name space structure similar to below.

MyNamespace.Validation
MyName         


        
相关标签:
5条回答
  • 2021-01-13 04:09

    I would say it depends.

    • First, it's best practice to put each class in its own file.
    • If you go with one project, I would create folders for each namespace inside that project, and put the code files in the appropriate folder.
    • Doing the above, Visual Studio will automatically create new class files within the correct namespace

    I think the real question here is this though:

    If this is only ever going to be used once, putting everything in one project would make sense. However, if this code is going to be reusable, you should think if you would ever reuse just a part (or one sub-namespace) of this library. If the answer is yes, I would break apart the namespaces into separate projects, so in the future, you could only include the projects you needed.

    0 讨论(0)
  • 2021-01-13 04:10

    There are pros and cons to both approaches, which you need to personally decide between for your own circumstance.

    Pro to multiple projects:

    • Separate assemblies allow the compiler to provide more strict guidance, potentially preventing coupling from creeping through. This allows you to maintain the dependencies better.
    • Separate assemblies can be loaded as needed in other projects, potentially easing reuse.
    • Separate assemblies can prevent unnecessary code from being loaded into a process, since they're loaded on demand.

    Cons to multiple projects:

    • More complex deployment, as more files need deployment (minor)
    • Slower build/compile, and even potentially load times from loading multiple assemblies (minor)

    Personally, I think the pros far outweigh the cons in most cases. I typically will split my namespaces into separate assemblies, provided they are not related. In your case, you're working on 4 very different concepts, so my gut feeling is that splitting makes the most sense.

    0 讨论(0)
  • 2021-01-13 04:17

    Deciding exactly how to break up your solution is subjective - and it really depends on the specifics of your code.

    However, one thing is certain: maintaining multiple assemblies has drawbacks! This article is particularly good at describing those drawbacks, observing how they add costs at development time, compile time, deployment time, and runtime.

    I use as few assemblies as possible, aiming for a single assembly while isolating volatile areas of the domain. When multiple assemblies are clearly appropriate or required (and they often are, particularly to enforce decoupling), I do my best to group interfaces that will change at the same time into the same assemblies.

    0 讨论(0)
  • 2021-01-13 04:18

    I have usually followed the pattern with one assembly is one namespace and the DLL name is in the namespace. Easier to find what DLLs to reference

    0 讨论(0)
  • 2021-01-13 04:23

    I would go for the one solution with multiple projects.

    Advantages:
    - Each project can be a separate dll
    - All projects in one solution for easy navigating between files

    0 讨论(0)
提交回复
热议问题