public void Scan(Assembly assembly) { bool postBuildNeeded; DependencyBuilder.Scan(assembly, out postBuildNeeded); if (postBuildNeeded) { DependencyBuilder.PostBuildScan(new List <Assembly>() { assembly }); } }
/// <summary> /// Registers all dependencies in referenced assemblies. /// </summary> /// <author>Anwar</author> /// <date>11/9/2011</date> public static void ScanAll() { var path = HostingEnvironment.IsHosted ? HostingEnvironment.MapPath("~/App_Data/bindings.json") : Path.Combine(Environment.CurrentDirectory, "bindings.json"); if (File.Exists(path)) { using (Stream stream = new FileStream(path, FileMode.Open)) { Container.LoadFrom(stream); var assemblies = ActivationManager.Assemblies; var list = new List <Assembly>(); foreach (var assembly in assemblies.AsParallel()) { var attr = assembly.GetCustomAttribute <ContainerAssemblyAttribute>(); if (attr != null && attr.PostBuild) { list.Add(assembly); } } DependencyBuilder.PostBuildScan(list); } } else { DependencyBuilder.ScanAll(); using (Stream stream = new FileStream(path, FileMode.Create)) { Container.SaveTo(stream); } } }
public static void LoadFrom(Stream stream) { StreamReader reader = new StreamReader(stream, Encoding.UTF8); dynamic deserializeObject = JsonConvert.DeserializeObject(reader.ReadToEnd()); reader.Dispose(); if (deserializeObject.defaultBindings != null) { foreach (var binding in deserializeObject.defaultBindings) { if (binding.type != null && binding.name != null) { Type type = Type.GetType(ParseTypeName((string)binding.type), false); if (type != null) { Container.OverrideDefaultService(type, binding.name); } } } } if (deserializeObject.allBindings != null) { foreach (var bindingInfo in deserializeObject.allBindings) { if (bindingInfo.type != null && bindingInfo.bindings != null) { Type type = Type.GetType(ParseTypeName((string)bindingInfo.type), false); if (type != null) { foreach (var binding in bindingInfo.bindings) { Type service = Type.GetType(ParseTypeName((string)binding.service), false); Type implementation = Type.GetType(ParseTypeName((string)binding.implementation), false); if (service != null && implementation != null) { string name = binding.name; int? order = null; if (binding.order != null) { order = Convert.ToInt32(binding.order); } Type scopeType = Type.GetType(ParseTypeName((string)binding.scope), false); ILifetime lifetime = scopeType == null ? new TransientLifetime() : (ILifetime)Activator.CreateInstance(scopeType); DependencyBuilder.BindInstance(service, implementation, name, lifetime, order); } } } } } } }
public void Bind(Type serviceType, Type implmentedType, string name, LifetimeType lifetime) { DependencyBuilder.BindInstance(serviceType, implmentedType, name, lifetime); }
public Func <TType> GetInstance <TType>(Type injectedType) { return(DependencyBuilder.GetInstance <TType>(injectedType)); }
public Func <TType> GetInstance <TType>() { return(DependencyBuilder.GetInstance <TType>()); }