Deep Levels of Inheritance and Code Contract Behaviors
i've scaled down , simplified code i've taken big project illustrate problem. however, levels of inheritance correctly represented. code follows:
public interface icontext : iunitofwork, isql, idisposable { /// <summary> /// apply changes made in item or related items in graph /// </summary> /// <typeparam name="tentity">type of item</typeparam> /// <param name="item">item changes</param> void setchanges<tentity>(tentity item) where tentity : class, iobjectwithchangetracker, new(); } [contractclass( typeof( irepositorycontract<> ) )] public interface irepository<tentity> where tentity : class, new( ) { /// <summary> /// context in repository /// </summary> icontext storecontext { get; } ..... } [contractclassfor( typeof( irepository<> ) )] internal abstract class irepositorycontract<tentity> : irepository<tentity> where tentity : class, new( ) { #region irepository<tentity> members public icontext storecontext { get { contract.ensures( contract.result<icontext>( ) != null ); contract.ensures( contract.result<icontext>( ) iunitofwork ); throw new notimplementedexception( ); } } } public interface ipersonrepository : irepository<person> { .... } public class genericrepository<tentity> : irepository<tentity> where tentity : class,iobjectwithchangetracker, new( ) { ...... public icontext storecontext { get { //contract.ensures( contract.result<icontext>( ) != null ); removed these contracts since included in irepository //contract.ensures( contract.result<icontext>( ) iunitofwork ); return _context icontext; } } [contractinvariantmethod] private void objectinvariant( ) { contract.invariant( _context != null ); } ...... } public class personrepository : genericrepository<person>, ipersonrepository { .... } public class personservice() { public void save( ipersonrepository personrepository ) { //dependency injection substitutes personrepository concrete class .... var unitofwork = ( iunitofwork )_personrepository.storecontext; unitofwork.commit( ); // codecontract: possibly calling method on null reference 'unitofwork' } }
in personservice class, still getting contract warning unitofwork might null, though ensured won't null in irepository interface.
can tell me missing?
i hadn't turned on contracts , set contract reference assembly build projects interfaces reside. since enabling that, working expected.
DevLabs > Code Contracts
Comments
Post a Comment