我有一个存储库抽象类,它封装了几乎所有的 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
和Transaction
属性都 return null
)。我认为构造函数的签名就是我需要在这里发布的全部内容:
public ListRepository(IEnumerable<T> items, Func<T, T, bool> keyCompare)
所以现在我已经准备好进行测试了,我想使用 Ninject 作为我的 IoC 容器。我的问题是我似乎无法弄清楚如何将匿名委托作为ConstructorArgument
:
Bind<IRepository<AuthInfo>>().To<ListRepository<AuthInfo>>()
.WithConstructorArgument("items", _mockAuthInfo)
.WithConstructorArgument("keyCompare", ??????);
我正在尝试做的事情是可行的,还是过于复杂?我不会问它是否是好/干净的代码,但欢迎在这里提出建设性意见: