开发者

proxy/mock object in c# for wcf

开发者 https://www.devze.com 2023-02-13 07:20 出处:网络
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 GreetingSer

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号