Search code examples
c#.netdependency-injectionninjectsimple-injector

Simple Injector - Register Compared to Bind in Ninject


I'm trying to register the following equivalent in Ninject but I'm having difficulty (partly because of all the changes/deprecation from earlier versions of Simple Injector). Can someone please confirm how the following bindings would be translated to Simple Injector?

this.kernel.Bind(x => x.FromThisAssembly()
                   .SelectAllClasses().InheritedFrom<MyFactory>().BindToSelf()
                   .Configure(b => b.InSingletonScope()));


this.kernel.Bind(x => x.FromThisAssembly()
                   .SelectAllClasses()
                   .InNamespaceOf<MyClass>().BindToSelf()
                   .Configure(b => b.InSingletonScope()));

Solution

  • container.RegisterCollection(typeof(MyFactory), Assembly.GetExecutingAssembly());
    
    var namespaceTypes =
        from type in Assembly.GetExecutingAssembly().GetTypes()
        where type.Namespace == typeof(MyClass).Namespace
        select type;
    
    foreach (Type type in namespaceTypes)
        container.Register(type, type, Lifestyle.Singleton);