我正在尝试为我的应用程序编写一种可扩展的数据层“存储库”之一是我的抽象存储类的内存实现
public abstract class Store<TEntity, TIdentifier> : IStore<TEntity, TIdentifier>
where TEntity : StorableEntity<TIdentifier>
{
//abstract methods here
public abstract TIdentifier GetUniqueIdentifier();
}
“StorableEntity”抽象类是:
public abstract class StorableEntity<TIdentifier>
{
public TIdentifier ID { get; set; }
}
我有一个名为“InMemoryStore”的具体 Store 类,如下所示:
public class InMemoryStore<T, U> : Store<T, U>
where T : StorableEntity<U>
{
protected static Dictionary<U, T> store = new Dictionary<U, T>();
public override U GetUniqueIdentifier()
{
// call relevant "generator" here - SOMETHING LIKE THIS??
// var generator = GetGeneratorSomehow(U);
// return generator.Create();
}
}
现在,这里的“U”类型可以是字符串、整数、Guid 等……(大多数时候它可能是整数)
我的想法是创建类似这样的 IUIDGenerator 的东西:
public interface IUIDGenerator<T>
{
T Create(ICollection<T> collection);
}
在上面的“InMemoryStore”中,我将创建一个 IUIDGenerator 实例,传入存储字典键集合,并调用“Create”方法以返回所需类型的唯一标识符。
例如,我可以有一个像这样的 IntUIDGenerator 类(它可以作为一种增量数字生成器,基于字典键中已经存在的数字)
public class IntUIDGenerator : IUIDGenerator<int>
{
public int Create(ICollection<int> collection)
{
var result = collection.Max() + 1;
if (collection.Contains(result))
return result;
throw new NotUniqueException();
}
}
实际问题: 我需要做的是在 InMemoryStore 中识别 U 的类型(标识符的类型)并能够动态选择所需的 IUIDGenerator 的具体实现——我该怎么做?
我想过有一种类工厂模式 - 将所有可用的 UIDGenerators 加载到字典中......但它们都可以有不同的类型?
有没有更好的方法来解决这个问题?
另外,我知道我的问题的标题可能有点偏离 - 如果有人有更好的建议,请随时发表评论,我会改变它。