Is there a way to inject dependencies during application startup?

44 views
Skip to first unread message

Gary Brunton

unread,
Dec 13, 2012, 1:33:43 AM12/13/12
to structure...@googlegroups.com
Say I have an interface like this:

public interface IApplicationStartupTask
{
  void Execute();
}

When my web application starts up I have multiple registries I use for SM configuration.  One of the registries scans for these IApplicationStartupTask types.  This is all standard here.

My problem is that I can't find a way to get the implementations on application startup unless I specifically call the following:

ObjectFactory.GetAllInstances<IApplicationStartupTask>()

But I don't want to do that. I would rather find a way to instantiate a type that has the implementations injected.

To be clear, if after application startup an endpoint is reached and that endpoint has IEnumerable<IApplicationStartupTask> tasks in it's constructor, the tasks are populated as expected.  But again, I want to somehow get access to these tasks on application startup. Not when an endpoint is reached.

Thanks,
Gary Brunton

Jeremy D. Miller

unread,
Dec 13, 2012, 7:42:10 AM12/13/12
to structure...@googlegroups.com
Gary,

I'm not clear on what you're asking.  Are you just trying to find out what the types are, or to have the IApplicationStartupTask implementations run before any endpoints are executed?  What platform is this for?

Jeremy


--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/structuremap-users/-/9xXxrbx0KskJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gary Brunton

unread,
Dec 13, 2012, 10:28:23 AM12/13/12
to structure...@googlegroups.com
Jeremy,

I'm wanting the implementations to run before the endpoints run preferably on application start up.  This is for a FubuMVC app.  I didn't ask this in that group because I thought this issue wasn't specific to fubu but I guess maybe it is.

I am using a nested container like so:

FubuApplication.For<ConfigureFubuMVC>()
.StructureMap(() => new Container(x =>
x.Scan(i =>
{
i.TheCallingAssembly();
PackageRegistry.PackageAssemblies.Each(i.Assembly);
i.LookForRegistries();
})))
.Bootstrap();

I can ask this within the fubu mailing list if you like.  Is there a recommended way of executing application tasks after structuremap/fubu configuration but before handlers are executed and only do it on application startup?

Thanks for your reply.
Gary Brunton

On Thursday, December 13, 2012 5:42:10 AM UTC-7, Jeremy Miller wrote:
Gary,

I'm not clear on what you're asking.  Are you just trying to find out what the types are, or to have the IApplicationStartupTask implementations run before any endpoints are executed?  What platform is this for?

Jeremy

On Dec 13, 2012, at 12:33 AM, Gary Brunton <gbru...@gmail.com> wrote:

Say I have an interface like this:

public interface IApplicationStartupTask
{
  void Execute();
}

When my web application starts up I have multiple registries I use for SM configuration.  One of the registries scans for these IApplicationStartupTask types.  This is all standard here.

My problem is that I can't find a way to get the implementations on application startup unless I specifically call the following:

ObjectFactory.GetAllInstances<IApplicationStartupTask>()

But I don't want to do that. I would rather find a way to instantiate a type that has the implementations injected.

To be clear, if after application startup an endpoint is reached and that endpoint has IEnumerable<IApplicationStartupTask> tasks in it's constructor, the tasks are populated as expected.  But again, I want to somehow get access to these tasks on application startup. Not when an endpoint is reached.

Thanks,
Gary Brunton

--
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.

Jeremy D. Miller

unread,
Dec 13, 2012, 11:35:24 AM12/13/12
to structure...@googlegroups.com
Oh, easy money then.  Use the IActivator interface from Bottles and register that with your container.  Or really, just convert your IApplicationStartupTask's to IActivator's and you're good to go.



To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/structuremap-users/-/W6YPjRPyHJEJ.

Gary Brunton

unread,
Dec 13, 2012, 11:42:45 AM12/13/12
to structure...@googlegroups.com
Ah yes... This seems like a basic/simple thing in fubu. I'm feeling ashamed I didn't see this earlier.

Thank you.  You work/help is sincerely appreciated.
Gary Brunton

Jeremy D. Miller

unread,
Dec 13, 2012, 11:53:28 AM12/13/12
to structure...@googlegroups.com
You mean you didn't see the thing that's not documented and never demonstrated in any blog post I'm aware of?

Docs coming soon...

Gary Brunton

unread,
Dec 13, 2012, 1:19:43 PM12/13/12
to structure...@googlegroups.com
Well here's a blog post demonstration...

To view this discussion on the web visit https://groups.google.com/d/msg/structuremap-users/-/W6YPjRPyHJEJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
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.
Reply all
Reply to author
Forward
0 new messages