C# -Fluent interface implementation Help

Posted by nettguy on Stack Overflow See other posts from Stack Overflow or by nettguy
Published on 2010-04-15T10:10:22Z Indexed on 2010/04/15 10:13 UTC
Read the original article Hit count: 226

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about c#3.0

Related posts about code-review