I need a little pattern direction here. New to C#.
I'm working with a third-party development kit that wraps a web service. There are two specific classes I deal with that, while relatively similar, are in two different namespaces in the dev kit and there's no common base class. I'd like to program against a common interface for them both however. I haphazardly threw together an implementation that essentially wraps the wrapper, but I feel rather certain it's not the most efficient method due to the incessant type casting.
I've been digging through articles on adapters, interfaces, extension methods, etc., but I'm running low on time, so if I could get a push in one direction that'd be greatly appreciated.
using ThirdParty.TypeA.Employee;
using ThirdParty.TypeB.Employee;
public class Employee
{
private object genericEmployee;
private EmployeeType empType;
public enum EmployeeType
{
TypeA = 0;
TypeB = 1;
}
public Employee(Object employee, EmployeeType type)
{
genericEmployee = employee;
empType = type;
}
public String Name
{
if (empType == EmployeeType.TypeA)
return (ThirdParty.TypeA.Employee)genericEmployee.Name;
else
return (ThirdParty.TypeB.Employee)genericEmployee.Name;
}
public String Age
{
if (empType == EmployeeType.TypeA)
return (ThirdParty.TypeA.Employee)genericEmployee.Age;
else
return (ThirdParty.TypeB.Employee)genericEmployee.Age;
}
}
Rev 2:
class EmployeeTypeAAdapter : TypeA, IEmployeeAdapter
{
TypeA _employee;
public EmployeeTypeAAdapter(TypeA employee)
{
_employee = employee
}
public String Name
{
get { return _employee.Name; }
set { _employee.Name = value; }
}
public String Balance
{
get
{
if (_employee.Balance != null)
{
decimal c = _employee.Balance.Amount;
return String.Format("{0:C}", c);
}
else
{
return "";
}
}
}
//...
}
class EmployeeTypeBAdapter : TypeB, IEmployeeAdapter
{
TypeB _employee;
public EmployeeTypeAAdapter(TypeB employee)
{
_employee = employee
}
public String Name
{
get { return _employee.Name; }
set { _employee.Name = value; }
}
public String Balance
{
get
{
if (_employee.Balance != null)
{
decimal c = _employee.Balance.Amount;
return String.Format("{0:C}", c);
}
else
{
return "";
}
}
}
//....
}
object
? – Norean