0

我认为这是我第一次无法仅通过搜索找到问题的答案。

我正在为我们公司系统的改造做一些原型,但我遇到了一个我似乎无法理解的问题。

我正在尝试使用合理通用的 DAO 耦合依赖注入。我遇到的问题是我想在接口的方法中使用第三方库进行查询定义。

一个例子

public interface TestDAO<TEntity>
{
    /// <summary>
    /// get ALL the things
    /// </summary>
    /// <returns></returns>
    IEnumerable<TEntity> GetAll();

    /// <summary>
    /// Runs a supplied "where" clause and returns ALL the matched results
    /// </summary>
    IEnumerable<TEntity> FindAll(Specification<TEntity> specification);
}

在这种情况下,规范是来自名为 LinqSpecs 的第三方库的对象。

据我了解,通过这样做,我实质上是在强制依赖第三方库来实现接口的任何实现。

我知道我可以放弃使用该库,但如果可能的话,我想保留它,至少在我们评估它的有用性之前。

我的问题是,有没有一种方法可以做到这一点(或类似的事情)而不用拖第三方依赖。

提前致谢。

4

2 回答 2

0

你可以添加扩展方法,如果有人要使用Specification,他会LinqSpec额外获得依赖

namespace Dao
{
    public interface TestDAO<TEntity>
    {
        /// <summary>
        /// get ALL the things
        /// </summary>
        /// <returns></returns>
        IEnumerable<TEntity> GetAll();
    }

namespace DaoExtensions
{
    public static class TestDAOExtensions
    {
        /// <summary>
        /// Runs a supplied "where" clause and returns ALL the matched results
        /// </summary>
        publicc static IEnumerable<TEntity> FindAll(this TestDAO<TEntity> dao, 
                                                    Specification<TEntity> specification);
    }
}
于 2012-08-21T03:27:27.530 回答
0

您实际上希望为您的界面 TestDAO 提供一个可以隐藏在您自己的界面后面的搜索过滤器

    public interface TestDAO<TEntity>
    {
        IEnumerable<TEntity> FindAll(ISearchFilter<TEntity> specification);
    }

    public interface ISearchFilter<TEntity>
    {
    }

searchfilter 实现包含实际数据

    public class LinqSpecsSearchFilter : ISearchFilter<TEntity>
    {
        Specification<TEntity> specification;
    }

这种方法的缺点是您的 TestDAO 实现必须进行一些丑陋的转换才能获得 LinqSpecs.Specification

于 2012-08-21T05:05:07.043 回答