Beispiel #1
0
        public static void LoadFromConfigurationFile(StructureMap.IContainer container, string file, string classNameConvention)
        {
            Log.DebugFormat("Reading configuration data from '{0}'", file);

            var fileName = Path.GetFileNameWithoutExtension(file);

            var className = string.Format(classNameConvention, fileName);

            // http://haacked.com/archive/2012/07/23/get-all-types-in-an-assembly.aspx/
            var types = new List <Type>();

            foreach (var assembly in GetAssemblies())
            {
                try
                {
                    var t = assembly.GetTypes();
                    types.AddRange(t);
                }
                catch (ReflectionTypeLoadException ex)
                {
                    types.AddRange(ex.Types);
                }
            }

            var matchingTypes = types.Where(x => x != null && x.Name == className).ToList();

            if (matchingTypes.Count == 0)
            {
                return;
            }
            if (matchingTypes.Count > 1)
            {
                throw new Exception(string.Format("Multiple types found with the name '{0}'", className));
            }

            var type = matchingTypes.First();

            if (container.TryGetInstance(type) != null)
            {
                return;
            }

            var configJson   = File.ReadAllText(file);
            var deserialized = JsonConvert.DeserializeObject(configJson, type);

            Log.DebugFormat("Mapping configuration data in '{0}' to '{1}'", file, type.Name);

            //Note: When using Unity, naming the instance BREAKs the resolving
            container.Configure(x => x.For(type).Use(deserialized));
        }
Beispiel #2
0
 public object GetService(Type serviceType)
 {
     if (serviceType == null)
     {
         return(null);
     }
     try
     {
         return(serviceType.IsAbstract || serviceType.IsInterface
                  ? container.TryGetInstance(serviceType)
                  : container.GetInstance(serviceType));
     }
     catch
     {
         return(null);
     }
 }
Beispiel #3
0
 public object TryResolve(Type resolve)
 {
     return(_container.TryGetInstance(resolve));
 }
Beispiel #4
0
 public object TryResolve(Type modelType)
 {
     return(_mapContainer.TryGetInstance(modelType));
 }
 public T TryResolve <T>()
 {
     return(_container.TryGetInstance <T>());
 }