Geeks With Blogs

The Life and Times of a Dev Yes, we're really that weird

We're writing an app for a client and we're using WCF and Nettiers together in the app.  One of the requirements of app is very good auditing.  Every time data is picked up in the system, we need to audit it and let people know what happened.

We wanted this to be transparent to the users, which meant that it should probably go into the DAL somewhere.  We thought about Triggers (no user information, so you'd have to pass the user to everything), and finally settled on putting it in the save, update, delete calls of nettiers.

We wrote our auditing code into EntityProvider.cs (the non-overwritten class) and then modified the templates to call the audit code.

There were various problems, none of them too hard to fix, but one major one that we ran into was in the mechanism that we were using to do diffs of the entities.  We basically were assuming that for each column inside of the database (Entity has a TableColumns array which tells you want columns are in the entity), there was a property as well.  Then, using reflection, we captured the value of that property and returned it to the caller, like so:

 private object GetProperty(object obj, string propertyName)object result = null;PropertyInfo pi = obj.GetType().GetProperty(propertyName);if (pi != null)null);return result;







result = pi.GetValue(obj,





To compare the old entity and the new entity, we assumed that we could make a clone of the entity and call CancelChanges and we'd have the original version. 

Entity clone =

MethodInfo mi = clone.GetType().GetMethod("CancelChanges");

This worked great, except when you sent the Nettiers entity down through a wcf service.

WCF only cares about the data contract, which means that the _originalData value from the Nettiers entity wasn't populated, and that's how nettiers does their cancel changes.








So, the original data was stripped out of the entity, and when you called cancel changes, you'd get a nasty NullReference exception, since OriginalData was null.

What to do, what to do.  What we decided to do was to make the original data, entity data, and backup data part of the DataContract in WCF.  To do this, we had to modify the nettiers templates to have OriginalData public properties and decorate them with [DataMember].  Then, the EntityData template had to be modified to mark the entitydata items with [DataContract] and the individual elements with the [DataMember] attribute.

Upon regeneration of the dal, everything now works as expected.  This solution works, but it does increase the payload substantially, since effectively everything is at least duplicated.  Was it worth the price?  Don't know.

We could have also solved this by simply getting the current version out of the database and looking for diffs.  That may be a better solution, but it does have its problems as well (database query, locking issues, etc).

We'll see what happens.

public override void CancelChanges()IEditableObject obj = (IEditableObject) this;this.entityData = null;this.entityData = this._originalData.Clone() as EntityEntityData; Posted on Friday, June 8, 2007 1:52 PM Work | Back to top

Comments on this post: NetTiers and WCF and object passing

# re: NetTiers and WCF and object passing
Requesting Gravatar...
I have recently run into the exact same problem. Not much good having the class decordated with {DataContract] if the correct internals do not have the [DataMember] attribute. I just download the 2.2 release of netTiers and it appears this problem has not been fixed. I have not seen much else on this and was curious if you heard any more info from the netTiers team. I think i will post something to the forum this week.
Left by matt b on Jul 30, 2007 4:15 AM

# re: NetTiers and WCF and object passing
Requesting Gravatar...
IMHO.. What about a private [Datamember]IsDirty flag that is set when a PropertyChangedEvent is fired?
Left by Michael Cheng on Aug 06, 2008 2:33 PM

Your comment:
 (will show your gravatar)

Copyright © Robert May | Powered by: