Skip to main content
Code Review

Return to Question

Notice removed Content dispute by Community Bot
Post Unlocked by Community Bot
Post Locked by 200_success
Notice added Content dispute by 200_success
Rollback to Revision 1
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

I use theCacheManager class throughout my program. If I want to change the implementation I simply add a provider and I add it in my dependency container

Is it correct to do this like that?

ICache

namespace Caching
{
 public interface ICache
 {
 object Get(string key);
 void Set(string key, object value);
 }
}

ICacheManager

namespace Caching
{
 public static class CacheManager
 {
 private static ICache cache;
 private static readonly object syncRoot = new object();
 private static ICache Cache
 {
 get
 {
 lock (syncRoot)
 {
 if (cache == null)
 {
 cache = Container.Instance.Resolve<ICache>();
 }
 }
 return cache;
 }
 }
 public static object Get(string key)
 {
 return Cache.Get(key);
 }
 public static void Set(string key, object value)
 {
 Cache.Set(key, value);
 }
 }
}

MemoryCacheProvider

using System.Runtime.Caching;
namespace Caching.Providers
{
 public class MemoryCacheProvider : ICache
 {
 public object Get(string key)
 {
 return MemoryCache.Default.Get(key);
 }
 public void Set(string key, object value)
 {
 MemoryCache.Default.Add(key, value, DateTime.Now.AddMinutes(5));
 }
 }
}

Main

static void Main(string[] args)
{
 Container.Instance.RegisterType<ICache, MemoryCacheProvider>();
}

I use the

I use theCacheManager class throughout my program. If I want to change the implementation I simply add a provider and I add it in my dependency container

Is it correct to do this like that?

ICache

namespace Caching
{
 public interface ICache
 {
 object Get(string key);
 void Set(string key, object value);
 }
}

ICacheManager

namespace Caching
{
 public static class CacheManager
 {
 private static ICache cache;
 private static readonly object syncRoot = new object();
 private static ICache Cache
 {
 get
 {
 lock (syncRoot)
 {
 if (cache == null)
 {
 cache = Container.Instance.Resolve<ICache>();
 }
 }
 return cache;
 }
 }
 public static object Get(string key)
 {
 return Cache.Get(key);
 }
 public static void Set(string key, object value)
 {
 Cache.Set(key, value);
 }
 }
}

MemoryCacheProvider

using System.Runtime.Caching;
namespace Caching.Providers
{
 public class MemoryCacheProvider : ICache
 {
 public object Get(string key)
 {
 return MemoryCache.Default.Get(key);
 }
 public void Set(string key, object value)
 {
 MemoryCache.Default.Add(key, value, DateTime.Now.AddMinutes(5));
 }
 }
}

Main

static void Main(string[] args)
{
 Container.Instance.RegisterType<ICache, MemoryCacheProvider>();
}
deleted 1946 characters in body
Source Link
user147732
user147732

I use theCacheManager class throughout my program. If I want to change the implementation I simply add a provider and I add it in my dependency container

Is it correct to do this like that?

ICache

namespace Caching
{
 public interface ICache
 {
 object Get(string key);
 void Set(string key, object value);
 }
}

ICacheManager

namespace Caching
{
 public static class CacheManager
 {
 private static ICache cache;
 private static readonly object syncRoot = new object();
 private static ICache Cache
 {
 get
 {
 lock (syncRoot)
 {
 if (cache == null)
 {
 cache = Container.Instance.Resolve<ICache>();
 }
 }
 return cache;
 }
 }
 public static object Get(string key)
 {
 return Cache.Get(key);
 }
 public static void Set(string key, object value)
 {
 Cache.Set(key, value);
 }
 }
}

MemoryCacheProvider

using System.Runtime.Caching;
namespace Caching.Providers
{
 public class MemoryCacheProvider : ICache
 {
 public object Get(string key)
 {
 return MemoryCache.Default.Get(key);
 }
 public void Set(string key, object value)
 {
 MemoryCache.Default.Add(key, value, DateTime.Now.AddMinutes(5));
 }
 }
}

Main

static void Main(string[] args)
{
 Container.Instance.RegisterType<ICache, MemoryCacheProvider>();
}

I use theCacheManager class throughout my program. If I want to change the implementation I simply add a provider and I add it in my dependency container

Is it correct to do this like that?

ICache

namespace Caching
{
 public interface ICache
 {
 object Get(string key);
 void Set(string key, object value);
 }
}

ICacheManager

namespace Caching
{
 public static class CacheManager
 {
 private static ICache cache;
 private static readonly object syncRoot = new object();
 private static ICache Cache
 {
 get
 {
 lock (syncRoot)
 {
 if (cache == null)
 {
 cache = Container.Instance.Resolve<ICache>();
 }
 }
 return cache;
 }
 }
 public static object Get(string key)
 {
 return Cache.Get(key);
 }
 public static void Set(string key, object value)
 {
 Cache.Set(key, value);
 }
 }
}

MemoryCacheProvider

using System.Runtime.Caching;
namespace Caching.Providers
{
 public class MemoryCacheProvider : ICache
 {
 public object Get(string key)
 {
 return MemoryCache.Default.Get(key);
 }
 public void Set(string key, object value)
 {
 MemoryCache.Default.Add(key, value, DateTime.Now.AddMinutes(5));
 }
 }
}

Main

static void Main(string[] args)
{
 Container.Instance.RegisterType<ICache, MemoryCacheProvider>();
}

I use the

Rollback to Revision 1
Source Link
Peilonrayz
  • 44.4k
  • 7
  • 80
  • 157

I usruse the CacheManager class throughout my program. If I want to change the implementation I simply add a provider and I add it in my dependency container

Is it correct to do this like that?

ICache

namespace Caching
{
 public interface ICache
 {
 object Get(string key);
 void Set(string key, object value);
 }
}

ICacheManager

namespace Caching
{
 public static class CacheManager
 {
 private static ICache cache;
 private static readonly object syncRoot = new object();
 private static ICache Cache
 {
 get
 {
 lock (syncRoot)
 {
 if (cache == null)
 {
 cache = Container.Instance.Resolve<ICache>();
 }
 }
 return cache;
 }
 }
 public static object Get(string key)
 {
 return Cache.Get(key);
 }
 public static void Set(string key, object value)
 {
 Cache.Set(key, value);
 }
 }
}

MemoryCacheProvider

using System.Runtime.Caching;
namespace Caching.Providers
{
 public class MemoryCacheProvider : ICache
 {
 public object Get(string key)
 {
 return MemoryCache.Default.Get(key);
 }
 public void Set(string key, object value)
 {
 MemoryCache.Default.Add(key, value, DateTime.Now.AddMinutes(5));
 }
 }
}

Main

static void Main(string[] args)
{
 Container.Instance.RegisterType<ICache, MemoryCacheProvider>();
}

I usr

I use the CacheManager class throughout my program. If I want to change the implementation I simply add a provider and I add it in my dependency container

Is it correct to do this like that?

ICache

namespace Caching
{
 public interface ICache
 {
 object Get(string key);
 void Set(string key, object value);
 }
}

ICacheManager

namespace Caching
{
 public static class CacheManager
 {
 private static ICache cache;
 private static readonly object syncRoot = new object();
 private static ICache Cache
 {
 get
 {
 lock (syncRoot)
 {
 if (cache == null)
 {
 cache = Container.Instance.Resolve<ICache>();
 }
 }
 return cache;
 }
 }
 public static object Get(string key)
 {
 return Cache.Get(key);
 }
 public static void Set(string key, object value)
 {
 Cache.Set(key, value);
 }
 }
}

MemoryCacheProvider

using System.Runtime.Caching;
namespace Caching.Providers
{
 public class MemoryCacheProvider : ICache
 {
 public object Get(string key)
 {
 return MemoryCache.Default.Get(key);
 }
 public void Set(string key, object value)
 {
 MemoryCache.Default.Add(key, value, DateTime.Now.AddMinutes(5));
 }
 }
}

Main

static void Main(string[] args)
{
 Container.Instance.RegisterType<ICache, MemoryCacheProvider>();
}
deleted 1950 characters in body
Source Link
user147732
user147732
Loading
Source Link
user147732
user147732
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /