Skip to main content
Code Review

Return to Answer

replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

Some quick remarks:

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 an IDictionary, use TryGetValue 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 an IDictionary, use TryGetValue 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));
}
Fixed some 'syntax' mistakes in a sentence, as well as a few grammatical errors.
Source Link

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 an IDictionary, instead use TryGetValue 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 an IDictionary, instead use TryGetValue.

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 an IDictionary, use TryGetValue 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));
}
Source Link
BCdotWEB
  • 11.4k
  • 2
  • 28
  • 45

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 an IDictionary, instead use TryGetValue.

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));
}
lang-cs

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