ASPNET SOLID C# Principles — Single Responsability [EN]

Nicolas Fontes
4 min readJul 9, 2019

--

Starting the série of posts about SOLID Principles in C# programming, today I want to detail more that I have used about this principle and show a REAL example about how you can start to change your mode to programming to better.

The first topic about this subject that is important we talk about is: All methods should have few rows because need to have just one action or funciton. If your method have more than 30 lines, maybe something is wrong.

I need to confess that on begin of my career, I have never realize how much is wrong and hard to understand one method with many lines working in many functions. All time that I needed to evolve or change the code, I had more work to search and understand the code than develop. And was very common I find replicated codes in different class or methods.

Nowadays, this theory is very clear to me, I can apply in 100% of my projects and I can get good results of this when I need to develop test, maintenance, evolve code, etc.

Real Example

I created a simple Console Application project in Visual Studio and developed two sections (PROBLEM and SOLUTION) to better show how this works. If you want to get the code about this post, just need to click this link in my Github.

The project’s structure looks like this:

Just remember that the reason is not to show DDD or any software architeture. I just want to show the principle that I have implemented IN the code.

The Program class is where my software will be started and it will call all specific class that I’ll show in the next steps.

Problem Example

The program class is working look like this to call the problem method.

This code just have another class: User.cs

Let’s look at it:

  • This is a entity class, but have services inside.
  • Have the database connection string.
  • Have user properties.
  • Have the save method with many other functions inside it. Like valid fields, open database connection, add parameters, execute the action, close database connection and return the value.

3/4 of this topics shouldn’t be there! If I need to change the database connection string I’d change the class that have user’s service. It’s completelly doesn’t make sense. If I needed to create another module or service I’d create another connection string in the other class, more duplicated code! We need to separate the responsabilities and layers to have a better code.

Solution Example

We have already begun to see difference already in the Solution region within Program.cs. Look that now I’m instancing different class to different funcitons.

Look the user class now:

First of all I already create the user object inside the User class. Not being on the “customer” of the service. But let’s see more details:

Look that the User service class is working now:

Now this class have the responsability to instance the connection class with database. Why? Because sometimes in my main class (program.cs) I just want to call the email service or other services that I won’t to communicate with database.

Also, you can look that I created a private method just to valid the fields about the user object. I know that this validation is very simple, but I just want to show how the principle works.

Just after the validation that the code will open de connection with database. For that, I created one repository connection class just to have the function about communication with database.

After I valid the fields and open the connection, it’s time that save the information in database. For that, I created the UserRepository just to execute actions about the users in database.

This moment just have one method to receive the information in User class and save in User table. After that, the code will close the connection and response to user the success of this action.

Finishing…

For me, this is the best principle that ALL developers need to learn and work. It’s easy to implement, easy to work and easy to read/understand. We just need to look in all details.

If you want to see the code of this project, it’s available in my Github.

Link here

Thanks and see you soon =D

--

--

Nicolas Fontes
Nicolas Fontes

Written by Nicolas Fontes

.Net Developer, living and working in Ireland. My goal here is help developers to evolve theirs knowledge. Let`s do it together! http://nicolasfontes.com.br

No responses yet