3
我是代码合同中的新手,我对如何将其与IoC集成进行了一个问题。 我试过在简单的测试程序(经典的CONSOLLE项目)中使用代码合同,但现在我会在我的官方项目中使用它。 问题是:如果我有一个容器在类的构造方法中提供我的服务接口,那么如何使用代码合同检查传递的值?与类构造方法中的IoC代码合同
经典的场景可能是
[ContractClass(typeof(ArticoliBLLContract))]
public interfare IMyInterface
{
void method1(int a)
void method2(string b)
void method3(bool c)
}
[ContractClassFor(typeof(IArticoliBLL))]
public abstract class MyContractsClass : IMyInterface
{
public void method1(int a)
{
Contract.Requires<ArgumentOutOfRangeException>(a > 0,"a must be > 0");
}
public void method2(string b)
{
Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(b),"b must be not empty");
}
public void method3(bool c)
{
Contract.Requires<ArgumentOutOfRangeException>(c == true,"c must be true");
}
}
public class MyClass : IMyInterface
{
private ITFactory _factory = null;
private IMyDAL _myDAL = null;
public MyClass(ITFactory factory,IMyDAL myDAL)
{
if (factory == null)
throw new ArgumentNullException("factory");
if (myDAL == null)
throw new ArgumentNullException("myDAL");
_factory = factory;
_myDAL = myDAL;
}
public void method1(int a)
{
a = a*2;
}
public void method2(string b)
{
b = b + "method2";
}
public void method3(bool c)
{
c = false;
}
}
在这种情况下,我在哪里可以查看“工厂”和“myDAL”? 有没有办法将抽象类中的合约放在同一个类中?
谢谢!!!
谢谢!我会尝试与sublclasses! – Faber 2011-04-18 10:33:01