4

I'm trying to read about best practices in C# development. I came across people creating different class library projects within a solution. I have been developing solutions for C# but never create separate projects for my classes. Though, some of my projects were small. To separate my namespaces, I normally create folders. Are there any good reason why I should create a separate project for my class modules?

  • 1
    Create a .dll assembly and use it across multiple projects. Eg., Logging, Authentication, Helper Classes etc etc. – Karthik Feb 28 '17 at 09:08
  • One reason is to manage complexity. You might have some public classes that use a variety of internal classes for their implementation. Types in an assembly cannot by default access internal types in a different assembly, so a programmer working in a different assembly wouldn't have to know about them. – Matthew Watson Feb 28 '17 at 09:16
  • 1
    Some useful info here : http://stackoverflow.com/questions/8934570/c-sharp-namespaces-and-assemblies-best-practice – PaulF Feb 28 '17 at 09:17

1 Answers1

2

Reason to create a separate project:

When we create a project of type Class Library, after the build the MSbuild process spits out an assembly/ dynamic linked library or .DLL file. This .dll file can be referenced in any project in the same or other solution. This supports the code reusability.

A general rule of thumb for a web based (ASP.Net MVC) solution structure is given as below:

  1. Create a Domain Model project (Class Library project type)
  2. Create a Data Access Layer (Class Library project type)
  3. Create a Service Layer project (Class Library project type)
  4. Create a UI Layer project (ASP.Net MVC project type)

Create the unit test projects (Class Library project type) for each project listed above.

Hope this helps.

Yawar Murtaza
  • 3,655
  • 5
  • 34
  • 40
  • Thanks Yawar. Will this be applicable to other development types i.e. WinForms, WPF etc? – Reydan Gatchalian Mar 02 '17 at 09:45
  • In case of WPF and / or WinForm applications, just change the step 4 to respective project type. This is generly true however, if the project is very large then in WPF you might want to create a separate project (.dll) for your view models which is also true for a MVC application - Here is a good link to read on code project that shows a MVC application architecture. https://www.codeproject.com/Articles/70061/Architecture-Guide-ASP-NET-MVC-Framework-N-tier-En - Hope this helps! – Yawar Murtaza Mar 02 '17 at 09:52