Skip to content

Building a WCF architecture using OpenAccess – setting the scene

October 29, 2009

So it’s time for part 1 of my series on building a WCF architecture using the Telerik OpenAccess ORM as a persistence service.

Just to point it out: The series assumes that the reader has some insight into .NET development in general and how an ORM works.

The ObjectScope

Anyway let’s dive into the details. First of all some background:

When using OpenAccess to persist objects, you deal with a thing called the ObjectScope. To boil it down to the core, you can see the ObjectScope as a cache where all objects you create, fetch and change are held until they are committed. It is also the ObjectScope that handles transactions – or at least begins and commits or rollbacks them. Be aware though (I have seen a lot of misunderstandings in this area in the Telerik forums):

There’s no 1 to 1 relationship between the ObjectScope and a physical database connection

Instead, whenever an ObjectScope needs a connection (for example during queries on objects) it pulls one from the connection pool, uses it, and returns it back again. So although you might have a long-lived ObjectScope it does not have a long-lived connection to the database. That’s in general – naturally (as always) – there’s exceptions, but we won’t deal with it within the scope of this post.

Ok, enough background on the ObjectScope so far. If you need to know more go to the Telerik forums or documentation.

WCF – short

In WCF you have requests coming in from clients to a specific service method and you have responses going back to client from that service method. The general guideline when implementing services it to make them stateless. That is, they need not to know what the previous request was about – in short: They get called, perform some work, may return a result and forgets everything about it (that would be nice to do in real life, right 🙂 )

“Business transaction”

Based on several projects using WCF, I have come to the solution that in general you can consider a service method as your “business transaction”: Everything within the business workflow carried out by the service must be successful, otherwise the service method will fail. For example a service method called “CreateCustomer” might perform several steps within its workflow: Create the customer entity, call a geocoding service on the customer address, etc.. All of these steps within the main workflow (customer creation) must succeed.

Mixing my “WCF with a twist of ObjectScope” drink

With a definition like the one above we have a really good candidate location for our transaction demarcation. Start the transaction when the workflow begins and commit the transaction if all steps in the workflow succeeds, otherwise perform a rollback. The transaction I am talking about is not a database transaction. Nope, it brings us right back to the transaction managed by the ObjectScope I was starting out with.

But, to handle a transaction within OpenAccess, we need an ObjectScope. So how do we make sure that ObjectScope is always available in our services?…

WCF extensions to rescue!!

WCF has been designed from the ground to be extensible and configurable and it sure is. It has several extension points there you can plug in code to be executed. The need we have is to do something upon receiving a request and upon sending the response to that request… The place in WCF terminology to do so is called a behavior. With a behavior you can change the way the WCF pipeline handles request… There are several behavior types, but the one we’re looking for is called an endpoint behavor… and right there we will have a message inspector.

Ok, now the scene is set. In the next part of this series, I will show you how to apply all the nifty parts to make this work – all with code…

Stay tuned – I’ll be back this week

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: