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 🙂

1 thought on “C# Regions

  1. pizzathehut

    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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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