코드 계약에서 새로 왔으며 IoC와 통합하는 방법에 대한 질문이 있습니다. 간단한 테스트 프로그램 (고전적인 consolle 프로젝트)에서 코드 계약을 사용하려했지만, 이제는 공식 프로젝트에서 코드 계약을 사용하려고합니다. 질문 : 클래스의 생성자 메서드에서 서비스 인터페이스를 제공하는 컨테이너가있는 경우 코드 계약을 사용하여 전달 된 값을 확인하려면 어떻게해야합니까?클래스 생성자 메서드에서 IoC와 코드 계약
고전적인 시나리오는 내가 "공장"과 "myDAL"를 확인하실 수 있습니다이 시나리오에서는
[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;
}
}
을 수 있을까? 계약을 추상 클래스에 두어 같은 클래스에 보관할 수 있습니까?
감사합니다.
감사합니다! 서브 클래스로 시도 할게! – Faber