c# - base class implementing base interface while derived/concrete class implementing extended interface, why? -


i following book namely ".net domain driven design c#".

question based on scenario shown in class diagram below:

figure: http://screencast.com/t/a9uuljvw0

in diagram,

a) irepository interface implemented (abstract base class) repositorybase whereas,

b) irepository interface extended interface icompanyrepository (icompanyrepository : irepository).

c) icompanyrepository implemented companyrepository derived sqlrepositorybase derived repositorybase (; which, stated in point a), implements irepository parent if icompanyrepository).

d) create variable of interface icompanyrepository having reference object of clas companyrepository, below:

icompanyrepository comrep = new company repository(); 

now, if call add() function icompanyrepository variable comrep...

comrep.add();  

then add() function in repositorybase class (which parent of companyrepository) called.

my question: exact underlying object oriented rule/mechanism takes place due function add() in (abstract-base) class "repositorybase" called? convenience stating below 2 possible mechanisms: (please tell me 1 of 2 stated below correct underlying mechanism)

mechanism-1 add() function in base class "repositorybase" called because "repositorybase" implements irepoitory ? (hence making mandatory repositorybase class implement irepository in order call add() )

or

mechanism-2: add() function in base class "repositorybase" called because companyrepository implements icompanyrepository implements irepository contains definition add() function such when add() function called on (with variable of) icompanyrepository first finds definition of add in icompanyrepository , in parent interface irepository , jumps companyrepository class find implementation of add() function , not finding definition of add() function traverse upward parent class sqlrepositorybase find add() function , on, , finds function add() in repositorybase class calls add() function in repositorybase. means if have found add() function in of derived classes of repositorybase, had not traverse further upward (in parent class). of means , in order traverse derived class parent class in chain of classes find add() function, repositorybase class not need inherit directly irepository?


there additional thing in question , unable understand oo-rule applying in case stated below:

in question there 2 interfaces 1 parent namely irepository , other extending namely icompanyrepository. parent interface irepository contains deifinition of add() function not child interface icopmanyrepository.

last derived class in chain of class hierarchy "companyrepository" implements icompanyrepository (companyrepository not implement add() function of irepository interface) whereas root (top parent) (abstract base) class namely repositorybase implements add() function.

so structure image shown in http://screencast.com/t/a9uuljvw0.

now if call add() function:

codeicompanyrepository lastderived = new companyrepository(); icompanyrepository->add();code

then according oo-rule stated in answer, lookup start companyrepository class expectation companyrepository have implemented add() function code irepository.add() { } //deduced p17 , p18 in [link] http://www.codeproject.com/articles/18743/interfaces-in-c-for-beginners[link]code

but, in case class companyrepository not have implementation irepository.add() { } although flow of control (while tracing) jumps add() function in base class (and code working fine). unable understand oo-rule applying here ?

please let me know if need me show above scenario code.

that's lot of words. i'm going restate think you're asking , answer question instead. if i'm off mark, let me know.

when invoking method through interface, matter if interface explicitly declared again being implemented on more derived type in type hierarchy?

yes, called "interface re-implementation" , changes how method mapped. c# language specification (section 13.4.6 interface re-implementation) goes in little bit more detail, gist derived type specifies interface starting point lookup.

interface icreature {     void speak(); }  class animal : icreature {     public void speak() { console.writeline("rawr"); } }  class duck:animal {     public void speak() { console.writeline("quack"); } }  class human : animal, icreature {     public void speak() { console.writeline("hello"); } } 

if following, print out "rawr" , "hello".

icreature duck = new duck(); icreature human = new human(); duck.speak(); human.speak(); 

this because in duck hierarchy, derived type specifying icreature interface animal , print out "rawr".

in human hierarchy, derived type specifying icreature interface human (and human declares implementation) , print out "hello". if human type had not declared implementation, have printed "rawr".

update

in specific case, exact same rule applies. let's walk through steps.

  • icompanyrepository inherits irepository
  • companyrepository declares implements icompanyrepository
  • companyrepository has implicitly redeclared implements irepository because icompanyrepository inherits irepository

the call chain follows these steps.

  • the add() method called through instance typed icompanyrepository interface.
  • the derived type has explicitly declared implements irepository companyrepository, lookup begins there.
  • companyrepository not implement add() method directly, parent class inspected.
  • sqlrepositorybase inspected , not directly implement method, parent class inspected.
  • repositorybase inspected , it does implement method, method called.

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -