EPiServer  /  CMS December 20, 2010

One small step for EPiServer, one giant leap for our code

This is what happens when you blog in the middle of a weekend during which you work 12hrs/day. IDataFactory is not included in EPiServer CMS 6 R2 Beta but in a preview of another version. On the other hand it could be included in the actual release of R2 (along with IPageData?). Pretty please EPiServer? Give me a white Christmas?

When Allan Thraen at EPiServer blogged about EPiServer CMS 6 R2 he had a picture of Christmas presents in his post. While many of the features that he mentions are really nice and useful it didn’t get me all that excited though. It seemed like the EPiServer Santa didn’t get me.

However, this weekend, doing what I often do, clicking around in Reflector, I stumbled upon something. Here’s what I saw:

EPiServer DataFactory viewed in Reflector

A little closer:

The declaration of EPiServer DataFactory viewed in Reflector

Still not seeing it? Let me magnify it for you:

Magnified view of EPiServers DataFactory implementing IDataFactory

That’s right! In The beta version of CMS 6 R2 the DataFactory class implements an interface called IDataFactory. The IDataFactory interface declares all of DataFactory’s members meaning that we can now write code that doesn’t depend on the concrete DataFactory class but on an abstract interface instead.

This has some pretty big implications. From an OO stand point it’s great as this makes it easier for us to create classes that abide by the SOLID principles, especially the Dependency Inversion Principle:

"Depend on abstractions, not on concretions."

In practice it makes it much easier to create unit level tests for code that depend on the functionality in, or rather the interface exposed by, DataFactory. It also makes it possible for us to switch implementation of some or all functionality used by our code. For instance we could create our own implementation of IDataFactory that delegates to DataFactory.Instance but with some additional logging and replace which implementation our code uses without modifying our code. That of course requires that we have implemented some sort of inversion of control for our code though.

How we can use it

To reap the benefits of the addition of the IDataFactory interface we need to be able to inject instances of it into our code or fetch instances of it using a Service Locator. However, as I feel that Service Locator is somewhat of an anti pattern let’s focus on how to inject dependencies into our code.

There are a number of ways to do that. Unfortunately not many of them match the standard way of using Web Forms.

We can move business logic into non-presentation layer services to which it can be injected.

We can use ASP.NET MVC and create our own controller factory that injects it into controllers, although it should be noted that EPiServer doesn’t support MVC yet.

We can use EPiMVP (or some other MVP framework) and create a presenter factory that injects it into presenters.

And we can inject instance of it into PageData objects if we use Page Type Builder by using a custom TypedPageActivator or the one that uses Structure Map that ships with Page Type Builder. For more on injecting dependencies into PageData objects be sure to check out this post.

I hope to describe some of these in more detail in future posts.

We’re not quite there yet

Although I think that the introduction of IDataFactory is great it’s important to note though that EPiServer still has a way to go in order to make their API easy to consume when it comes to unit testing and some general design principles like the SOLID principles.

For instance there are a few members of the PageData class that we need to be able to isolate our code from. This could easily be fixed by making them virtual or perhaps by letting PageData implement an interface that declares those members.

There are also quite a few other classes with similar problems as well as some classes with static members that are hard to isolate code from. I’m guessing that’s to much to ask Santa for for this Christmas though :-)

In the meantime we can still use EPiAbstractions which provides facades for many classes in the EPiServer API, as well as a multi-purpose toolset for creating testable and flexible code that uses EPiServer CMS.

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more


comments powered by Disqus

My book

Want a structured way to learn EPiServer 7 development? Check out my book on Leanpub!

More about EPiServer CMS