In the particular project I'm working on we are already using Enterprise Library. I'm researching using it (i.e. Unity) for dependency injection.

The recommended way to include Unity is modify global.asax so that it exists for the entire length of the web application. I'm very reluctant to pursue this route primarily because of the increased deployment risk across multiple servers. A way that can take advantage of SharePoint's solution/feature deployment mechanism is much preferred. Also, Spence Harbar strongly recommends not to modify global.asax.

I noticed that SharePoint Guidance avoids EntLib and uses a Service Locator pattern. For reference, this article shows both EntLib and Service Locator options.

How should dependency injection and inversion of control be implemented within SharePoint?


I'm using the ServiceLocator for SharePoint from Microsoft P&P (SPG). Is doing the job ok, and I'm able to write Unit Tests without abusing of heavy artillery: TypeMock. There is one thing to know: if you activate feature on site coll. level or web app. level, you need to run with elev. privileges instead of current user, cuz the user activating the feature might no be a farm admin, so registering interfaces and classes in SPFarm property bag will fail.

Speaking about Microsoft P&P for SharePoint, I'm using also the Repository pattern and MVP when building webparts. Code looks very clean when using the combination Repository and ServiceLocator.

I did also search for other dependency injection frameworks for SharePoint. I know SL is not a DI framework, but for SharePoint I think is the best I can get. Unity for SharePoint scares me, I don't wanna do unsupported modification to customers to be able to use Unity.

| improve this answer | |

I've been using the Autofac DI Container successfully in my past few projects. It's more lightweight than Unity and faster as it doesn't use reflection for its resolves. Using that for unit testing has so far been smooth. More detailed overview of my setup is here:


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.