Some quick remarks:
Remove the regions. They're an anti-pattern an anti-pattern basically, and you certainly shouldn't need them with code that's less than 250 lines long.
Your singleton implementation is not liked by Jon Skeet.
Don't use
ContainsKey
. If you need to get something from anIDictionary
, useTryGetValue
instead.
So GetService
would become this:
public static T GetService<T>(object key) where T : class
{
var type = typeof(T);
var dictKey = new Tuple<Type, object>(type, key);
object returnValue;
if(Instance.services.TryGetValue(dictKey, out returnValue))
{
return (T)returnValue;
}
if (!Explicit)
{
var subTypeKey = Instance.services.Keys.FirstOrDefault(x =>
(key == null ? x.Item2 == null : key.Equals(x.Item2))
&& type.IsAssignableFrom(x.Item1));
if (subTypeKey != null)
{
return (T)Instance.services[subTypeKey];
}
}
throw new KeyNotFoundException(string.Format("Cannot get a value for type: {0} and key: {1}. That type has not been registered yet.", type, key));
}
Some quick remarks:
Remove the regions. They're an anti-pattern basically, and you certainly shouldn't need them with code that's less than 250 lines long.
Your singleton implementation is not liked by Jon Skeet.
Don't use
ContainsKey
. If you need to get something from anIDictionary
, useTryGetValue
instead.
So GetService
would become this:
public static T GetService<T>(object key) where T : class
{
var type = typeof(T);
var dictKey = new Tuple<Type, object>(type, key);
object returnValue;
if(Instance.services.TryGetValue(dictKey, out returnValue))
{
return (T)returnValue;
}
if (!Explicit)
{
var subTypeKey = Instance.services.Keys.FirstOrDefault(x =>
(key == null ? x.Item2 == null : key.Equals(x.Item2))
&& type.IsAssignableFrom(x.Item1));
if (subTypeKey != null)
{
return (T)Instance.services[subTypeKey];
}
}
throw new KeyNotFoundException(string.Format("Cannot get a value for type: {0} and key: {1}. That type has not been registered yet.", type, key));
}
Some quick remarks:
Remove the regions. They're an anti-pattern basically, and you certainly shouldn't need them with code that's less than 250 lines long.
Your singleton implementation is not liked by Jon Skeet.
Don't use
ContainsKey
. If you need to get something from anIDictionary
, useTryGetValue
instead.
So GetService
would become this:
public static T GetService<T>(object key) where T : class
{
var type = typeof(T);
var dictKey = new Tuple<Type, object>(type, key);
object returnValue;
if(Instance.services.TryGetValue(dictKey, out returnValue))
{
return (T)returnValue;
}
if (!Explicit)
{
var subTypeKey = Instance.services.Keys.FirstOrDefault(x =>
(key == null ? x.Item2 == null : key.Equals(x.Item2))
&& type.IsAssignableFrom(x.Item1));
if (subTypeKey != null)
{
return (T)Instance.services[subTypeKey];
}
}
throw new KeyNotFoundException(string.Format("Cannot get a value for type: {0} and key: {1}. That type has not been registered yet.", type, key));
}
- 6.1k
- 2
- 23
- 62
Some quick remarks:
Remove the regions. They're an anti-pattern basically, and you certainly shouldn't need them with code that's less than 250 lines long.
Your singleton implementation is not liked by Jon Skeet.
Don't use
ContainsKey
if. If you need to need to get something from anIDictionary
, instead useTryGetValue
instead.
So GetService
would become this:
public static T GetService<T>(object key) where T : class
{
var type = typeof(T);
var dictKey = new Tuple<Type, object>(type, key);
object returnValue;
if(Instance.services.TryGetValue(dictKey, out returnValue))
{
return (T)returnValue;
}
if (!Explicit)
{
var subTypeKey = Instance.services.Keys.FirstOrDefault(x =>
(key == null ? x.Item2 == null : key.Equals(x.Item2))
&& type.IsAssignableFrom(x.Item1));
if (subTypeKey != null)
{
return (T)Instance.services[subTypeKey];
}
}
throw new KeyNotFoundException(string.Format("Cannot get a value for type: {0} and key: {1}. That type has not been registered yet.", type, key));
}
Some quick remarks:
Remove the regions. They're an anti-pattern basically, and you certainly shouldn't need them with code that's less than 250 lines long.
Your singleton implementation is not liked by Jon Skeet.
Don't use
ContainsKey
if you need to need to get something from anIDictionary
, instead useTryGetValue
.
So GetService
would become this:
public static T GetService<T>(object key) where T : class
{
var type = typeof(T);
var dictKey = new Tuple<Type, object>(type, key);
object returnValue;
if(Instance.services.TryGetValue(dictKey, out returnValue))
{
return (T)returnValue;
}
if (!Explicit)
{
var subTypeKey = Instance.services.Keys.FirstOrDefault(x =>
(key == null ? x.Item2 == null : key.Equals(x.Item2))
&& type.IsAssignableFrom(x.Item1));
if (subTypeKey != null)
{
return (T)Instance.services[subTypeKey];
}
}
throw new KeyNotFoundException(string.Format("Cannot get a value for type: {0} and key: {1}. That type has not been registered yet.", type, key));
}
Some quick remarks:
Remove the regions. They're an anti-pattern basically, and you certainly shouldn't need them with code that's less than 250 lines long.
Your singleton implementation is not liked by Jon Skeet.
Don't use
ContainsKey
. If you need to get something from anIDictionary
, useTryGetValue
instead.
So GetService
would become this:
public static T GetService<T>(object key) where T : class
{
var type = typeof(T);
var dictKey = new Tuple<Type, object>(type, key);
object returnValue;
if(Instance.services.TryGetValue(dictKey, out returnValue))
{
return (T)returnValue;
}
if (!Explicit)
{
var subTypeKey = Instance.services.Keys.FirstOrDefault(x =>
(key == null ? x.Item2 == null : key.Equals(x.Item2))
&& type.IsAssignableFrom(x.Item1));
if (subTypeKey != null)
{
return (T)Instance.services[subTypeKey];
}
}
throw new KeyNotFoundException(string.Format("Cannot get a value for type: {0} and key: {1}. That type has not been registered yet.", type, key));
}
Some quick remarks:
Remove the regions. They're an anti-pattern basically, and you certainly shouldn't need them with code that's less than 250 lines long.
Your singleton implementation is not liked by Jon Skeet.
Don't use
ContainsKey
if you need to need to get something from anIDictionary
, instead useTryGetValue
.
So GetService
would become this:
public static T GetService<T>(object key) where T : class
{
var type = typeof(T);
var dictKey = new Tuple<Type, object>(type, key);
object returnValue;
if(Instance.services.TryGetValue(dictKey, out returnValue))
{
return (T)returnValue;
}
if (!Explicit)
{
var subTypeKey = Instance.services.Keys.FirstOrDefault(x =>
(key == null ? x.Item2 == null : key.Equals(x.Item2))
&& type.IsAssignableFrom(x.Item1));
if (subTypeKey != null)
{
return (T)Instance.services[subTypeKey];
}
}
throw new KeyNotFoundException(string.Format("Cannot get a value for type: {0} and key: {1}. That type has not been registered yet.", type, key));
}