在我的應用程序中,有一組數據提供者和Redis緩存。 每個執行不同的供應商使用,但他們都儲存自己的數據在Redis的:沒有實例創建的自我註冊類
hset ProviderOne Data "..."
hset ProviderTwo Data "..."
我想有一個方法,將刪除數據中存在的所有代碼提供商。
del ProviderOne
del ProviderTwo
我已經做下面的代碼:
void Main()
{
// Both providers have static field Hash with default value.
// I expected that static fields should be initialized when application starts,
// then initialization will call CacheRepository.Register<T>() method
// and all classes will register them self in CacheRepository.RegisteredHashes.
// But code start working only when i created this classes (at least once)
// new ProviderOne();
// new ProviderTwo();
CacheRepository.Reset();
}
public abstract class AbstractProvider
{
//...
}
public class ProviderOne : AbstractProvider
{
public static readonly string Hash =
CacheRepository.Register<ProviderOne>();
//...
}
public class ProviderTwo : AbstractProvider
{
public static readonly string Hash =
CacheRepository.Register<ProviderTwo>();
//...
}
public class CacheRepository
{
protected static Lazy<CacheRepository> LazyInstance = new Lazy<CacheRepository>();
public static CacheRepository Instance
{
get { return LazyInstance.Value; }
}
public ConcurrentBag<string> RegisteredHashes = new ConcurrentBag<string>();
public static string Register<T>()
{
string hash = typeof(T).Name;
if (!Instance.RegisteredHashes.Contains(hash))
{
Instance.RegisteredHashes.Add(hash);
}
return hash;
}
public static void Reset()
{
foreach (string registeredHash in Instance.RegisteredHashes)
{
Instance.Reset(registeredHash);
}
}
protected void Reset(string hash);
}
interface IData{}
interface IDataProvider
{
string GetRedisHash();
IData GetData();
}
intefrace IRedisRepository
{
}
如何使它的工作?
您不一定需要創建實例。一旦任何靜態方法或屬性被調用,它們將被初始化。是否有什麼原因需要它們在你使用它們之前自動初始化? – TyCobb
你可能想檢查這個問題。也許一個接口和一些反射會很好地與第二個答案:http://stackoverflow.com/questions/8748492/how-to-initialize-ac-sharp-static-class-before-it-is-actually-needed – TyCobb
這個類是否需要自己註冊,或者你是否只想在加載dll時註冊類? – FriendlyGuy