Can we use Abstract class with repository design pattern instead of interface


here not asking interface or abstract class rather know can use abstract class instead of interface

see below code of repository design pattern interface has been used

if expose repository interface

public interface iemployeerepository {     employee[] getall(); }

then advantage have many implementations below

public class employeerepositoryef: iemployeerepository {     public employee[] getall()     {         //here return employees after querying ef dbcontext     } }  public class employeerepositoryxml: iemployeerepository {     public employee[] getall()     {         //here return employees after querying xml file     } }  public class employeerepositorywcf: iemployeerepository {     public employee[] getall()     {         //here return employees after querying remote wcf service     } }

see above code has 1 contract method

getall()
, ever extend interface can provide own implementation. advantage.

but question can write abstract class instead of interface ?

suppose have 1 abstract class

abstract class absemployeerepository

{ abstract public employee[] getall(); }

now other repository extend abstract class

absemployeerepository


, override function getall() give own implementation.

now question if abstract class solve purpose why need interface in scenario. multiple inheritance concern interface preferred other wise can complete job abstract class.

looking other valuable comments , suggestions.

thanks



>>can use abstract class repository design pattern instead of interface

yes.

>>but question can write abstract class instead of interface ?

you could replace interface following abstract class:

    public abstract class employeerepository {       public abstract employee[] getall();     }

>>now question if abstract class solve purpose why need interface in scenario

you don't. might replace interface abstract class if have reason so.

whether choose interface or abstract class (google or bing "interface vs abstract class" , lot of results) topic of own in general typically replace interface abstract class when want provide default implementation in abstract class. since don't provide default implementation getall() method here makes no sense use abstract class. please refer following link more information:

recommendations abstract classes vs. interfaces: https://msdn.microsoft.com/en-us/library/scsyfw1d(v=vs.71).aspx


hope helps.

please remember close threads marking helpful posts answer , start new thread if have new question. please don't ask several questions in same thread.



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'