Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
binding to a factory of another bound type
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
fschwiet  
View profile  
 More options Jul 18 2010, 9:10 pm
From: fschwiet <fschw...@gmail.com>
Date: Sun, 18 Jul 2010 18:10:17 -0700 (PDT)
Local: Sun, Jul 18 2010 9:10 pm
Subject: binding to a factory of another bound type
I've been playing with Ninject to see if it has the features I need to
port a project over to it.  One feature that seemed missing was the
ability to bind to a factory method for a service.  You can bind a
service to a factory method, but this is different.  What I mean is
that a class can take as a dependency a factory method, where that
factory method can be any service already bound within that kernel.
So the class can create multiple instances of the dependency it has.

For example, a Juggler needs more than 1 weapon, they may need many
weapons over time.  In fact every time they pull out a weapon, its a
new one, because presumably the old one was dropped.

    public class Juggler : IWarrior
    {
        private readonly Func<IWeapon> _weaponFactory;

        public Juggler(Func<IWeapon> weaponFactory)
        {
            _weaponFactory = weaponFactory;
        }

        public IWeapon Weapon
        {
            get { return _weaponFactory(); }
        }
    }

I'm not sure Ninject supports this natively, I couldn't find it.
Here's a test case.  It passes, with the commit linked below.  I'm
just sharing, and wondering if anyone else thinks this is useful or
perhaps has thoughts on a better approach.

    class FactoryBindingTests : FactoryBindingContext
    {
        [Fact]
        public void can_bind_factory()
        {
            kernel.Bind<IWeapon>().To<Sword>();
            kernel.Bind<Juggler>().ToSelf();

            var result = kernel.Get<Juggler>();

            Assert.NotNull(result);
        }
   }

Commit adding this capability:
http://github.com/fschwiet/ninject/commit/1ae73369725b4378a178c379e2b...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Remo Gloor  
View profile  
 More options Jul 19 2010, 6:26 am
From: Remo Gloor <remo.gl...@bbv.ch>
Date: Mon, 19 Jul 2010 03:26:53 -0700 (PDT)
Local: Mon, Jul 19 2010 6:26 am
Subject: Re: binding to a factory of another bound type
Yes that's possible

    public WeaponFactory : IWeaponFactory
    {
        private readonly IKernel kernel;
        public WeaponFactory(IKernel kernel)
        {
            this.kernel = kernel;
        }

        public IWeapon CreateWeapon()
        {
            return this.kernel.Get<IWeapon>();
        }
    }

    public class Juggler : IWarrior
    {
        private readonly IWeaponFactory _weaponFactory;

        public Juggler(IWeaponFactory weaponFactory)
        {
            _weaponFactory = weaponFactory;
        }

        public IWeapon Weapon
        {
            get { return _weaponFactory.CreateWeapon(); }
        }
    }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
fschwiet  
View profile  
 More options Jul 19 2010, 12:10 pm
From: fschwiet <fschw...@gmail.com>
Date: Mon, 19 Jul 2010 09:10:09 -0700 (PDT)
Local: Mon, Jul 19 2010 12:10 pm
Subject: Re: binding to a factory of another bound type
  Oh yeah I forgot about that approach.  I like to be more explicit
with the dependencies, to have a dependency on IKernel leaves some
question as to what the dependency is for.  Requiring a specific
factory was useful when I was generating dependency graphs for the
final result, though I don't have that capability with Ninject yet.

On Jul 19, 3:26 am, Remo Gloor <remo.gl...@bbv.ch> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Urs Enzler  
View profile  
 More options Jul 21 2010, 4:38 am
From: Urs Enzler <urs.enz...@gmail.com>
Date: Wed, 21 Jul 2010 01:38:14 -0700 (PDT)
Local: Wed, Jul 21 2010 4:38 am
Subject: Re: binding to a factory of another bound type
Hei Remo

[InsiderJoke]
Since when do we pass IKernel into a factoy anf not an
IResolutionRoot  ;-)
[/InsiderJoke]

There is no reason to pass in the kernel because no additional
bindings are defined. An IResolutionRoot is sufficient and more
flexible.

Cheers,
Urs

On Jul 19, 12:26 pm, Remo Gloor <remo.gl...@bbv.ch> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Remo Gloor  
View profile  
 More options Jul 21 2010, 11:43 am
From: Remo Gloor <remo.gl...@bbv.ch>
Date: Wed, 21 Jul 2010 08:43:41 -0700 (PDT)
Local: Wed, Jul 21 2010 11:43 am
Subject: Re: binding to a factory of another bound type
Of course IResolutionRoot is preferable. But in that case you have to
define a binding from IResolutionRoot to the kernel because Ninject
has only IKernel by default. Or in case you need to know the parent
context in the factory you can load my ContextPreservationModule which
already has defined this binding.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »