1

背景:

我不确定如何表达这个问题,所以这是我想要实现的示例:

public interface IEverythingVM : IA, IB
{
    MyTypeA {get;}
    MyTypeB {get;}
    MyTypeC {get;}

    MyTypeD {get;}
    MyTypeE {get;}
    MyTypeF {get;}
}

public class EverythingVM : IEverythingVM
{
    // Populate everything from MyTypeA to MyTypeF
}

public interface IA
{
    MyTypeA {get;}
    MyTypeB {get;}
    MyTypeC {get;}
}


public interface IB
{
    MyTypeD {get;}
    MyTypeE {get;}
    MyTypeF {get;}
}

实现:

我的印象是我可以做类似以下的事情,但是写出来之后,它为什么不起作用就很明显了——我正在尝试做一些大的事情,去定义它,然后重新定义它,这是一个概念太荒谬了,让我想大声笑出来。

{
    IA varIA = new EverythingVM(param1, param2);
    IB varIB = new EverythingVM(param1, param2);

    var cm = new ComparisonManager(varIA, varIB);        
}

public class ComparisonManager
{
    public ComparisonManager(IEverythingVM varEIA, IEverythingVM varEIB)
    {
        // be able to acces MyTypeA, MyTypeB & MyTypeC from varEIA
        // be able to acces MyTypeD, MyTypeE & MyTypeF from varEIB
    }
}

问题

最终,我想要实现的是,在某些情况下我想要MyTypeAMyTypeB&MyTypeC可用ComparisonManager,而在其他情况下,我想要MyTypeDMyTypeE&MyTypeF可用ComparisonManager。鉴于当前的结构,我有什么办法可以实现这一目标?

4

2 回答 2

1

你为什么不这样做:

public class ComparisonManager
{
    public ComparisonManager(IA varEIA, IB varEIB) {}
}

?

于 2012-08-30T15:37:59.567 回答
1

事实证明,解决方案是:

public class ComparisonManager<T>
{
    private readonly T _diskServiceVm;
    private readonly T _panelServiceVm;

    public ComparisonManager(T diskServiceVm, T panelServiceVm)
    {
        _diskServiceVm = diskServiceVm;
        _panelServiceVm = panelServiceVm;
    }
}

执行:

if (/* circumstance A */)
{
    IA varIA = new A(varA, varB);
    IA varIB = new B(varA, varB);

    // this knows how to deal with MyTypeA, MyTypeB & MyTypeC
    var comparisonManager = new ComparisonManager<IA>(varIA , varIB);
}
else
{
    IB varIA = new A(varA, varB);
    IB varIB = new B(varA, varB);

    // this knows how to deal with MyTypeD, MyTypeE & MyTypeF
    var comparisonManager = new ComparisonManager<IB>(varIA , varIB);
}
于 2012-08-30T16:14:10.037 回答