8

我在这里需要一点图案方向。C# 新手。

我正在使用包装 Web 服务的第三方开发工具包。我处理了两个特定的类,虽然相对相似,但它们位于开发工具包中的两个不同的命名空间中,并且没有通用的基类。但是,我想针对他们俩的通用接口进行编程。我随意地将一个基本上包装了包装器的实现拼凑在一起,但由于不断的类型转换,我觉得它不是最有效的方法。

我一直在挖掘关于适配器、接口、扩展方法等的文章,但我的时间不多了,所以如果我能朝一个方向推进,我将不胜感激。

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;
     }
 }

修订版 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 "";
            }
          }
       }  

     //....
}
4

2 回答 2

9

试试这个方法:

public interface IEmployeeAdapter
{
    string Age { get; set; }
    string Name { get; set; }
}

class EmployeeTypeAAdapter : TypeA, IEmployeeAdapter
{
    public EmployeeTypeAAdapter(TypeA employee) { }
}

class EmployeeTypeBAdapter : TypeB, IEmployeeAdapter
{
    public EmployeeTypeBAdapter(TypeB employee) { }
}

public static class EmployeeAdapterFactory
{
    public static IEmployeeAdapter CreateAdapter(object employee, EmployeeType type)
    {
        switch (type)
        {
            case EmployeeType.TypeA: return new EmployeeTypeAAdapter((TypeA)employee);
            case EmployeeType.TypeB: return new EmployeeTypeBAdapter((TypeB)employee);
        }
    }

    // or without enum

    public static IEmployeeAdapter CreateAdapter(object employee)
    {
        if (employee is TypeA) return new EmployeeTypeAAdapter((TypeA)employee);
        if (employee is TypeB) return new EmployeeTypeABdapter((TypeB)employee);
    }

    // or better introduce sort of type map
}

另一个专有名称是 EmployeeProxy,如您所愿。

于 2013-01-23T18:54:16.920 回答
3

您正在尝试做的事情被称为Duck typing。您可以使用适配器类和共享接口来做到这一点,但是手动创建这些适配器需要大量重复的胶水代码。您可以绕过编写胶水代码的一种方法是动态构造适配器类型。您可以通过 IL Emit 自己完成此操作(如果您以前从未有机会玩过它,这是一个值得练习的练习,尽管可能需要考虑很多边界情况。)如果您只是对让它工作感兴趣,但是,您可以查看这个项目 as a place to start. The C# 'dynamic' type could also be used (and winds up doing some of the same code generation behind the scenes), but it doesn't give you a reference you can pass around to non-dynamic code as if it were an interface type.

于 2013-01-23T23:54:05.690 回答