服务层中特定于实现的常量

时间:2013-03-21 21:47:09

标签: c# asp.net architecture interface

我有一个ASP.Net Web窗体项目,该项目是(除其他事项外)CRM Web服务的用户界面。

我有一个接口IMembershipService,它公开了许多方法来购买不同类型的服务订阅。具体实现SpecificMemberService将抽象许多Web服务调用到第三方CRM系统。

具体实现要求将不同的“订阅级别”作为特定的4字符字符串代码传递给它。目前,我已在服务层中定义了以下内容:

public static class MemberTypes
{
    public const string Basic = "MEM1";
    public const string Extra = "MEM2";
    public const string Professional = "MEM3";
}

但是,这些代码特定于具体类SpecificMemberService,并且 - 因此 - 不应该像它们当前那样独立于它。如何向我的Web应用程序公开强类型MemberType代码,这些代码与IMembershipService具体实现相关?

1 个答案:

答案 0 :(得分:2)

创建“成员类型”界面。向服务接口添加GetMemberTypes方法,公开集合IMemberType

public interface IMemberType {      
    public string Name { get; set; } // i.e. "Professional"
    public string Code { get; set; } // i.e. "MEM3"
}

public void ClientCode() {

    // Instantiate a concrete service (calling a factory would be even better).
    IMembershipService service = new MembershipService();

    // Get the list of MemberTypes exposed by the concrete MembershipService.
    Collecton<IMemberTypes> types = service.GetMemberTypes();

    // Subscribe to service defined in IMembershipService with "Professional" level, if possible.
    foreach (IMemeberType type in types) {
        if ((type.Name == "Professional"))
            service.SubscribeToAwesomeService(type.Code);
    }
}

将“专业”展示为奖励积分的常数或枚举。