1

I'm working on legacy code in a DotNetNuke module, trying to get classes and behaviors under a testing framework: I'm taking this opportunity to follow advice from the "Working effectively with legacy code" book, so what happens is that i'm trying to define areas which can be tested thoroughly and then converted to services. Then i'd like to use an IoC framework for it to work. For now, i've set eyes on Ninject.

However i'm hitting a design problem: as i'm in a DotNetNuke module, i can't really change application-wide structure: for example i can't derive the Application from NinjectHttpApplication. I can't use these suggestions from SO either. I was thinking about having the Kernel in a static class that my module would set up and then use but from what i've read around it's a very bad idea.

So i'm starting to ask myself if it's possible to use an IoC in an application that hasn't been set up to support it from scratch. If i'm supposed to have a whole dependency tree loaded for each request, how can i rewrite legacy code locally and benefit from IoC? Is there a pattern where IoC use can grow out from very local rewrites?

Even though i'm working with DotNetNuke, any standalone component that can be installed into an independent framework begs the same question. Also i'm not targeting Ninject specifically, if another IoC framework can help in this case i'm willing to consider it.

Community
  • 1
  • 1
samy
  • 14,832
  • 2
  • 54
  • 82

1 Answers1

2

From my experience, your best bet to get this type of abstraction within the context of DotNetNuke is by using the WebFormsMVP framework. This is really the only sane way I've found to do unit testing in a DNN module, and if memory serves I spent awhile trying to wire up Ninject a year or so ago.

But be warned, it is still WebForms and will never be drop dead simple. And without knowing your existing code base, I'd have a hard time knowing how easy it will be to make the migration.

I have a couple of resources on GitHub that you can check out for reference:

The first is a module template that should act as a solid starting point:

https://github.com/irobinson/WebFormsMvp-DNN-Module-Template

The second is a small example project:

https://github.com/irobinson/BeerCollectionMVP

Depending on the version of DNN you're using, it may or may not already ship with WebFormsMVP, but you should be able to either bundle the dependencies w/ your module or upgrade to the newer version of DNN if that's reasonable.

Ian Robinson
  • 16,892
  • 8
  • 47
  • 61
  • Thanks Ian. What's troubling me is that the IoC pattern really seem to be a "all or nothing" approach in this case; i can't use the IoC container in some areas without having to prepare my framework at a very low level. To give me an idea, how long did you spend on your wiring of Ninject? – samy Dec 19 '12 at 08:50
  • Sorry I don't have a very good answer for you on that one - I probably spent a couple hours a night looking into it for a few days out of a week. It wasn't a tremendously strong effort. It might be worth investigating again. And honestly there may have been some changes to DNN itself in the meantime that would make it simpler - who knows :) – Ian Robinson Dec 28 '12 at 23:18