Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
3  
Just on a related note: we might all soon have to change our global.asax if you read Todd Carters mem leak post: todd-carter.com/post/2010/02/08/… –  Anders Rask Feb 9 '10 at 8:09
add comment

2 Answers 2

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.

share|improve this answer
add comment

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:

http://elenadimitrova.blogspot.com/2010/10/designing-sharepoint-applications-for.html

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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