proxy/mock object in c# for wcf
I am trying to create a generic way of using either a mock object or the proxy object when calling the GetGreeting(). How can i structure the code so it is more generic ie not only for the GreetingService? I have to somehow pass in the typeof WCF service i guess?
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().GetGr开发者_StackOverfloweeting());
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();
}
}
}
}
take a look a the ChannelFactory class
If you want a proxy for wcf specifically that should do the trick
for doing tests you can use one of the mocking frameworks outthere such as moles, nmock or moq
精彩评论