Imagine you have an interface like this:
public interface IPersonManager
{
public void AddPerson(string name);
}
... and the implementation we will call DefaultPersonManager. Say we want to be sure that any implementation IPersonManagercannot give an empty or empty string as an argument AddPerson(string name). In this case, we are going to implement the contract class as follows:
[ContractClassFor(typeof(IPersonManager))]
public abstract class IPersonManagerContract : IPersonManager
{
public void AddPerson(string name)
{
Contract.Requires(!string.IsNullOrEmpty(name), "Person name cannot be a null or empty string");
}
}
... and we will decorate our IPersonManagerinterface with the attribute ContractClassAttribute:
[ContractClass(typeof(IPersonManagerContractClass))]
public interface IPersonManager
{
public void AddPerson(string name);
}
We talked about DefaultPersonManager. It will look like this:
public class DefaultPersonManager
{
private readonly List<string> _personNames = new List<string>();
public void AddPerson(string name)
{
_personNames.Add(name);
}
}
Good!
IPersonManager, DefaultPersonManager , AddPerson SQL (.. SQL Server, ...). DbBackedPersonManager.
DbBackedPersonManager , AddPerson DbBackedPersonManager:
public void AddPerson(string name)
{
Contract.Requires(ConfigurationManager.ConnectionStrings["SomeConnectionStringId"] != null, "A connection string is required in your application/web configuration file");
}
: , AddPerson , Requires ( Q & A , , - -.).
, , ?