Refactoring singleton implementation for DatabaseFactory and Repository Cache Providers as per recommendations from http://csharpindepth.com/articles/general/singleton.aspx
Adding unit test for DatabaseFactory.
This commit is contained in:
@@ -13,23 +13,15 @@ namespace Umbraco.Core.Persistence.Caching
|
||||
internal class InMemoryCacheProvider : IRepositoryCacheProvider
|
||||
{
|
||||
#region Singleton
|
||||
private static volatile InMemoryCacheProvider _instance;
|
||||
private static readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
|
||||
|
||||
private InMemoryCacheProvider() { }
|
||||
private static readonly Lazy<InMemoryCacheProvider> lazy = new Lazy<InMemoryCacheProvider>(() => new InMemoryCacheProvider());
|
||||
|
||||
public static InMemoryCacheProvider Current
|
||||
public static InMemoryCacheProvider Current { get { return lazy.Value; } }
|
||||
|
||||
private InMemoryCacheProvider()
|
||||
{
|
||||
get
|
||||
{
|
||||
using (new WriteLock(Lock))
|
||||
{
|
||||
if (_instance == null) _instance = new InMemoryCacheProvider();
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private readonly ConcurrentDictionary<string, IEntity> _cache = new ConcurrentDictionary<string, IEntity>();
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Umbraco.Core.Models.EntityBase;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Caching
|
||||
@@ -9,23 +8,15 @@ namespace Umbraco.Core.Persistence.Caching
|
||||
internal class NullCacheProvider : IRepositoryCacheProvider
|
||||
{
|
||||
#region Singleton
|
||||
private static volatile NullCacheProvider _instance;
|
||||
private static readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
|
||||
|
||||
private NullCacheProvider() { }
|
||||
private static readonly Lazy<NullCacheProvider> lazy = new Lazy<NullCacheProvider>(() => new NullCacheProvider());
|
||||
|
||||
public static NullCacheProvider Current
|
||||
public static NullCacheProvider Current { get { return lazy.Value; } }
|
||||
|
||||
private NullCacheProvider()
|
||||
{
|
||||
get
|
||||
{
|
||||
using (new WriteLock(Lock))
|
||||
{
|
||||
if (_instance == null) _instance = new NullCacheProvider();
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Implementation of IRepositoryCacheProvider
|
||||
|
||||
@@ -13,26 +13,18 @@ namespace Umbraco.Core.Persistence.Caching
|
||||
internal sealed class RuntimeCacheProvider : IRepositoryCacheProvider
|
||||
{
|
||||
#region Singleton
|
||||
private static volatile RuntimeCacheProvider _instance;
|
||||
private static readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
|
||||
|
||||
private RuntimeCacheProvider() { }
|
||||
private static readonly Lazy<RuntimeCacheProvider> lazy = new Lazy<RuntimeCacheProvider>(() => new RuntimeCacheProvider());
|
||||
|
||||
public static RuntimeCacheProvider Current
|
||||
public static RuntimeCacheProvider Current { get { return lazy.Value; } }
|
||||
|
||||
private RuntimeCacheProvider()
|
||||
{
|
||||
get
|
||||
{
|
||||
using (new WriteLock(Lock))
|
||||
{
|
||||
if (_instance == null) _instance = new RuntimeCacheProvider();
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private ObjectCache _memoryCache = new MemoryCache("in-memory");
|
||||
private readonly ObjectCache _memoryCache = new MemoryCache("in-memory");
|
||||
private ConcurrentDictionary<string, string> _keyTracker = new ConcurrentDictionary<string, string>();
|
||||
|
||||
public IEntity GetById(Type type, Guid id)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Threading;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Umbraco.Core.Configuration;
|
||||
|
||||
namespace Umbraco.Core.Persistence
|
||||
@@ -11,27 +12,13 @@ namespace Umbraco.Core.Persistence
|
||||
{
|
||||
#region Singleton
|
||||
|
||||
private static Database _database;
|
||||
private static volatile DatabaseFactory _instance;
|
||||
private static readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
|
||||
private static readonly Database _database = new Database(GlobalSettings.DbDsn);
|
||||
private static readonly Lazy<DatabaseFactory> lazy = new Lazy<DatabaseFactory>(() => new DatabaseFactory());
|
||||
|
||||
private DatabaseFactory() { }
|
||||
public static DatabaseFactory Current { get { return lazy.Value; } }
|
||||
|
||||
public static DatabaseFactory Current
|
||||
private DatabaseFactory()
|
||||
{
|
||||
get
|
||||
{
|
||||
using (new WriteLock(Lock))
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = new DatabaseFactory();
|
||||
_database = new Database(GlobalSettings.DbDsn);
|
||||
}
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
internal abstract class PetaPocoRepositoryBase<TId, TEntity> : RepositoryBase<TId, TEntity>
|
||||
where TEntity : IAggregateRoot
|
||||
{
|
||||
private Database _database;
|
||||
private readonly Database _database;
|
||||
|
||||
protected PetaPocoRepositoryBase(IUnitOfWork work) : base(work)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user