Sunday, November 9, 2008

C# 3.0 Cookbook

  Every once in a while you run into a reference that should be on every C# developers desk. The C# 3.0 Cookbook is just such a reference. It covers a ton of everyday how do I's for every thing from Exception handling to LINQ queries. Each chapter is broken down into a series of Problem, solution, discussion so you get a clear understanding of not only what problem is solved and how, but more importantly, why it was solved the way it was.

Some of the solutions have been around since .Net 1.0 and others are new to 3.0. If you are looking for a great reference, this is the one.

Saturday, November 8, 2008

Changing Source Control

Up to this week I've used Source Safe for my Source control at home. Since Installing Visual Studio 2008 SP1 though, source safe has become a problem. It locks up and is unable to complete file operations more often than not and even after running the db analyze utility, the problem still exists. So after a couple of weeks of this, I decided to switch Source control.

My requirements are pretty straight forward

  • It needs to be easy to install and maintain
  • It needs to have the ability to migrate from source safe
  • It needs to be free for a single user
  • It needs to be smart enough to exclude "noise" from the repository
  • It cannot add extra "noise" to the project working directory
  • It must plug into Visual Studio at no extra cost

To clarify the term "noise", is additional files and folders added by either the source control system, visual studio or third party addin's such as Code Rush, which creates a cache folder for intellisense.

  At the office we use subversion. Personally I find subversion annoying.  First it has no plugin without purchasing a license of Visual Svn or some other third party plugin. It adds a ton of "noise" to the project directory in the form of .svn folders and you have to define exclude lists for the bin and obj folders as well as any other "noise" folders. Additionally it's not smart, if you change a file, say for example add a space, then undo the change, subversion still insists it modified. So based on those issues, subversion was out.

I know there are a number of other free and open source source control alternatives, but in the end I ended up with Source Gear's Vault. Vault uses SQL server as it's repository, so maintenance and backups are greatly simplified. And it installed on SQL 2008 with no issues. Vault has met or exceeded the requirements I listed above and for a single user it's free if you use the provided admin login to access it. Optionally you can purchase a single user license for $249, which is very reasonable given the features and functionality of Vault.

Overall I'm pleased with the switch from Source Safe to Vault, the conversion process to a little less than an hour including installation of the client and server and migration of projects from Source Safe.

Friday, November 7, 2008

Catching up

It's been a busy couple of months for me. I was recently promoted to Development Lead and that's consumed a large chunk of time. I've been busy juggling sprint schedules and new developers, but fortunately, I've still found time to code a fair amount, although I suspect that will change in the near term unless I change organizations, which is always something that could happen in IT given the current economic environment.

  We did for good or bad drop the Entity  Framework for the application refresh I talked about a couple of months ago. LINQ to SQL was just a better all around fit. I suppose we'll change that layer again at some point since Microsoft seems to be determined to ditch LINQ to SQL in favor of the Entity framework.

We also switched the communication layer to WCF from remoting. The transition was far more than expected because we had to implement an uber security context, which is not as straight forward as it would appear given the constraints imposed by the architect.

All in all though it was a good opportunity to roll up my sleeves and get into some of the new technologies. The only piece we could have leveraged and didn't was the workflow foundation.

You'll notice that I said communications layer as opposed to service layer. There seems to be a great deal of confusion around these terms and specifically what they mean. I define a service layer as providing some piece of functionality that provides business value via a service interface. And communications as a layer that sits between the business and presentation layer in a distributed application and provides access to the business and data layer. Of course I talked to people and work with people who think that if it's WCF and provides an interface, it's a service. I suppose that is the result of the "loose coupling" of terms that is currently so prevalent in our industry.

Saturday, September 20, 2008

LINQ to SQL Expressions and WCF

I'm currently involved in an extensive migration of an existing .Net remoting architecture to WCF. The original architecture used Net Tiers in the data layer, which has been replaced by LINQ to SQL. We opted for LINQ to SQL over the Entity Framework, because EDM frankly offers little for a great deal of overhead additionally it just doesn't support functionality that it should.

One of the issues that we ran into during the migration was the migration of services methods that relied on client side criteria to filter the data, the original implementation used code that was heavily dependant on the Net Tiers metadata to write the queries on the client and pass them to the proxy. Initially  it looked as if Expression<Func<T,TResult>> would provide a perfect solution, it allowed us to create the expression and replace the current implementation with little impact. Unfortunately, it turns out that this solution won't work since Expressions can't be serialized. So we were left with the option of rolling a new dynamic query engine that could build the expression trees on the server, based on a string passed from the client. Initial discoveries lead us to believe it was feasible to create the expression in this way, but it would cost hours that we really didn't have.

  Then I ran across this article on ScottGu's blog regarding LINQ dynamic queries. After downloading the C# samples and looking at what it contained, it turned out that for the most part it contained most of the functionality we would need to create. So if you find yourself in need of the ability to pass queries across the wire to LINQ this is a great place to start.

Tuesday, September 9, 2008

Working around DataContract serialization with existing .Net classes

I'm working on moving some existing code to WCF. The code was originally passed via remoting so I thought it would be a pretty straight forward move. Turns out that's not the case. I ran into an issue with properties defined as System.Object and WCF. The error message that was returned was telling me that the connection was forcibly closed by the host, which I'm finding out can really be interpreted as "There was a problem with the object you want". After tracking the problem for the better part of the day it came down to an issue with the properties which looked like this:

[DataContract]
public class ContractTest
{
[DataMember]
public int PropOne { get; set; }

[DataMember]
public object PropTwo { get; set; }

[DataMember]
public string PropThree { get; set; }

[DataMember]
public object PropFour { get; set; }
}





It turns out that the serialization of the data contract can be altered by using the XmlSerializer. This is really helpful when moving older code. So to fix the issue, I modified the class like this:



using System.ServiceModel;

[DataContract]
[XmlSerializerFormat]
public class ContractTest
{
[DataMember]
public int PropOne { get; set; }


public object PropTwo { get; set; }

[DataMember]
public string PropThree { get; set; }


public object PropFour { get; set; }
}

The XmlSerializer will serialize the public properties and the end result is that you can now pass the class through the service noramlly.

Sunday, August 17, 2008

ADO.Net Entity Data Framework

I've spent a great deal of time with the Entity framework over the past month trying to incorporate into an application solution. I had high hopes that the framework, but after a month it's left me with a bad taste. No matter how you slice it, this framework is not ready for prime time. Here are a few issues that I have with it so far.

1. The Conceptual Data model does not allow me to abstract from the data store, I thought this was the great promise of this model but it turns out to be the great impendence mismatch magnifier. The mappings are absolutely unforgiving in complex situations. Mapping is really an all or nothing scenario, if every key isn't matched you'll never be able to map the entities.

2. The EDM modeler has a real difficult time with poorly normalized data stores. Arguably this could be addressed by cleaning and normalizing the database, however, it's currently not an option in this project and I'm sure that this is not an uncommon enterprise scenario.   As a result it is nearly impossible to map the entities in the store, even though it can be accomplished with LINQ2SQL.

3. I've run into issues where the model generator will, for whatever reason, just arbitrarily change the namespaces. and the fact that you can view the conceptual model at all is just unbelievable given that the conceptual model is such a central part in the framework.

4. Now I have to learn a number of new query technologies in order to use the model effectively. And coincidentally none of these models provides any real benefit when you need strong typing and generic runtime support.  

So if I had a chance to start over, LIN2SQL would be my first choice, especially given the the DB lives on SQL Server. The LINQ2SQL programming and mapping models are more forgiving and easier to work with as well as being functional.  If these issues and the multitude of other issues addressed in other blogs and numerous forums aren't addressed, the Entity framework will be stillborn. My advise to anyone who is considering LINQ2SQL and EDM for a SQL server based project would be this, save yourself pain, time and productivity and go with LINQ2SQL.

Tuesday, August 12, 2008

In case you've been in a cellar (VS2008 SP1)

 

  Visual Studio 2008 SP1 and .Net 3.5 SP1 have been released, you can download them from the links below as well as a the service pack prep utility.

Visual Studio 2008 SP1 and .Net 3.5 SP1 download

Service Pack Preparation Utility

My installation on two machines went without a hitch. I completely uninstalled SQL server and Visual Studio , deleted the remaining directories and defragged before reinstalling. I installed in the following order on both machines.

  1. SQL Server 2008 Developer
  2. Visual Studio 2008
  3. Visual Studio 2008 SP1

SQL server will install the .Net 3.5 service pack. 

So far everything runs fine, one disappointment thought was the missing WPF controls promised during the beta. The Office ribbon bar is nowhere to be found but the new grid control is available separately on Codeplex  as part of the WPF Toolkit.