Interface Segregation Principle (ISP) in C#
In my previous articles I wrote about Solid Principles in C#.
In this article, I am going to show you when and how to use the Interface Segregation Principle in C# with an example project. You can find the repository on GitHub.
The master branch shows the initial code used in the example.Ā There are separate tags and branches for each of the all solid principles that you can review or download as well. Here are links you can use to jump to these tagged versions in your browser:
What is the Interface Segregation Principle (ISP) in C#?
The Interface Segregation Principle is the fourth of the SOLID design principles. Let us take a look at Robert C. Martin’s definition and try to analyse it.
Clients should not be forced to depend on methods that they do not use.
What’s a Client?
It’s the calling code. It’s the code that is interacting with an instance of the interface.
What does Interface mean in C# for the Interface Segregation Principle (ISP)?
In C# we have two kinds of interfaces described by the ISP.
- C# interface.
- Public or Accessible Interface of a C# class.
A type’s interface is whatever can be accessed by a client.
Ok nice description but in the end what do we have to do?
Prefer small and related interfaces to fat ones.
Why should you use the Interface Segregation Principle (ISP) ?
To understand this concept, just take a look at the famous example of class MembershipProvider in .NET Framework 2.0, implementing a Membership Provider required to implement at least 25 methods. As you know from the previous article, not fully implementing an interface breaks LSP. Should you depend on this massive interface? No.
Large interfaces result in more dependencies, this means:
- More dependencies result in more coupling;
- Increased coupling generates weak code;
- Difficult code testing due to many methods to be implemented;
- Long and difficult deployment.
You need to use Interface Segregation Principle (ISP) to avoid all the problems listed above.
How to detect Interface Segregation Principle (ISP) Violation?
- Fat Interfaces.
- NotImplementedException.
interface INotificationService { void SendEmail(); void SendSms(); } class NotificationService : INotificationService { public void SendEmail() { Console.Write("SendEmail"); } public void SendSms() { throw new NotImplementedException(); } }
- Interfaces with multiple responsibilities.
interface INotificationService { void SendEmail(); void SendSms(); } interface IEmailNotificationService { void SendEmail(); } interface ISmsNotificationService { void SendSms(); } class EmailNotificationService : IEmailNotificationService { public void SendEmail() { Console.Write("SendEmail"); } }
How can i use the Interface Segregation Principle (ISP)?
These are the typical approaches to Interface Segregation Principle (ISP):
- Break up large interfaces into smaller ones.
- To manage large interfaces you don’t control:
- Create a small cohesive interface.
- Use the Adapter design pattern as use it as a Wrapper.
How we can refactor the code applying the Interface Segregation Principle (ISP)?
Please have a look at how I have refactored my previous code here ISP-END
If you think your friends/network would find this useful, please share it with them. Iād really appreciate it.
Thanks for reading!
Ā
Ā