Analog to Windsor's typed factory facility

119 views
Skip to first unread message

Jimmy Bogard

unread,
Mar 15, 2010, 9:35:40 AM3/15/10
to structuremap-users
One of the interesting applications of Windsor is to build automatic factories, so if you have an interface:

IFooFactory {
   Foo Build();
}

As long as the method is no-arg etc, Windsor builds up a proxy class FooFactory that just delegates to the container.  This is an interesting concept for a couple of reasons.  First, it removes the need to make my classes depend *directly* on a container.  Second, it can provide a more intention-revealing interface for what I'm trying to do.  Lastly, if it removes the need to build a bunch of wrappers over the container for places where I'm using factories.

I don't think this would be hard to add, except for a couple of things:

- No reference AFAIK to any proxy generation library
- Plugin types must have an explicit forrealz plugged type <- not terrible, just means I'd have to gen the proxy at config time

Adding a non-system reference would be a Big Deal I realize, so where would something like this better live?  A separate project, a la automocking?  A contrib project?

Thanks,

Jimmy

Jeff Doolittle

unread,
Mar 15, 2010, 1:16:05 PM3/15/10
to structuremap-users

Regardless of the implementation, I'd like to add my vote to such a
feature. This would be a snap to implement with Castle Dynamic Proxy,
but I understand the importance of keeping SM from directly making
such a reference.

I'm curious if anyone else has thoughts, ideas, etc. on this.

--Jeff

Jeremy D. Miller

unread,
Mar 15, 2010, 1:36:34 PM3/15/10
to structure...@googlegroups.com
Jimmy,

Would just using Func<T> do the trick?  That's available in 2.5.4+.  I started to implement Func<string, T> too, but didn't finish it.  I'm obviously very hesitant to add any dependencies, to the point of purposely duplicating code across FubuMVC and StructureMap.

People really, really dislike Windsor/Castle's dependency hell problems and I'd like to keep it out of SM.  Let's put that in a separate assembly.
 
Jeremy D. Miller
The Shade Tree Developer
jeremy...@yahoo.com



From: Jeff Doolittle <jeffdo...@gmail.com>
To: structuremap-users <structure...@googlegroups.com>
Sent: Mon, March 15, 2010 12:16:05 PM
Subject: [sm-users] Re: Analog to Windsor's typed factory facility
--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To post to this group, send email to structure...@googlegroups.com.
To unsubscribe from this group, send email to structuremap-users+unsub...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/structuremap-users?hl=en.


Jimmy Bogard

unread,
Mar 15, 2010, 4:54:05 PM3/15/10
to structure...@googlegroups.com
Yeah, it would do the trick, but I'm not sure I'd want to expose something like that in a public API, where I can't really guarantee that folks are using a container that supports func-y ctor args.  I'm looking at something like:

For<IFooFactory>().CreateFactory();

Separate project it is, and there's always ilmerge...

To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.

Jeremy D. Miller

unread,
Mar 15, 2010, 4:55:09 PM3/15/10
to structure...@googlegroups.com
Ok, send me your GitHub account...


From: Jimmy Bogard <jimmy....@gmail.com>
To: structure...@googlegroups.com
Sent: Mon, March 15, 2010 3:54:05 PM
Subject: Re: [sm-users] Re: Analog to Windsor's typed factory facility

Krzysztof Koźmic (2)

unread,
Mar 16, 2010, 4:37:59 AM3/16/10
to structuremap-users
few things to note:

1. Windsors TFF does not just work with no-arg methods. Only real
requirement is that method does not have out arguments. You can also
plug your custom logic that maps the argument to whatever you want to
pass further down the invocation pipeline. See my last post where I do
it to do something like SM's "Connect to type closing generic".

2. Func does not always cut it. You may want to pull things
differently depending on context. It's nicer to have factory with two
methods than two Func delegates. Additionally Func is unable to work
when you need generic method on non generic class.

take this sample:

public class Foo
{
private IHandlerFactory handlerFactory;//set by ctor

public void Handle<TMessage>(TMessage message)
{
var handle = handlerFactory.GetHandler<TMessage>()
handler.Handle(message);
}
}

You can't do this with Func<>.


Just my $0.02 + VAT

On 15 Mar, 21:55, "Jeremy D. Miller" <jeremydmil...@yahoo.com> wrote:
> Ok, send me your GitHub account...
>
>  Jeremy D. Miller
> The Shade Tree Developer

> jeremydmil...@yahoo.com
>
> ________________________________
> From: Jimmy Bogard <jimmy.bog...@gmail.com>


> To: structure...@googlegroups.com
> Sent: Mon, March 15, 2010 3:54:05 PM
> Subject: Re: [sm-users] Re: Analog to Windsor's typed factory facility
>
> Yeah, it would do the trick, but I'm not sure I'd want to expose something like that in a public API, where I can't really guarantee that folks are using a container that supports func-y ctor args.  I'm looking at something like:
>
> For<IFooFactory>().CreateFactory();
>
> Separate project it is, and there's always ilmerge...
>

> On Mon, Mar 15, 2010 at 12:36 PM, Jeremy D. Miller <jeremydmil...@yahoo.com> wrote:
>
> Jimmy,
>
> >Would just using Func<T> do the trick?  That's available in 2.5.4+.  I started to implement Func<string, T> too, but didn't finish it.  I'm obviously very hesitant to add any dependencies, to the point of purposely duplicating code across FubuMVC and StructureMap.
>
> >People really, really dislike Windsor/Castle's dependency hell problems and I'd like to keep it out of SM.  Let's put that in a separate assembly.
>
> > Jeremy D. Miller
> >The Shade Tree Developer

> >jeremydmil...@yahoo.com
>
> ________________________________
> From: Jeff Doolittle <jeffdoolit...@gmail.com>

> >To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.

> >For more options, visit this group athttp://groups.google.com/group/structuremap-users?hl=en.


>
> >--
> >>You received this message because you are subscribed to the Google Groups "structuremap-users" group.
> >>To post to this group, send email to structure...@googlegroups.com.
> >>To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.
>

> >For more options, visit this group athttp://groups.google.com/group/structuremap-users?hl=en.

Jimmy Bogard

unread,
Mar 16, 2010, 2:09:40 PM3/16/10
to structure...@googlegroups.com
I just forked it on my github http://github.com/jbogard/structuremap

And thanks Krzysztof, for giving me more to do :P

Jeremy D. Miller

unread,
Mar 16, 2010, 2:20:58 PM3/16/10
to structure...@googlegroups.com
Take your time, I don't know how to take in pull requests yet;)
Sent: Tue, March 16, 2010 1:09:40 PM

Jimmy Bogard

unread,
Mar 22, 2010, 10:08:09 AM3/22/10
to structure...@googlegroups.com
I've got a simple implementation up on my fork, it supports everything in this interface:

    public interface IDummyFactory
    {
        IDummyService CreateDummyService();
        TService CreateService<TService>();
        IHandler<TMessage> CreateHandler<TMessage>();
        object CreateService(Type pluginType);
    }

Windsor's does a few extra things as K-2 noted, but I couldn't figure out why those extra things were necessary or interesting based on the tests Windsor had in place for these features.  The only rather big change I had to do was change the IContext object to add another overload for GetInstance.  I assumed that the IContext dude was a wrapper around the currently executing IContainer...but it looks like instead those two share the PipelineGraph etc.

Feel free to have Josh do the pull request for ya :)

Josh Flanagan

unread,
Mar 23, 2010, 5:22:06 PM3/23/10
to structure...@googlegroups.com
Done

Reply all
Reply to author
Forward
0 new messages