Ninject可以使用匿名委托(func)作为ConstructorArgument吗?

时间:2013-04-19 02:26:51

标签: c# ninject func constructor-injection

我有一个存储库抽象类,它几乎封装了所有CRUD功能:

public abstract class DataRepository<T> : IRepository<T>
    where T : class
{
    public DataContext Context { get; private set; }
    public TransactionScope Transaction { get; private set; }

    /// <summary>
    /// A <see cref="bool"/> function that compares the keys for fetching a single item, for example: 
    /// return item1.Id == item2.Id (as an anonymous delegate).
    /// </summary>
    public Func<T, T, bool> KeyCompare { get; private set; }

    /// <summary>
    /// Creates a new data repository.
    /// </summary>
    /// <param name="context"></param>
    /// <param name="scope"></param>
    /// <param name="keyCompare">
    /// A <see cref="bool"/> function that compares the keys for fetching a single item, for example: 
    /// return item1.Id == item2.Id (as an anonymous delegate).
    /// </param>
    public DataRepository(DataContext context, TransactionScope scope, Func<T, T, bool> keyCompare)
    {
        Context = context;
        Transaction = scope;
        KeyCompare = keyCompare;
    }

    public virtual T Item(T item)
    {
        return Items().SingleOrDefault(e => KeyCompare(e, item));
    }

    public virtual IEnumerable<T> Items()
    {
        return DataTable.AsEnumerable();
    }

    protected virtual Table<T> DataTable { get { return Context.GetTable<T>(); } }

    /// <summary>
    /// A method that updates the non-key fields of an existing entity with those of specified <see cref="item"/>.
    /// Called by the <see cref="Save"/> method.
    /// </summary>
    /// <param name="existing">The existing record to update.</param>
    /// <param name="item">A <see cref="T"/> object containing the values to update <see cref="existing"/> object with.</param>
    /// <returns></returns>
    protected abstract void UpdateExisting(T existing, T item);

    /// <summary>
    /// A method that updates an existing item or creates a new one, as needed.
    /// </summary>
    /// <param name="item">The entity containing the values to be saved.</param>
    public virtual void Save(T item)
    {
        var existing = Item(item);
        if (existing != null)
        {
            UpdateExisting(existing, item);
        }
        else
        {
            DataTable.InsertOnSubmit(item);
        }

        Context.SubmitChanges();
    }

    /// <summary>
    /// A method that saves all specified items (creates new, updates existing).
    /// </summary>
    /// <param name="items">The entities to be saved.</param>
    public virtual void Save(IEnumerable<T> items)
    {
        foreach (var item in items)
        {
            Save(item);
        }
    }

    /// <summary>
    /// A method that deletes specified item.
    /// </summary>
    /// <param name="item"></param>
    public virtual void Delete(T item)
    {
        var existing = Item(item);
        if (existing != null)
        {
            DataTable.DeleteOnSubmit(existing);
        }

        Context.SubmitChanges();
    }

    public virtual void Delete(IEnumerable<T> items)
    {
        var selection = Items().Where(e => items.Any(item => KeyCompare(e, item)));
        DataTable.DeleteAllOnSubmit(selection);

        Context.SubmitChanges();
    }
}

在派生类中使用KeyCompare属性,以便基类知道如何隔离存储库中的单个项目(并非所有“实体”都具有“Id”属性,以及某些键跨多个列 - 此解决方案尝试解决该特定点:

    public AuthInfoRepository(DataContext context, TransactionScope scope)
        : base(context, scope, (item1, item2) => { return item1.Id == item2.Id;})
    { }

这个KeyCompare属性实际上是允许派生类仅实现UpdateExisting方法的基石,如下所示:

    protected override void UpdateExisting(AuthInfo existing, AuthInfo item)
    {
        existing.AuthId = item.AuthId;
        existing.ActiveDirectoryGroup = item.ActiveDirectoryGroup;
    }

其余的(实际的CRUD)全部由基类处理。有了这个抽象存储库,我已经在几分钟内实现了具体的存储库,如果不是几秒钟,只编写特定于每个实现的代码。所以,我很渴。

DataRepository<T>处理SQL Server,所以我还需要另一个模拟实现,我称之为ListRepository<T>并完全相同(除了Context和{ {1}}属性都返回Transaction)。我认为构造函数的签名是我需要在这里发布的所有内容:

null

所以现在我已准备好进行测试,我想使用Ninject作为我的IoC容器。我的问题是我似乎无法弄清楚如何将匿名委托作为public ListRepository(IEnumerable<T> items, Func<T, T, bool> keyCompare) 传递:

ConstructorArgument

我正在努力做到可行,还是过于复杂?我不打算问它是否是好的/干净的代码,但欢迎提出建设性的意见:

https://codereview.stackexchange.com/questions/25250/code-review-for-an-abstract-repository-implementation

1 个答案:

答案 0 :(得分:3)

哎呀。我讨厌这种情况发生(即在发布后找到答案的几分钟)

我所需要的只是一个明确的演员:

.WithConstructorArgument("keyCompare", (Func<AuthInfo, AuthInfo, bool>)((item1, item2) => item1.Id == item2.Id));