Im not dead
3 posts • Page 1 of 1
Im not dead
Hi everybody.
You must know im a lazy guy. But i have burst of work. In a few days ill release a new version of design by contract. The new feature is that you can define custom contracts as attributes, and the code that verifies the contract is inject into the methods directly (this is like LinFu definitions).
Ex:
So you can define preconditions and postconditions to methods. And invariants to classes.
In addition, contracts for parameter are like this:
This is like the old NonNull attribute.
I still need to finish some things and ill upload the code.
In the meanwhile ill hear comments.
See ya.
You must know im a lazy guy. But i have burst of work. In a few days ill release a new version of design by contract. The new feature is that you can define custom contracts as attributes, and the code that verifies the contract is inject into the methods directly (this is like LinFu definitions).
Ex:
- Code: Select all
[AccountContract]
public class Account
{
private double m_stock = 0;
[WithdrawContract]
public void Withdraw(double amount)
{
Stock -= amount;
}
[DepositContract]
public void Deposit(double amount)
{
Stock += amount;
}
public double Stock { get { return m_stock; } private set { m_stock = value; } }
}
public class WithdrawContractAttribute : MethodContractAttribute
{
[Precondition]
public bool AmountNonNegative([Parameter] double amount)
{
return amount >= 0;
}
[Precondition]
public bool AmountLessOrEqualsToStock([Parameter] double amount, [Subject] Account account)
{
return account.Stock >= amount;
}
[Postcondition]
public bool VerifyWithdraw(
[Parameter] double amount,
[OldSubject] Account oldSubject,
[NewSubject] Account newSubject)
{
return oldSubject.Stock - amount == newSubject.Stock;
}
}
public class DepositContractAttribute : MethodContractAttribute
{
[Precondition]
public bool AmountNonNegative([Parameter] double amount)
{
return amount >= 0;
}
[Postcondition]
public bool VerifyDeposit(
[Parameter] double amount,
[OldSubject] Account oldSubject,
[NewSubject] Account newSubject)
{
return oldSubject.Stock + amount == newSubject.Stock;
}
}
public class AccountContractAttribute : TypeContractAttribute
{
[Invariant]
public bool EnsureStock([Subject] Account account)
{
return account.Stock >= 0;
}
}
So you can define preconditions and postconditions to methods. And invariants to classes.
In addition, contracts for parameter are like this:
- Code: Select all
public class NonNullAttribute : ParameterContractAttribute
{
[Precondition]
public bool Condition([Subject] object subject)
{
return subject == null;
}
}
/*some method*/
public void SomeMethod([NonNull] SomeType param) { ... }
This is like the old NonNull attribute.
I still need to finish some things and ill upload the code.
In the meanwhile ill hear comments.
See ya.
Ignacio Vivona
- cocotapioca
- Posts: 19
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Ignacio Vivona
Re: Im not dead
This seems great
Let me know when you're ready, I'll blog about this!
Gael
Let me know when you're ready, I'll blog about this!
Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 608
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
3 posts • Page 1 of 1