Classic Example for C# Generics: implementing a method differently to return different types

Till date, C# Generics is one of my favourite concepts. I still feel Generics is one timeless concept that is handy to show your technical prowess while solving complicated scenarios at the same time.  Like any technical concept, the more we implement a concept, we become better at it. So, here is one such practical situation I encountered and I felt it deserves a blog post as an eulogy for C# generics implementation. Since I'm also a long-time fan of GoF design patterns, Generics is used here as part of Factory design pattern to return two different objects from one single method implemented differently as part of the same interface when instantiated differently through Dependency Injection(DI).

For some background, with my earlier two blog posts as the basis, for creating sitemap xml in two different ways, there are two different object models required to access the Sitemap GraphQL resultset in the asp.net core rendering host side.

1. Generate/Store Sitemap xml in CMS and retrieve on rendering host request

2. Generate and display Sitemap.xml on rendering host request

Complex return type for first approach:

############

############

Complex return type for second approach:

###########

###########

One step backward, without generics in place,  you will need two different methods with same structure but different return types:

Diagrammatic representation of the scenario:

Github code for reference

The above code clearly violates the SOLID architecture' Open-Closed(OC) principle of open to extension but closed to changes. In summary, the CustomGraphQlServiceHandler class is a bottle neck. So, this is where Generics steps in to the rescue. With some minor refactoring, the GetSitemap becomes a base method that can be overridden as desired to produce the respective object for the GraphQL resultset. Retaining the return types as-is (for the first approach is named Result while the return type for the second approach is named XmlResult), this is the technical design with Generics in place:


So, here is the new code:

Base Class:

############
############

Child Class-1:

############
############

Child Class-2:

###########

###########

With such a structure in place and Dependency Injection in action, instantiating separate class for each approach, will automatically invoke the corresponding method and create the correct object model.

MvpSitemapXmlDataProvider Class method:

MvpSitemapUrlProvider Class method:


Diagrammatic representation as to how this solution confirms to SOLID architecture' Open-Closed(OC) principle:

Github Commit

Reference:

https://stackoverflow.com/questions/25211123/generic-method-that-can-return-different-types

Comments

Popular Posts