ASPX templates
Version 7.x > ASPX templates > DI / IoC with page templates and web parts View modes: 
User avatar
Member
Member
bdrenner-new42 - 1/16/2014 9:47:09 AM
   
DI / IoC with page templates and web parts
I want to unit-test my Kentico page templates and web parts. I can't find any information about dependency injection / inversion of control with Kentico. Does anyone have a good technique for injecting dependencies?

All I can think of is to create a service locator (yuck) and wrappers for Kentico's static API methods.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 1/17/2014 7:09:41 AM
   
RE:DI / IoC with page templates and web parts
Hi,

There is no native support for this, regrettably.
We know it and there are being made changes in the architecture and API to support this in the future. It is a bigger change so it takes some time to fully support this.

Best regards,
Juraj Ondrus

User avatar
Member
Member
bdrenner-new42 - 1/17/2014 1:56:28 PM
   
RE:DI / IoC with page templates and web parts
I was under the impression that Kentico used a custom IHttpHandlerFactory, but I just perused the Kentico source (using ILSpy), and it seems like Kentico only rewrites URLs before allowing the built-in ASP.NET PageHandlerFactory do its work. If that's accurate, then it shouldn't be difficult to slip in a custom PageHandlerFactory that wraps the built-in PageHandlerFactory and uses a DI container. ...Right? Or am I barking up the wrong tree?

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 1/27/2014 7:37:39 AM
   
RE:DI / IoC with page templates and web parts
Hi,

I am sorry but I am little bit confused and I think I am missing something. Would you be so kind and describe it on some example? What behavior you would like to test?
What web part you would like to use and what is the expected result/behavior?

Thank you!

Best regards,
Juraj Ondrus

User avatar
Member
Member
bdrenner-new42 - 1/27/2014 9:49:26 AM
   
RE:DI / IoC with page templates and web parts
It's a hypothetical situation. For example, imagine I've made a page template that lets a user invite a friend to the site. The template has a text field for the friend's email address, and a submit button. When a user submits the friend's email address, the event handler should send the invitation email to that user.

For sending invitations, I want the page template class to call on an `IInviter`, which exposes `Invite(string email)`. The page template class, itself, should not determine its own concrete `IInviter`. I want to be able to swap in a dummy implementation, or one using `System.Net.Mail`. I also want to unit test the event handler method, in which case I would swap in a mock `IInviter`.

User avatar
Member
Member
bdrenner-new42 - 1/27/2014 9:53:46 AM
   
RE:DI / IoC with page templates and web parts
Here's an article about using a custom PageHandlerFactory to affect IoC:
http://aspnetresources.com/articles/ioc_and_di_with_web_forms

My lingering concern is whether Kentico obfuscates the raw ASP.NET Web Forms way of accomplishing this.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 2/3/2014 1:43:42 AM
   
RE:DI / IoC with page templates and web parts
Hi,

You are right, Kentico does not use a custom page handler factory and you are free to provide your own as mentioned in the linked article. However, I can't stress enough one important fact; there's little value in testing web parts or page templates. The UI tests are difficult to write, they take long time to run, the coverage is low and they tend to be very fragile. To focus on testing your API is much better choice. You are certainly on the right track. I recommend you write an integration test. It requires a test database and you must call the CMSContext.Init method before the actual test runs. Without this call the application wouldn't initialize and you wouldn't be able to use the Kentico API. At least you need to perform the following steps:

• Create a test project.
• Add references to the System.Data, System.Xml, CMS.CMSHelper, CMS.DataEngine, CMS.DataProviderSQL, CMS.SettingsProvider, CMS.SiteProvider and CMS.Synchronization assemblies.
• Add an application configuration file with a test database connection string.

There's less friction in version 8. You do not need a database as you can mock the database object providers, and there are predefined test suite classes to derive from that shield you from the necessary plumbing. However, better support for testability is currently in scope of version 9.

Best regards,
Juraj Ondrus

User avatar
Member
Member
bdrenner-new42 - 2/3/2014 9:19:56 AM
   
RE:DI / IoC with page templates and web parts
Thank you, Juraj, for your always reliable and helpful responses!

> There's little value in testing web parts or page templates.

I've come to more or less the same conclusion. The code-behind for my web parts and page tempates tends to be a thin layer of glue that calls into a deeper "application" layer. Nothing depends on the UI layer, itself. The effects of refactoring could better be caught by integration testing.

Still, in a perfect world, unit testing code-behind methods would be easier. I'm excited for Kentico 8 and 9.