3
votes

I am reading StructureMap about dependency injection, well there are two parts first to initialize the mapping, interface to concrete class type, and another one is just instantiation (asking for an instance).

First part requires configuration, setup which is mentioned to be done at a boot strapper.

what s the best practice for a boot strapper? static class with static constructors? how about in IIS?

Also, how can i configure Structure Map so that without restarting the application, I can change the dependencies? is that possible? how?

2
What do you mean by how about IIS? The answer totally depends on the framework used (Web Forms, MVC, WCF, etc.), not the server itself. - Mark Seemann

2 Answers

2
votes

The configuration is done in the Composition Root. IoC container support for ASP.NET WebForms is very bad. The pages are created by IIS. The only thing you can do here is to inject properties after the page is created.

If you want to do DI for Websites then you should use MVC 3 instead of WebForms. In this case there is an integration package Structuremap.MVC3 that does the bootstrapping for you. You can find it on nuget. https://github.com/webadvanced/Structuremap-MVC3

1
votes

Using a static class with a static constructor doesn't help much, because the static constructor won't get called until the class is actually used by the running code. Hence your best option is to bootstrap the DI in the program's main() method.

In IIS or similar environment there are usually events that get fired when the application / add-in / component is loaded or 'started'. In ASP.NET (that is in IIS) the global application events in the global.asax.cs file serve this purpose.