Disconnected n-tier usage of Entity Framework objects

To enable the use of Entity Framework objects in a disconnected scenario, we use 3 projects together:
Read more here: http://www.sitechno.com/Blog/DisconnectedNTierObjectsUsingEntityFramework.aspx and here: http://www.sitechno.com/Blog/NewSampleApplicationForDisconnectedNtierEntityFrameworkObjects.aspx.

What we need to achieve is work from one domain layer, but create two versions of it. This is not done in source, but by enhancing the dll.
Client side: use Circular Serializer and Editable Business Objects
Server side: use Circular Serializer and PostSharp4EF

ntier.png

The end result would be two solutions (one for the client, one for the server) that both reference the same Domain project (which holds your business objects). When compiling, the correct enhancements need to be executed.

please note that all these steps are simplified when using TFS or nAnt to manage builds where more control is possible

Step 1: get a conditional compilation symbol
Open the proj file of your domain project. Add the following code to it:
<PropertyGroup>
       <DefineConstants>$(DefineConstants);$(SolutionName)</DefineConstants>
</PropertyGroup>


This will set your normal constants, but add the solution name to it. As an alternative, you might want to use a <Choose> group and create more elaborate schemes of determining in what context this project is loaded.

Step 2: use conditional attributes
I like to leave my domain objects completely clean, and so I put my enhancement attributes in the assembly info of my Domain project.
The following example is based on solutionnames 'Server' and 'Client'.
#if Server
[assembly: Poco("InheritanceTypeConnection", AttributeTargetAssemblies = "Domain", AttributeTargetTypes = "Domain.*")]
[assembly: CreateSerializeSurrogate("EntityFrameworkContrib.PostSharp4EF.ISerializationHelp, EntityFrameworkContrib.PostSharp4EF", 
AttributeTargetAssemblies = "Domain", AttributeTargetTypes = "Domain.*")]
#endif
#if Client
[assembly: EditableBusinessObjectAttribute(AttributeTargetAssemblies="Domain", AttributeTargetTypes="Domain.*")]
[assembly: CreateSerializeSurrogate("EditableBusinessObjects.Postsharp.Public.IEditableBusinessObject, 
EditableBusinessObjects.Postsharp.Public", AttributeTargetAssemblies="Domain", AttributeTargetTypes="Domain.*")]
#endif


We instruct PostSharp to enhance all objects starting with namespace Domain, in assembly Domain to either use Poco and CircularSerializer or EditableBusinessObjects and CircularSerializer.

(optional) Step 3: Create different configurations so that Domain does not get overwritten
When compiling your solution, the Domain.dll is placed in either Debug or Release folder. However, you are very likely to compile both the client and server solution at the same time. It might be prudent to create a new configuration so that Domain gets placed in different folders.
In the end, the only thing that is important, is the dll that is copied to either the client project or the server project.

Please take a look at the client/server samples that are included with the source code.

Last edited Apr 16, 2008 at 7:53 PM by RBoeke, version 3

Comments

No comments yet.