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,
- 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.
- 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.
- 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.
- 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.
using System;
namespace DerivedtoBase
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Demo for returning Child class object from Derived class object.");
DerivedClass derivedObj = new DerivedClass();
var baseObj = derivedObj.ReturnBase();
baseObj.TellClass();
Console.WriteLine("varBase1 = " + baseObj.varBase1);
Console.WriteLine("varBase2 = " + baseObj.varBase2);
// This baseObj cannot even access varBase3 because that is part of derived class
// Console.WriteLine("varBase2 = " + baseObj.varBase3);
Console.ReadKey();
}
}
public class BaseClass
{
public int varBase1 { get; set; }
public int varBase2 { get; set; }
public BaseClass()
{
varBase1 = 10;
varBase2 = 15;
}
public void TellClass()
{
Console.WriteLine("base");
}
}
public class DerivedClass : BaseClass
{
public int varBase3 { get; set; }
public int varBase4 { get; set; }
public DerivedClass()
{
varBase1 = 100;
varBase2 = 150;
varBase3 = 200;
varBase4 = 300;
}
public new void TellClass()
{
Console.WriteLine("derived class");
}
public BaseClass ReturnBase()
{
return (BaseClass)this;
}
public object ReturnObjectBasedOnInput(string Type)
{
if (Type.Equals("Full"))
{
return this;
}
else
{
return (BaseClass)this;
}
}
}
}




Archibaldo HaddockPosted Sep 10, 2021, 10:28 AM
Hi Kapil, I tryed to understand the focus of tis article. But I've a couple of questions about that, a) if you have a method that returns a list of both objects, will you use List<T> to return any of those objects? or simplier the method should only returns the parent / base one ?