Lazy Func<T>

57 views
Skip to first unread message

shapper

unread,
Sep 12, 2010, 7:58:23 AM9/12/10
to structuremap-users
Hello,

I have the following:

public class Foo {
private readonly Func<IPostService> _postServiceFactory;
public Foo(Func<IPostService> postServiceFactory) {
}
}

IPostService implements IService.

How can I set the Func<T> in Structure Map for all types of IService?

I think this is called Lazy Func<T> but I have been trying a few
options but it never compiles.

Thanks,
Miguel

Brian Chiasson

unread,
Sep 12, 2010, 8:41:25 AM9/12/10
to structure...@googlegroups.com
I am not sure there is an easy way to do this. You may have to create your own scanner if you have a lot of them, but I think this should work:
 
For<Func<IPostService>>().Use(() => /* Your func here */);

-Brian Chiasson
 

--
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 at http://groups.google.com/group/structuremap-users?hl=en.


shapper

unread,
Sep 12, 2010, 10:35:49 AM9/12/10
to structuremap-users
Yes, I tried exactly that:

For<Func<IPostService>>().Use(() =>
ObjectFactory.GetInstance<PostService>());

Basically, I am checking the Structure Map Lazy Func<T> to deliver the
current PostService instance in use.

However, on the Foo class that Func is null.

What am I doing wrong?

Thank You,
Miguel
> > structuremap-us...@googlegroups.com<structuremap-users%2Bunsubs cr...@googlegroups.com>
> > .

Elliott O'Hara

unread,
Sep 12, 2010, 11:20:32 AM9/12/10
to structure...@googlegroups.com
For<Func<IPostService>>().Use(() => the actual function)

Also, don't use ObjectFactory in your Use method... use Use( container => container.SomeMethod)



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

Jeremy D. Miller

unread,
Sep 12, 2010, 11:30:15 AM9/12/10
to structure...@googlegroups.com
Better still, just register your T and Func<T> will work automatically if you're on 2.6+.
 
Jeremy D. Miller
The Shade Tree Developer
jeremy...@yahoo.com



From: Elliott O'Hara <elliot...@gmail.com>
To: structure...@googlegroups.com
Sent: Sun, September 12, 2010 10:20:32 AM
Subject: Re: [sm-users] Re: Lazy Func<T>

Elliott O'Hara

unread,
Sep 12, 2010, 11:37:38 AM9/12/10
to structure...@googlegroups.com
Thanks boss man!

it'll just use the greediest ctor?

Jeremy D. Miller

unread,
Sep 12, 2010, 11:39:08 AM9/12/10
to structure...@googlegroups.com
It just delegates to whatever is registered for the T, so yes.

Ctor selection will get more flexible in 3.0 -- whenever that happens;)
 
Sent: Sun, September 12, 2010 10:37:38 AM

Elliott O'Hara

unread,
Sep 12, 2010, 12:13:38 PM9/12/10
to structure...@googlegroups.com
Take your time... Don't get burned out!

Enjoy the non 100 degree days in ATX, take the kid to Zilker or something! It's beautiful here right now!

2.6x is working great for us.... 

Thanks for such a great tool man!

/E

shapper

unread,
Sep 12, 2010, 12:40:55 PM9/12/10
to structuremap-users
That is nice.

Thank You!

On Sep 12, 5:13 pm, "Elliott O'Hara" <elliott.oh...@gmail.com> wrote:
> Take your time... Don't get burned out!
>
> Enjoy the non 100 degree days in ATX, take the kid to Zilker or something!
> It's beautiful here right now!
>
> 2.6x is working great for us....
>
> Thanks for such a great tool man!
>
> /E
>
> On Sun, Sep 12, 2010 at 10:39 AM, Jeremy D. Miller
> <jeremydmil...@yahoo.com>wrote:
>
>
>
> > It just delegates to whatever is registered for the T, so yes.
>
> > Ctor selection will get more flexible in 3.0 -- whenever that happens;)
>
> > Jeremy D. Miller
> > The Shade Tree Developer <http://codebetter.com/blogs/jeremy.miller>
> > jeremydmil...@yahoo.com
>
> > ------------------------------
> > *From:* Elliott O'Hara <elliott.oh...@gmail.com>
> > *To:* structure...@googlegroups.com
> > *Sent:* Sun, September 12, 2010 10:37:38 AM
>
> > *Subject:* Re: [sm-users] Re: Lazy Func<T>
>
> > Thanks boss man!
>
> > it'll just use the greediest ctor?
>
> > On Sun, Sep 12, 2010 at 10:30 AM, Jeremy D. Miller <
> > jeremydmil...@yahoo.com> wrote:
>
> >> Better still, just register your T and Func<T> will work automatically if
> >> you're on 2.6+.
>
> >> Jeremy D. Miller
> >> The Shade Tree Developer <http://codebetter.com/blogs/jeremy.miller>
> >> jeremydmil...@yahoo.com
>
> >> ------------------------------
> >> *From:* Elliott O'Hara <elliott.oh...@gmail.com>
> >> *To:* structure...@googlegroups.com
> >> *Sent:* Sun, September 12, 2010 10:20:32 AM
> >> *Subject:* Re: [sm-users] Re: Lazy Func<T>
>
> >> For<Func<IPostService>>().Use(() => the actual function)
>
> >> Also, don't use ObjectFactory in your Use method... use Use( container =>
> >> container.SomeMethod)
>
> >>> > > structuremap-us...@googlegroups.com<structuremap-users%2Bunsubs cr...@googlegroups.com><structuremap-users%2Bunsubs
Reply all
Reply to author
Forward
0 new messages