I need a small drawing direction here. New in C #.
I work with a third-party developer kit that wraps a web service. There are two specific classes that I'm dealing with relatively similar that are in two different namespaces in devkit and there is no common base class. I would like the program against a common interface for both of them. I accidentally put together an implementation that essentially wraps the wrapper, but I am sure that this is not the most efficient method due to the ongoing casting.
I have been digging articles about adapters, interfaces, extension methods, etc., but I don't have much time, so if I could get a push in one direction, that would be very useful.
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 ""; } } }
source share