Friday, December 28, 2007

Coding Standards and Design Guidelines

I get a lot of people at work asking me about our coding standards and guidelines. We do have some, but to be perfectly honest, they could be better.

I like to utilize the Code Analysis feature of the Visual Studio version I am running, and let the compiler help with warnings and errors when rules are broken. Unfortunately this is not available with all versions.

I also found two very useful articles on MSDN that help a lot with defining standards:

Development Books

A colleague sent me this link to an online book store in Australia that specializes in computer books (development, networking, project management, etc).

Postage should be a lot cheaper than ordering from Amazon, and the prices are way lower than our local stores in Perth.

Thursday, December 27, 2007

Optmistic Concurrency withTimestamps, LLBLGen and Web Services

Found a great article on how to implement optimistic concurrency with LLBLGEN and SQL Server:

Optmistic Concurrency and Timestamps in SQL Server Using LLBLGen Pro O/R Mapper by David Hayden.

Now, I am using XML Web Services and passing entities back and forth, and what I have found is that the DBValue property on each field is NOT serialized! I have not found a better solution as yet, but I am currently just using the field's CurrentValue. So, assuming the timestamp properties used do not get changed on the client, it will work, but this is a problem that will need to be fixed.

Update: Looks like there was an issue with the deserialization in version 2.5 (October 25 2007 release). The December release has apparently fixed this issue:

Modifications with LLBLGEN 2.5

I am inherently lazy, which is a bad thing when it comes to coding, but the solution described by LLBLGEN's help and David Hayden relies on a factory class for each entity. Sure, we should probably modify the templates and create a new factory class for each of our entities, but for what I wanted, that sounded like too much work.

Now, in LLBLGEN 2.5, all entities are now derived from another generated partial class called CommonEntityBase. And, lets face it, we are probably going to create our timestamp field on every entity and call it the same name (in this case, every entity has a timetamp field called LastModified). So, I created a new partial class definition for CommonEntityBase that allows me to get access to the timestamp field I will use for all entities' concurrency control.

public partial class CommonEntityBase
private const string LAST_MODIFIED_FIELD_NAME = "LastModified";

public IEntityField2 LastModifiedField
get { return this.Fields[LAST_MODIFIED_FIELD_NAME]; }

This then allows me to create a single factory for concurrency predicate expressions:
public class EntityConcurrencyFactory : IConcurrencyPredicateFactory
public IPredicateExpression CreatePredicate(ConcurrencyPredicateType predicateTypeToCreate,
object containingEntity)
IPredicateExpression toReturn = new PredicateExpression();
CommonEntityBase entity = (CommonEntityBase)containingEntity;

switch (predicateTypeToCreate)
case ConcurrencyPredicateType.Delete:
case ConcurrencyPredicateType.Save:
// only for updates
toReturn.Add(new FieldCompareValuePredicate(entity.LastModifiedField,
null, ComparisonOperator.Equal, entity.LastModifiedField.CurrentValue));
return toReturn;

Two classes for all entities - sounds good to me. Now, using David's example, there isn't any difference in the code (just every entity uses the same factory):
DataAccessAdapter adapter = new DataAccessAdapter(...);

BlogEntity blog = new BlogEntity();
blog.BlogId = 1;


blog.Name = "New Name";

IPredicateExpression expression = new EntityConcurrencyFactory()
.CreatePredicate(ConcurrencyPredicateType.Save, blog);

adapter.SaveEntity(blog, true, expression);

You can still change optimistic concurrency strategies easily, but it is easier to apply to all of your entities at once.

Tag Cloud for Blogger

I found a neat bit of code to turn your standard tag list into a "cloud".

This one is okay: Compender by Raymond May Jr.

This one is better (in my opinion) and is the one I am using: New Blogger Tag Cloud / Label Cloud

Monday, December 10, 2007

Can't Connect to Machine via Remote Desktop

I had a machine I could not Remote Desktop (RDC) to. No matter what I did, I got the same error:

The connection was ended because of a network error. Please try connecting to the remote computer again.

In the System event log on the machine I was trying to connect to, I found quite a few Error entries with the source TermDD. This described the following error:

The RDP protocol component "DATA ENCRYPTION" detected an error in the protocol stream and has disconnected the client.

After a quick search, I found the following Microsoft KB article that helped me fix the problem:

"The RDP Protocol Component "DATA ENCRYPTION" Detected an Error..." error message

A reboot on the machine and it is now working fine.