I am implementing the following piece of code using Fluent Interface design in C# 3.0.
The code is working fine.
public interface ITrainable
{
ITrainable AddSkill(string _skill);
}
public interface ISearchSkill
{
ISearchSkill SearchSkill(SoftwareEngineer emp,string[] _skills);
}
public abstract class Person
{
public Person(){}
protected string Name { get; set; }
}
public class SoftwareEngineer:Person,ITrainable
{
protected internal List<string> skillSet { get; set; }
public SoftwareEngineer() { }
public SoftwareEngineer(string name)
{
Name=name;
skillSet = new List<string>();
}
public ITrainable AddSkill(string _skill)
{
skillSet.Add(_skill);
return this;
}
}
public class HRExecutive :Person,ISearchSkill
{
SoftwareEngineer _employee;
public HRExecutive()
{
_employee=new SoftwareEngineer();
}
public ISearchSkill SearchSkill(SoftwareEngineer _employee,string[] skills)
{
this._employee= _employee;
foreach (string _skill in skills)
{
if (_employee.skillSet.Contains(_skill))
{
Console.WriteLine(Name + " is trained on " + _skill);
}
else
{
Console.WriteLine(Name + " is not trained on " + _skill);
}
}
return this;
}
}
Execution
SoftwareEngineer emp1 = new SoftwareEngineer("JonSkeet");
emp1.AddSkill("java").AddSkill("C#").AddSkill("F#");
HRExecutive hr = new HRExecutive();
hr.SearchSkill(emp1, new string[] { "java", "C#" }).
SearchSkill(emp1, new string[] { "Oracle", "F#" });
Question :
I don't want the skillSet of SoftwareEngineer being accessed by some XXX class.It could be accessed by limited classes.But protected internal List<string> skillSet { get; set; }
is the only option (i think) i can declare in order to access the skillSet from HRExecutive.If i do so other XXX class can still access it.
How to rewrite the code to prevent it?