jonathana jonathana - 3 months ago 8
C# Question

How to prevent multiple Initialization of property from derived classes

The code below is a short "summary" of my real project that contain only the relevant parts to understand my question. (and don`t want to ruin someones day with the original code).

imagine you have 4 classes: Division, Branch, Department and Team. every class inherits base class in that order.

The final goal: return one division object that holds List of 7 Branches, each Branch holds List of 7 Departments and each Department holds List of 7 Teams.
for example i will be able to reach from outside to every class instance:

division d = new division();
d.CreateDivisionStructure();
int Example = d.ListOfBranches[5].ListOfDepartments[4].ListOfTeam[3].SomeIntegerProperty;


(the original code has that lists, override functions, properties etc..)

the problem: imagine that
protected string _myVar
in the code below is a
Datatable
that needs to initialized via
myVar
property with heavy resource consuming SQL query. hence, my wish is to initialize
protected string _myVar
only one time for all the creation of the "division units structure". in the code below
protected string _myVar
will be null 64 times and will be initialized 64 times (for my understanding 7 times for each unit and one time for each
base()
call).

how can i achieve that?

i tried quite a lot other ways but could not solve that. I would appreciate any help, different way of thinking or advice.

class Program
{
static void Main(string[] args)
{
division d = new division();
d.CreateDivisionStructure();

Console.ReadLine();
}
}


class division

{
private static int CountHowManyTimesMyVarWasInitilized = 0;
public division()

{

}

protected string _myVar;

public string myVar
{
get
{
if (_myVar == null)
{
CountHowManyTimesMyVarWasInitilized++;
Console.WriteLine(CountHowManyTimesMyVarWasInitilized);
_myVar = "now myVar is not null";
return _myVar;
}
else
{ return _myVar; }

}
set { _myVar = value; }
}

public void CreateDivisionStructure()

{

Console.WriteLine(myVar);
for (int i = 0; i < 7; i++)
{
Branch b = new Branch(7);
}
}
}

class Branch : division

{

public Branch(bool dImDerivedClass)
{
// constructor for department to prevent recursive stackoverflow if base of department will call the empty constructor
}

public Branch(int NumberOfBranches)
{
Console.WriteLine(myVar);
Department d = new Department(7);
}

}


class Department : Branch

{


public Department(bool ImDerivedClass) : base(true)
{
// constructor for team to prevent recursive stackoverflow if base of Team will call the empty constructor
}

public Department(int numberOfDep) : base(true)
{
for (int i = 0; i < numberOfDep; i++)
{
Console.WriteLine(myVar);
Team t = new Team(7);
}
}
}


class Team : Department

{
public Team(int numberOfTeams) : base(true)
{
for (int i = 0; i < numberOfTeams; i++)
{
Console.WriteLine(myVar);
}
}
}
}

Answer

This is likely a good use of the Lazy<T> class, used in a static variable so there is only a single copy for the process. It will run the Func you give it once to initialize during the first access of the variable.

https://msdn.microsoft.com/en-us/library/dd642331(v=vs.110).aspx

However, based on your class structure I'm not sure if it is the best approach. What is the purpose for the hierarchy of Branch : division and Department : Branch. Is the Branch a Division? If you are trying to share common properties as to not code them over again, I would suggest creating a common class that can hold those variables that Branch, Division, and Department can inherit from.

Comments