0

C# Regions

 3 years ago
source link: https://blog.coates.dk/2015/04/22/c-regions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C# Regions

Whilst reviewing a solution I came across the class in the image below, and felt the need for a rant about using regions.

Regions C#

If you need regions, you should consider if the class has more than one RESPONSIBILITY and or is too big! In this case it is responsible for everything and over 1800 lines of code:-(

So a tip, if you need to use the #REGION’s in your code. I would advise you take a few moments and ensure that the class does not have mixed responsibilities. If it does have more than one responsibility you should split it up into a number of smaller classes with a clearly defined responsibility.

I re-factored the class into the following classes:

  • 4 Service classes
  • 13 Factories
  • 13 Repositories
  • 13 Model classes.

In fact by the time I had removed all the code from the class I found that it was not required at all 🙂

This entry was posted in .Net, Bad code, Region and tagged .net, C#, Regions on April 22, 2015.

Post navigation

← Sitecore 7.5 SQL Provider (WFFM 2.5) – Download for specific data range Sitecore SPEAK Insert Link Dialog – missing icons & sitecore tree – Don’t panic, U didn’t break it!!!! →

1 thought on “C# Regions”

  1. Great post. It’s a very good point that if you ever have to start organising classes in this way, then they are probably doing too much. Regions should really only ever wrap the properties and methods in a class. If you have multiple regions for either, then rethink the class and its responsibility.

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK