如何干掉.NET webforms包装器?

时间:2018-03-07 14:54:29

标签: c# asp.net unit-testing webforms

大量修改:

所以,我已经将一个ASP.NET代码重构为Presenter,以进行单元测试。完成后,我注意到我所做的包装有很多重复,我想消除。我的想法是使用类继承来抽象复制,但我无法弄明白。我目前拥有的一个例子:

[TestFixture]
public class PresenterTests
{
    [Test]
    public void TestMethodTest()
    {
        var fooMock = Mock<IFooWrapper>();
        var fooMock2 = Mock<IFooWrapper>();
        var barMock = Mock<IBarWrapper>();
        var barMock2 = Mock<IBarWrapper>();

        fooMock.Setup(x => x.Baz()).Returns(fooMock2);
        barMock.Setup(x => x.Baz()).Returns(bazMock2);
    }
}

public interface IFooWrapper
{
    IFooWrapper Baz();
}

public class FooWrapper : IFooWrapper
{
    private Foo _instance;

    public FooWrapper(Foo instance)
    {
        _instance = instance;
    }

    public IFooWrapper Baz()
    {
        return new FooWrapper(_instance);
    }
}

public interface IBarWrapper
{
    void Baz();
}

public class BarWrapper : IBarWrapper
{
    private Bar _instance;

    public BarWrapper(Bar instance)
    {
        _instance = instance;
    }

    public IBarWrapper Baz()
    {
        return new BarWrapper(_instance);
    }
}

基于史蒂夫哈里斯在下面基于我非常糟糕的沟通所说的话,我目前有以下似乎很接近但不完全存在:

public interface IBaseWrapper<TWrapper, T> where TWrapper: IBaseWrapper<TWrapper, T> where T : class
{
    TWrapper Baz();
}

public class BaseWrapper<TWrapper, T> : IBaseWrapper<TWrapper, T> where TWrapper : BaseWrapper<TWrapper, T> where T : class
{
    public T Instance;

    public BaseWrapper()
    {
    }

    public BaseWrapper(T instance)
    {
        Instance = instance;
    }

    public TWrapper Baz()
    {
        return new TWrapper(Instance);
    }
}

public interface IFooWrapper : IBaseWrapper<FooWrapper, Foo>
{
}

public class FooWrapper : BaseWrapper<FooWrapper, Foo>, IFooWrapper
{
    public FooWrapper() {}
    public FooWrapper(T instance) : base(instance) {}
}

public interface IBarWrapper : IBaseWrapper<BarWrapper, Bar>
{
}

public class BarWrapper : BaseWrapper<BarWrapper, Bar>, IBarWrapper
{
    public BarWrapper() {}
    public BarWrapper(T instance) : base(instance) {}
}

3 个答案:

答案 0 :(得分:1)

这些是您正在寻找的限制吗?

public interface IBaseWrapper<TWrapper, T> where TWrapper : IBaseWrapper<TWrapper, T>
{
    TWrapper Baz();
}

public class BaseWrapper<TWrapper, T> : IBaseWrapper<TWrapper, T> where TWrapper : BaseWrapper<TWrapper, T> where T : class
{
    private T _instance;

    public BaseWrapper(T instance)
    {
        _instance = instance;
    }

    public TWrapper Baz()
    {
        // note whatever wrapper class you are using must have a
        // constructor matching this base class
        return Activator.CreateInstance(GetType(), new object[] { _instance });
    }
}

public class Foo
{
    public void FooSpecificMethod()
    {
        // foo stuff
    }
}

public interface IFooWrapper<TWrapper> : IBaseWrapper<TWrapper, Foo> where TWrapper : IBaseWrapper<TWrapper, Foo>
{
    void FooSpecificMethod();
}

public class FooWrapper : BaseWrapper<FooWrapper, Foo>, IFooWrapper<FooWrapper>
{
    public FooWrapper(Foo instance) : base(instance)
    {
    }

    public void FooSpecificMethod()
    {
        instance.FooSpecificMethod();
    }
}

答案 1 :(得分:0)

您可能使用factory method实施。

public class BaseWrapper
{
    public virtual void Baz()
    {
       // Do some shared work
    }
}

public class BarWrapper : BaseWrapper
{
    private Bar _instance;

    public BarWrapper(Bar instance)
    {
        _instance = instance;
    }

    public override void Baz()
    {
        base.Baz();
        // Do custom stuff for this wrapper type?
    }
}

当我尝试构建一段代码时,我通常会使用Generic实现,这些代码可以处理对继承共享接口公共类IFileParser的某些对象集合进行处理,其中IFileImplementation可以是各种各样的东西。

希望这有帮助!

答案 2 :(得分:0)

从您的示例代码中,看起来不需要泛型。您可能正在实现Decorator模式。

此示例说明了代码的非泛型版本。这是你之后的事吗?

public interface IBazzer
{
    void Baz();
}

public class TheBaz : IBazzer
{
    public void Baz()
    {
        Console.WriteLine("Hello World");
    }
}
public class Wrapper : IBazzer
{
    private IBazzer _instance;

    public Wrapper(IBazzer instance)
    {
        _instance = instance;
    }

    public void Baz()
    {
        // do stuff before calling the wrapped Bazzer
        this.PreBaz();

        _instance.Baz();

        // do stuff after calling the wrapped Bazzer        
        this.PostBaz();
    }

    protected virtual void PreBaz(){}
    protected virtual void PostBaz(){}
}

public class FooWrapper : Wrapper
{
    public FooWrapper(IBazzer wrapped):base(wrapped){}

    protected override void PreBaz()
    {
        Console.WriteLine("Foo Pre Baz");
    }
    protected override void PostBaz()
    {
        Console.WriteLine("Foo Post Baz");
    }

}

public class BarWrapper : Wrapper
{
    public BarWrapper(IBazzer wrapped):base(wrapped){}

    protected override void PreBaz()
    {
        Console.WriteLine("Bar Pre Baz");
    }
    protected override void PostBaz()
    {
        Console.WriteLine("Bar Post Baz");
    }
}

void Main()
{
    var x = new     TheBaz();
    var y = new FooWrapper(x);

    y.Baz(); 
    // Foo Pre Baz
    // Hello World
    // Foo Post Baz
}