I am currently using IoC to provide specific repository implementations in a project. All the examples I read use the interface as a service definition. However, after reading the recommendations from Microsoft, it is recommended that abstract classes be preferred over interfaces .
I found this useful in combination with a template pattern to reduce repetition. For example, give the Product class with the IsActive property, I could use the interface for the repository, for example:
interface IProductRepository { IEnumerable<Product> Load(); }
If the general goal is to download active products, then I will need:
IEnumerable<Product> activeProducts = repository.Load().Where(x => x.IsActive);
Where repository is a specific implementation. If I were to use an abstract class, for example:
abstract class ProductRepository { protected abstract IEnumerable<Product> LoadCore(); public IEnumerable<Product> Load() { return LoadCore().Where(x => x.IsActive); } }
Then I could download active products using
IEnumerable<Product> activeProducts = repository.Load();
The entire implementation for loading product information is inside the class class that receives the ProductRepository , so there is no connection with the level of stability. If it is discovered that there is another activity usually performed, then this can be added to the base class as an inextricable change that cannot be made using the interface.
Is there any benefit of using an interface instead of an abstract class? What potential flaws in using abstract classes can I find?
I use Castle Windsor as an IoC controller and .Net framework 3.5.
detaylor
source share