抽象类和接口的IoC服务? [英] Abstract class or Interface for IoC service?

查看:160
本文介绍了抽象类和接口的IoC服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的IoC提供存储库的具体implemenations在一个项目我。所有这一切我已经阅读使用一个接口作为服务的定义的例子。不过,在读取微软是通过接口建议 preFER抽象类的建议。

I am currently using IoC for providing concrete implemenations of repositories in a project. All of the examples that I have read use an interface as the definition of the service. However, having read recommendations from Microsoft it is recommended to prefer abstract classes over interfaces.

我发现这个有用conjuntion与模板图案,以减少重复。例如给一个产品类属性 IsActive 我可以使用的资源库,如接口:

I have found this useful in conjuntion with the template pattern to reduce repetition. For example give a Product class with a property IsActive I could use an interface for the repository such as:

interface IProductRepository
{
    IEnumerable<Product> Load();
}

如果一个共同的任务就是装载活性产品那么我需要做的:

If a common task is to load active products then I would need to do:

IEnumerable<Product> activeProducts = repository.Load().Where(x => x.IsActive);

其中,是一个具体的实现。如果我用一个抽象类,如:

Where repository is a concrete implementation. If I used an abstract class such as:

abstract class ProductRepository
{
    protected abstract IEnumerable<Product> LoadCore();

    public IEnumerable<Product> Load()
    {
        return LoadCore().Where(x => x.IsActive);
    }
}

然后,我可以使用加载有源产品

Then I could load active products using

IEnumerable<Product> activeProducts = repository.Load();

所有的实现装载的产品信息是派生 ProductRepository 所以没有连接到持久性层contrete类中。如果发现有另一个常见的​​表现活动那么这可以被添加到基类作为非重大更改所不能使用的接口进行。

All the implementation for loading the product information is within the contrete class that derives ProductRepository so there is no coupling to the persistance layer. If it is found that there is another commonly performed activity then this could be added to the base class as a non-breaking change which could not be done using an interface.

有没有好处在于使用抽象类的接口呢?我会遇到什么潜在的缺点,使用抽象类?

Is there any benefit to using an interface instead of an abstract class? What potential drawbacks to using abstract classes could I come across?

我使用的温莎城堡作为国际奥委会控制器和.NET Framework 3.5。

I am using Castle Windsor as the IoC controller and .Net framework 3.5.

推荐答案

我不认为这个问题取决于国际奥委会;大多数这些框架不在乎你使用。

I don't think this question hinges on IoC; most of these frameworks don't care which you use.

使用接口​​在抽象基类之间最大的问题是,<一个href="http://haacked.com/archive/2008/02/21/versioning-issues-with-abstract-base-classes-and-interfaces.aspx">interfaces不能进行版本以及

The big issue between using interfaces over abstract base classes is that interfaces cannot be versioned well.

抽象基类是因为这通常是更好的选择。我不认为有任何的缺点,使用一个抽象基类,除获得支持的问题,如为什么我不能创建这种类型的实例?​​

Abstract base classes are usually the better choice because of this. I don't think there is any drawback to using an abstract base class, other than getting support questions like "why can't I create an instance of this type?"

这篇关于抽象类和接口的IoC服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆