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:
Friday, December 28, 2007
Coding Standards and Design Guidelines
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).
http://www.bookware.com.au
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:
http://www.llblgen.com/TinyForum/Messages.aspx?...
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));
break;
}
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;
adapter.FetchEntity(blog);
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.
Friday, November 30, 2007
Accessing the Current Principal in VSTO Add-In
I recently tried to get access to the Current Principal within an Outlook Add-In. Accessing the Identity was returning a blank name and an unauthorised user. Wierd.
It turns out the the VSTOLoader will load each Add-In into its own AppDomain. As such, you need to set the principal policy explicitly before you access the current principal for the first time.
Example:
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
string userName = Thread.CurrentPrincipal.Identity.Name;
As it running within an isolated environment, I am sure there are other issues you need to be aware of.
See the AppDomain Class in the MSDN Library for more information on application domains.
Thursday, November 29, 2007
Accessing Custom Resource Mailbox Properties With VSTO
In Exchange 2007 you can create custom resource properties to annotate your resources. For example, you may want to create a custom property called Vehicle and add this to all resources that represent vehicles in your Exchange organisation. The TechNet article, How to Create or Remove Custom Resource Properties, describes how this is done.
Getting access to these custom resource properties via Visual Studio Tools for Office (VSTO) is relatively straight forward, as long as you know exactly what you need to do! The problem is finding this with the documentation and newsgroup postings out there.
Anyway, access to this type of information is not available via properties on the Outlook Object Model. However, in VSTO 3 (for Outlook 2007), most Outlook objects have a PropertyAccessor object that allows you to get access to ALL the properties on the underlying MAPI object, not just the ones exposed by the COM interface. The GetProperty method takes a string identifying a property on the object and returns the value as an object. This string identifier consists of a namespace and some sort of property tag value. There are several namespaces that can be used, but the one used here is http://schemas.microsoft.com/mapi/proptag/. More information on referencing properties by namespace can be found in the MSDN article outrageously called Referencing Properties by Namespace.
Finding out the property tag value is next to impossible looking at the MSDN documentation. They simply don't document it, which I found incredibly frustrating. That is, until I found a copy of Outlook Spy. This is an excellent tool that allows you to browse the object model and also get access to the MAPI objects, including the elusive property tags!
Getting back to custom resource properties, I used Outlook Spy to examine one of my vehicle-type recipients. Under the AddressEntity property is a MAPIObject. If you "browse" this property, a window is displayed listing all of the properties of that MAPI object, including the property tag value, the type and its current value. On the right of the window is a field called DASL. This field is the value that you need to pass into the PropertyAccessor.GetProperty() method to extract its value. In my case, the property listing the custom resource properties was using the DASL http://schemas.microsoft.com/mapi/proptag/0x0806101E.
So, once you have this, the code is simple. The following example assumes you already have a Recipient object which is an Equipment resource with the custom property Vehicle.
public static bool IsVehicleResource(Recipient aRecipient)
{
bool result = false;
if (aRecipient.Type == (int)OlMeetingRecipientType.olResource)
{
try
{
//
// Get resource info property via MAPI property accessor.
// NOTE: this will throw an exception if it doesn't exist (ie: not a resource)
//
object resourceInfo = aRecipient.AddressEntry.PropertyAccessor.GetProperty(
"http://schemas.microsoft.com/mapi/proptag/0x0806101E");
//
// Split comma separated info into parts and look for information identifying
// the resource as Equipment and Vehicle.
//
string[] resourceDetails = resourceInfo.ToString().Split(',');
result = (resourceDetails.Contains("Equipment") && resourceDetails.Contains("Vehicle"));
}
catch (System.Exception ex)
{
// TODO: error handling
}
}
return result;
}
And that is it!
For more information on using the PropertyAccessor object, have a look at the MSDN documentation online.

