using System; using System.Collections.Generic; using System.Web.Mvc; using Ninject.Core; using System.Reflection; using Ninject.Conditions; using Castle.ActiveRecord; using ActiveRecordScaffolding.Controllers; namespace NinjectMvc.Helpers { public class ARControllerModule : StandardModule { private IEnumerable _assemblies; public ARControllerModule(params Assembly[] assemblies) { _assemblies = assemblies; } public override void Load() { foreach (Assembly assembly in _assemblies) { IDictionary controllers = CreateARControllers(assembly); foreach (KeyValuePair entry in controllers) { string name = entry.Key; Type type = entry.Value; Bind(typeof(IController)).To(type).Only( When.Context.Variable("controllerName").EqualTo(name) ); } } } public IDictionary CreateARControllers(Assembly assembly) { Dictionary controllers = new Dictionary(); Type[] types = assembly.GetExportedTypes(); foreach (Type type in types) { object[] attribs = type.GetCustomAttributes(typeof(ActiveRecordAttribute), false); if (attribs != null && attribs.Length == 1) { Type controller = typeof(ARController<>).MakeGenericType(type); controllers.Add(type.Name, controller); } } return controllers; } } }