How To Return Different Types Of Objects In C# Based On A Input Parameter Type

Requirements come like this that you have multiple roles in an organization. Now based on the role you want to return different profiles of same data like for some user you want to show Full data and for some you want to return the medium amount of same data and for some user you want to show only minimal attributes of same data.

Introduction
 
Requirements come like this that you have multiple roles in an organization. Now based on the role you want to return different profiles of same data like for some user you want to show Full data and for some you want to return the medium amount of same data and for some user you want to show only minimal attributes of same data. Now there are multiple ways of doing this like,
  • A> Based on the user you can hide attributes: At many places, people also use this method they just hide some attributes based on the job role of the user. They just toggle the visibility of fields in data.
  • B> You write different entities and code altogether to render data for different job roles. People use this also but it is not a good practice. Because this kind of code is not well managed.
  • C> We can use the concept of inheritance and casting to do the same. The advantage here is that we do not need to toggle the visibility of fields which makes the security of data intact. Security of data is the main issue while toggling visibility because if there is a techie user he can anyway see your data by toggling the visibility himself using inspect element or some other mean. so when there is a need of detail level always throw a limited object. Hiding is not a good idea for a large application.
  • D> Others
So here we will see methodology C where we will use inheritance and casting concept and will achieve the detail levels.
 
Let us come into action with a small demo,
 
Here we have created a console application using Visual Studio 2017 and written code like below just to make you aware of the concept. We have a BaseClass and DerivedClass over here and you will see that DerivedClass inherits BaseClass. Which means BaseClass have fewer details of data and Derived Class will have more details to data. Hence we can easily say DerivedClass is a full profile of data and BaseClass is a low profile of that data. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace DerivedtoBase  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Console.WriteLine("This is demo for returning Child class object from Derived class object.");  
  14.             DerivedClass derivedObj = new DerivedClass();  
  15.             var baseObj = derivedObj.ReturnBase();  
  16.             baseObj.TellClass();  
  17.             Console.WriteLine("varBase1 = "+ baseObj.varBase1);  
  18.             Console.WriteLine("varBase2 = " + baseObj.varBase2);  
  19.             // this baseObj cannot even access varBase3 because that is part of derived class  
  20.             // Console.WriteLine("varBase2 = " + baseObj.varBase3);  
  21.             Console.ReadKey();  
  22.         }  
  23.   
  24.         public class BaseClass  
  25.         {  
  26.             public int varBase1 { getset; }  
  27.             public int varBase2 { getset; }  
  28.             public BaseClass()  
  29.             {  
  30.                 varBase1 = 10;  
  31.                 varBase2 = 15;  
  32.             }  
  33.               
  34.             public void TellClass()  
  35.             {  
  36.                 Console.WriteLine("base");  
  37.             }  
  38.               
  39.         }  
  40.         public class DerivedClass: BaseClass  
  41.         {  
  42.             public int varBase3 { getset; }  
  43.             public int varBase4 { getset; }  
  44.             public DerivedClass()  
  45.             {  
  46.                 varBase1 = 100;  
  47.                 varBase2 = 150;  
  48.                 varBase3 = 200;  
  49.                 varBase4 = 300;  
  50.             }  
  51.             public new void TellClass()  
  52.             {  
  53.                 Console.WriteLine("derived class");  
  54.             }  
  55.             public BaseClass ReturnBase()  
  56.             {  
  57.                 return (BaseClass)this;  
  58.             }  
  59.   
  60.             public object ReturnObjectbasedonInput(string Type)  
  61.             {  
  62.                 if (Type.Equals("Full"))  
  63.                 {  
  64.                     return this;  
  65.                 }  
  66.                 else  
  67.                 {  
  68.                     return (BaseClass)this;  
  69.                 }  
  70.                   
  71.             }  
  72.         }  
  73.     }  
  74. }  
You will see here that we have created a full object that is of DerivedClass and we have written a method in DerivedClass to return the ChildClass object of the same DerivedClass object using Casting. Though we have created an object DerivedObj which is having full details in variable baseObj we have just returned the baseObj attributes only. Which is low detail level data. There is no need to hide any attributes. If you will run this code then you will find out baseObj cannot even access varBase3 attribute which is available as part of full profile data in DerivedClass.
 
The output of the above code comes like this having only two details because we are having full data but we are throwing low-level data only.
 
 
 
Now let us discuss and create a more practical example of this concept. Suppose you are creating a student database where there are two roles students and teachers etc. Now you want to show student data using two profiles Low & Full. So How will we do that? Let us create two data profiles first and write code as below.
  1. namespace DerivedtoBase  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Console.WriteLine("This is demo for returning different detail levels using inheritance in C#.");  
  8.             FullProfile fpdata = new FullProfile();  
  9.             string profile = "Full";  
  10.             if (profile.Equals("Full"))  
  11.             {  
  12.                 Console.WriteLine("---------------------Full Profile------------------------");  
  13.                 Console.WriteLine("StudentName = " + fpdata.StudentName);  
  14.                 Console.WriteLine("FatherName = " + fpdata.FatherName);  
  15.                 Console.WriteLine("CourseName = " + fpdata.CourseName);  
  16.                 Console.WriteLine("attendance = " + fpdata.attendance);  
  17.                 Console.WriteLine("City = " + fpdata.City);  
  18.                 Console.WriteLine("DOJ = " + fpdata.DOJ);  
  19.                 Console.WriteLine("Subject = " + fpdata.Subject);  
  20.             }  
  21.             else  
  22.             {  
  23.   
  24.                 Console.WriteLine("---------------------Low profile------------------------");  
  25.                 lowProfile baseObj = (lowProfile)fpdata;  
  26.                 Console.WriteLine("StudentName = " + baseObj.StudentName);  
  27.                 Console.WriteLine("FatherName = " + baseObj.FatherName);  
  28.                 Console.WriteLine("CourseName = " + baseObj.CourseName);  
  29.                 //Only these 3 properties are accessible through baseObj  
  30.             }  
  31.   
  32.   
  33.               
  34.             Console.ReadKey();  
  35.         }  
  36.   
  37.   
  38.         public class lowProfile  
  39.         {  
  40.             public string StudentName { getset; }  
  41.             public string FatherName { getset; }  
  42.             public string CourseName { getset; }  
  43.             public lowProfile()  
  44.             {  
  45.                 StudentName = "Rahul Khanna";  
  46.                 FatherName = "Abhishek Khanna";  
  47.                 CourseName = "Graduation";  
  48.             }  
  49.   
  50.         }  
  51.         public class FullProfile : lowProfile  
  52.         {  
  53.             public int attendance { getset; }  
  54.             public string Subject { getset; }  
  55.             public string City { getset; }  
  56.             public string DOJ { getset; }  
  57.             public FullProfile()  
  58.             {  
  59.                 StudentName = "Rahul Khanna";  
  60.                 FatherName = "Abhishek Khanna";  
  61.                 CourseName = "Graduation";  
  62.                 attendance = 15;  
  63.                 City = "Lucknow";  
  64.                 DOJ = "28-02-2012";  
  65.                 Subject = "Physics";  
  66.             }  
  67.               
  68.         }  
  69.        
  70.     }  
  71. }  
The output of the above code comes like this,
 
 
 
Now, let's change the profile declaration value in code to low. The output would come as below,
 
 
 
So this is how we can control what kind of object to be rendered. You can also tweak and enhance the code little bit to return data from the database. This demo I have created just for concept purpose. We can write better code while writing it for an org level project.
 
Conclusion
 
I always aspire for working on alternatives to doing things. This is just one way of doing it as I mentioned in the introduction of the article. By using this approach when you return object then there is no need to hide properties based on job roles or detail profiles but you altogether use inheritance and casting to achieve the same. This is only one alternative. I am still working and will try to find a better and optimized approach for the same.