BEST PRACTICES

A Best Practice for Designing Interfaces in .NET C#

Is it enough to define IMyInterface<T>? do I need IMyInterface as well?

Ahmed Tarek
Published in
9 min readNov 2, 2021

--

Best Practice for designing Interfaces in DotNet (.NET) CSharp (C#). When to define a non-generic interface in addition to the generic interface. Best Practice Code Coding Programming Software Development Architecture Engineering
Photo by Kelly Sikkema on Unsplash

While working on your masterpiece software system, you define your own interfaces to act as contracts between your different system modules and to expose some contracts to the outside world. This is not rocket science to any software developer.

However, what I learned throughout my years as a Software Engineer is that we should put more care about how we design our interfaces. If you search the internet, you would find tons of resources discussing the best practices to follow when designing interfaces, some of these are actually good resources.

But, I have one more best practice to tell you about which I couldn’t find online except for rare coincidences.

Best Practice for designing Interfaces in DotNet (.NET) CSharp (C#). When to define a non-generic interface in addition to the generic interface. Best Practice Code Coding Programming Software Development Architecture Engineering
Photo by Susan Q Yin on Unsplash

Is it enough to define IMyInterface<T>? do I need IMyInterface as well?

To answer this question, let’s walk through the example below step by step, so bear with me and don’t rush things out.

Best Practice for designing Interfaces in DotNet (.NET) CSharp (C#). When to define a non-generic interface in addition to the generic interface. Best Practice Code Coding Programming Software Development Architecture Engineering
Photo by Goh Rhy Yan on Unsplash

Disclaimer

We would not follow a lot of the best practices here for simplicity and to drive the main focus to the topic we are discussing. So, no unnecessary abstractions, no care for immutability,….and so on.

Main Entity Classes

These are the main entity classes we are going to use.

--

--

Software Engineer 👨‍💻 | .NET dotnet C# csharp c-sharp | Website: https://www.developmentsimplyput.com | LinkedIn: @atarekhasan