Im not dead

Help on Ignacio's Design By Contract plug-in hosted on http://code.google.com/p/postsharp-user-plugins/.

Im not dead

Postby cocotapioca on Mon Apr 14, 2008 2:14 am

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:

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

Postby gfraiteur on Mon Apr 14, 2008 8:59 pm

This seems great 8-)

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.
gfraiteur
Site Admin
 
Posts: 581
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Im not dead

Postby blahetal on Sat Apr 19, 2008 9:24 pm

nice :), cant wait to try it out
blahetal
blahetal
 
Posts: 13
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: blahetal


Return to DesignByContract


cron