Multiple kernels possible? Using Inversion of Control in a componente-orentied world ...

398 views
Skip to first unread message

Georg Wächter

unread,
Jan 16, 2010, 12:03:23 PM1/16/10
to ninject
Hello everyone,

i'm wondering whether it is possible to use multiple kernels in one
application where one kernel object could inherit all bindings from
another? I have a componented oriented software where there are many
different componentes that are composed in a large tree. With NInject
i can isolate the creation and configuration of those, but i seems
this is only possible within the whole application scope. What i
really want is the possibility to create kernels out of existing ones
like this:

var derivedKernel = new StandardKernel(existingKernel, myNewModules);

The existing kernel remains untouched, but the new one inherits all
modules plus a few new ones that can also override old bindings.

This allows me to re-configure the bindings within a component. The
new kernel will be passed to the child-components, where it will be
used to instantiate any IIComponent component - depending on the
position within the large component tree.

Any ideas on how to achieve this with NInect?

georg

Ian Davis

unread,
Jan 16, 2010, 12:42:29 PM1/16/10
to nin...@googlegroups.com
You can call kernel.GetModules()  and then pass them into the new kernel via kernel.Load(modules). I am not sure if there would be any side effects.

--
You received this message because you are subscribed to the Google Groups "ninject" group.
To post to this group, send email to nin...@googlegroups.com.
To unsubscribe from this group, send email to ninject+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ninject?hl=en.






--
Ian Davis
http://disassembla.net

Miguel Madero

unread,
Jan 16, 2010, 8:23:08 PM1/16/10
to nin...@googlegroups.com
I remember we had Scopes in Ninject 1 or 1.5. I don't know if that concept is still there, but it might be what you're looking for.
Miguel A. Madero Reyes
www.miguelmadero.com (blog)
m...@miguelmadero.com

Miguel Madero

unread,
Jan 16, 2010, 8:28:58 PM1/16/10
to nin...@googlegroups.com
I checked Ninject 2.0 and it doesn't have the concept of a Scope so forget my previous comment.
 
I've not tested Ian's suggestion, so as he mentioned, I'm not sure about the side effects, but another possibility, instead of getting the modules would be to get the bindings (Kernel.GetBindings) and then add all of those bindings to the new Kernel.

LOBOMINATOR

unread,
Jan 17, 2010, 4:09:23 AM1/17/10
to nin...@googlegroups.com

Might this help?

 

    public class ChildKernel : StandardKernel

    {

        private readonly IResolutionRoot parent;

 

        /// <summary>

        /// Initializes a new instance of the <see cref="ChildKernel"/> class.

        /// </summary>

        /// <param name="parent">The parent.</param>

        /// <param name="modules">The modules.</param>

        public ChildKernel(IResolutionRoot parent, params INinjectModule[] modules)

            : base(modules)

        {

            this.parent = parent;           

        }

 

        /// <summary>

        /// Initializes a new instance of the <see cref="ChildKernel"/> class.

        /// </summary>

        /// <param name="parent">The parent.</param>

        /// <param name="settings">The settings.</param>

        /// <param name="modules">The modules.</param>

        public ChildKernel(IResolutionRoot parent, INinjectSettings settings, params INinjectModule[] modules)

            : base(settings, modules)

        {

            this.parent = parent;

        }

 

        /// <summary>

        /// Determines whether the specified request can be resolved.

        /// </summary>

        /// <param name="request">The request.</param>

        /// <returns>

        ///     <c>True</c> if the request can be resolved; otherwise, <c>false</c>.

        /// </returns>

        public override bool CanResolve(IRequest request)

        {

            return base.CanResolve(request) || this.parent.CanResolve(request);

        }

 

        /// <summary>

        /// Resolves instances for the specified request. The instances are not actually resolved

        /// until a consumer iterates over the enumerator.

        /// </summary>

        /// <param name="request">The request to resolve.</param>

        /// <returns>

        /// An enumerator of instances that match the request.

        /// </returns>

        public override IEnumerable<object> Resolve(IRequest request)

        {

            return base.CanResolve(request) ? base.Resolve(request) : this.parent.Resolve(request);

Nate Kohari

unread,
Jan 17, 2010, 1:02:51 PM1/17/10
to nin...@googlegroups.com
Ninject 2.0 does have the concept of scope; the syntax is just slightly different:

using (kernel.BeginBlock())
{
  // Activate things here...
}
// ...and they'll be deactivated here.


-Nate

Miguel Madero

unread,
Jan 18, 2010, 8:38:17 PM1/18/10
to nin...@googlegroups.com
Excellent, I forgot about that, someone mentioned it a while ago. I like that syntax.
 

 
On Mon, Jan 18, 2010 at 5:02 AM, Nate Kohari <nko...@gmail.com> wrote:
and they'll be deactivated here.



Reply all
Reply to author
Forward
0 new messages