我正在尝试创建一个在调用GetGreeting()时使用模拟对象或代理对象的通用方法。我如何构建代码以使其更通用,即不仅仅用于GreetingService?我必须以某种方式传递WCF服务的类型吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace proxymockproblem
{
class Program
{
static void Main(string[] args)
{
GreetingServiceManager caller = new GreetingServiceManager(false);
Console.Write(caller.GetCaller().GetGreeting());
Console.ReadLine();
}
}
public interface IGreetingServiceManager
{
string GetGreeting();
}
public class proxyGreetingService : IGreetingServiceManager
{
public string GetGreeting()
{ return "hi from proxyGreetingService"; }
}
public class mockGreetingService : IGreetingServiceManager
{
public string GetGreeting()
{ return "hi from mock"; }
}
public class GreetingServiceManager
{
bool UseMock;
public GreetingServiceManager(bool usemock)
{
UseMock = usemock;
}
public IGreetingServiceManager GetCaller()
{
if (UseMock)
{
return new proxyGreetingService();
}
else
{
return new mockGreetingService();
}
}
}
}
答案 0 :(得分:1)