.NET MVC3. Service locator / permission issue with Ninject

I have what I consider to be the standard .NET MVC3 repository template project I played / studied with. This is a fairly standard structure.

  • Repository project (with caching infrastructure mentioned below)
  • Domain Model Project
  • Service Level Project
  • MVC Presentation Project

I came across a script when I need to introduce a private member of a class that has only a static constructor that does not give me luck to install the constructor.

This class is a wrapper for using the AppFabric caching implementation that I just completed. (For those who are so addicted, my implementation is based on http://cgeers.wordpress.com/2010/07/04/windows-server-appfabric-caching/ )

Essentially I have:

  • interface ICacheProvider
  • class DefaultCacheProvider: ICacheProvider
  • Cache static class (using any implementation I introduced)

The static class cache is where I would like to introduce ICacheProvider that DefaultCacheProvider will allow.

private static readonly ICacheProvider CacheProvider; static Cache() { //DependencyResolver.Current.GetService<ICacheProvider>(); //CacheProvider = // (ICacheProvider)ServiceLocator.Current // .GetInstance(typeof(ICacheProvider)); } public static void Add(string key, object value) { CacheProvider.Add(key, value); } public static void Add(string key, object value, TimeSpan timeout) { CacheProvider.Add(key, value, timeout); } public static object Get(string key) { return CacheProvider[key]; } public static bool Remove(string key) { return CacheProvider.Remove(key); } 

Based on what I read, it looks like a script for ServiceLocator, but I saw some very strong opinions on this (anti-pattern, etc. etc.) that my familiarity with it is low so I not sure about an implementation that will work.

I saw a StackOverflow recommendation for developing a Cache class as a standard class and entering ICacheProvider in SingletonScope

 kernel.Bind<ICacheProvider>().To<DefaultCacheProvider>().InSingletonScope(); 

but I personally would prefer a static shell for ease of use.

Is ServiceLocator setting up the path here, or is there something else obvious that I don't know about? If ServiceLocator is the way to go, is there any connection with Ninject to use? I know that Ninject now has the capabilities of a service locator, but did not know how to implement it.

Thanks for any info.

+4
source share
1 answer

I think your approach does not contain the essence of the Inversion of Control container to provide dependency injection.

Based on what I read, this looks like a script for ServiceLocator, but I saw very strong opinions on it (anti-pattern, etc.)

Very strong points of view usually include aversion to the Singleton pattern or, in other words, using a static class to provide services. The problem here is that the Cache class you wrote is the same Singleton pattern, which is the anti-pattern you referenced.

What does the code that consumes singleton Cache look like? Let me suggest a hypothetical.

 public class SomeClass { public string GetSomeMetaData() { return Cache.Get("magicKey"); } } 

In this case, you detached the IoC and avoided the DI using Singleton. I would suggest

 public class SomeClass { private readonly ICacheProvider _cacheProvider; public SomeClass(ICacheProvider cacheProvider) { _cacheProvider = cacheProvider; } public string GetSomeMetaData() { return _cacheProvider.Get("magicKey"); } } 

Now consumption of ICacheProvider takes place directly in the class corresponding to it and can easily transfer changes to the implementation of ICacheProvider . This has the added benefit of simplifying testing. The Singleton template is almost impossible to test.

+5
source

All Articles