Understanding Class Libraries in C#

Kmcnet 1,066 Reputation points
2022-07-06T02:10:18.15+00:00

Hello and thanks for the help in advance. I'm trying to understand the concepts of using a class library for enterprise development using C#. Conceptually, I understand te concept is to encapsulate code in order for it to be resused by different applications. Where I am confused is to how to set up the library using Visual Studio 2022. Intuitively, I thought the library would be developed as a stand-alone project then referenced b the consuming applications. But I see examples where the class library is included (and sometimes required to be included) in the consuming application solution. So is the class library developed initially within the consuming application? What are the best practices for developing these libraries, especially when the library may reference other package libraries? And when shoudl a NuGet package be used instead of a class library. Any insight would be appreciated.

Developer technologies .NET Other
Developer technologies C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rijwan Ansari 766 Reputation points MVP
    2022-07-07T14:56:32.523+00:00

    HI @Kmcnet

    It's all about structuring your projects/classes. You can follow some best practices, design patterns and architectural recommendations.

    You can add classes into any projects, however:
    – Categorize your classes/files based on the responsibilities like Application business logics, Data access layer, Entities, Helpers, common, external and so on.

    • KISS principle (keep it simple and Stupid)
    • Easy to find, navigate and meaningful

    https://rijsat.com/2022/03/14/clean-architecture-with-net-6/

    1 person found this answer helpful.

  2. Jack J Jun 25,296 Reputation points
    2022-07-06T10:09:08.593+00:00

    @Kmcnet , Welcome to Microsoft Q&A, let me answer your confusion.

    But I see examples where the class library is included (and sometimes required to be included) in the consuming application solution. So is the class library developed initially within the consuming application?

    It will be convenient for us to write the class library and consuming application in the same solution. You could also developed the Class Libraries separately.

    What are the best practices for developing these libraries

    Please refer to the following link to know how to deal with it.

    What are best practices for developing class library in general and database layer specifically?

    In C# what is best practice when setting up a project with dependencies (e.g. class library or separate files)

    Best practice when developing a C# Library for use in multiple solutions/projects

    In a conclusion, I think we need to consider the following things.

    First, We could try to use the repository pattern in the Class Library.

    Second, We could consider that we need to have a single code base maintaining the library.

    Third, Please try to use Nuget when you used multiple versions of the library.

    Hope my answer could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-07-06T23:49:37.52+00:00

    A class project should reside in a separate Visual Studio solution if the intent is to reuse. For instance, to enforce coding standards you can place generic interfaces and generic classes in one class project while another may be connecting to SQL-Server using Entity Framework Core and might even be a local NuGet package or if no sensitive data (which is not needed) than you can host it on NuGet (like mine). Another example for packaging on NuGet, a validator package.

    Any .NET Core class library/project which is consumed by a front end project does not need the Entity Framework package installed (unlike pre-EF Core).

    Here is another idea, create a solution template which in the projects points to your NuGet packages and or local globe class projects.

    No matter the direction my advise is to take time, think through what you are planning now and down the road. This is what I've done on each team I work on. The last time did much of the above and other than upgrading things like Entity Framework and how mapping is done the base remains constant but with reviews to see if there is a better way.

    And lastly, learn to work with interfaces, generics, sealed and abstract classes. Many developers tend to stray away from these and could greatly benefit from understanding them and implementing. Look at design patterns too.

    Edit

    Make sure to write unit test for all class project be they in packages or not, this can weed out issues now and later down the road.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.