Category Archives: .NET

Advice for .NET Web Developers: Learn the command-line!

Visual Studio has spoiled us .NET developers. In Visual Studio we have a GUI that does everything for us: compiling, source-control, editing, you don’t even have to remember class or method names because of auto-complete and parameter guessing! It is all there and it holds your hand and it feels safe and easy.

The next version of ASP.NET is going to use a lot of the web functionality that many .NET devs have never seen: NPM, Bower, Grunt, and others. Visual Studio will probably cover the front-end of those things, but it will be invaluable for you to learn them the way they were meant to be used: from the command-line.

Oh Unit Tests? Wherefore Art Thou, Unit Tests?

There are few things that help a developer maintain an application better than Unit Tests. There are a few things that make me nervous when I open a solution from VSS, but nothing makes me more nervous than a lack of unit tests projects.

I know the Unit Testing drum has been beaten for years, so instead of getting on a soap box and berating the Internet for not Unit Testing, I’m going to give you a story about a recent project I worked on, and how unit testing helped me succeed.

Continue reading Oh Unit Tests? Wherefore Art Thou, Unit Tests?

Web Services, Overhead and Performance

Earlier this week a friend of mine asked me for some advice over an argument that some developers at his company were having.  They are collecting data from a device, and one developer wanted to have the device send the data to a web service to receive the data.  The other developer thought a web service would be to much “overhead” and wanted a different solution using direct writes or XML to the database and a UDP socket notifier for the server to parse the data.

Continue reading Web Services, Overhead and Performance

NHibernate vs. SubSonic Another Look

Earlier I had compared NHibernate and SubSonic and chose SubSonic because it had migrations.  I’ve done two projects, one using SubSonic and the other using NHibernate and I can now compare the features a little better.

NHibernate is documented better than I originally thought if you look at nhforge.com, not at the hibernate site.  It also does some nice schema generation based on your mapping.  You still have to go through “XML Hell” to map the site, unless you use another mapper or FluentNHibernate (which does the mapping for you in code) or another tool.  As an ORM SubSonic feals cleaner and quicker since it generates the ORM classes from the database, where-as NHibernate generates the Schema from your mappings and ORM classes.

Continue reading NHibernate vs. SubSonic Another Look

Comparing Open Source ORM: SubSonic vs. NHibernate

ORM tools are probably one of the most time-saving tools for a web developer.  Nothing is more tedious than building a data access layer, adding caching or coordinating transactions.  Well okay, sometimes things like that are really fun, but when you’re trying to start a project out from scratch you want to get something up and running ASAP to show to your customers.

In the .NET world there are two powerful ORM tools out there, NHibernate and SubSonic.  NHibernate is based on the successful and popular Java EE ORM Hibernate.  SubSonic is an entirely new ORM developed in the Ruby on Rails ActiveRecord development model, with some nice .NET 2.0 features like Controllers that you can use with ObjectDataSource controls.

For a recent project I quickly evaluated both software tools, and I’m publishing what I learned here.

Continue reading Comparing Open Source ORM: SubSonic vs. NHibernate

The Open / Closed Principle

I just read a great article in MSDN Magazine (available online here) on the Open / Closed Principle from the Patterns in Practice column.  I really enjoyed it.  I’m going to have to spend some more time learning some more design patterns.  I’ve spent some time learning Model-View-Presenter (MVP) and Inversion of Control (IoC) but haven’t put either into practice yet.  Who knows what the future holds though?

Using var And Living With The Consequences

Another new feature added in C# 3.5 to accommodate anonymous types is the introduction of the var type keyword.  In his Coding Horror blog Jeff Atwood is extolling the virtues of using var to reduce type declaration redundancy.  His example is converting:

StringBuilder sb = new StringBuilder(256); UTF8Encoding e = new UTF8Encoding(); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

into this:

var sb = new StringBuilder(256); var e = new UTF8Encoding(); var md5 = new MD5CryptoServiceProvider();

His point is that declaring the type as StringBuilder and then assigning it a new StringBuilder is redundant – StringBuilder appears twice on that line of code, before and after the assignment operator (=).  He makes an excellent point because the example using var is far more concise than the original example.  Furthermore, we should be more interested in what is being assigned than what we’re assigning too (at least on that line of code).  I find when I read the first block my eyes stick on the left-hand side of the assignment, but in the second example I read the right-hand side code instead.

As a side note. the excellent tool ReSharper 4.0 suggests using var for every local variable declaration by default.  It’s one of the first things I changed in ReSharper when I started using it.

You see, I don’t entirely agree with using var.  Using var is fine, but we aren’t converting C# into a dynamically typed language like Python or JavaScript – C# is still statically typed, using var just forces the compiler to figure out the type you’re using, usually based on the first value assigned to it.  In Python or JavaScript I could take sb from the above example and assign to it the UTF8Encoding() object reference on the next line, but C# won’t let you do that, the compiler will give you an error.

I don’t like the illusion, so where possible I will use var, but I’ll keep using types too when necessary for clarity.

Finding Common Values in Two Lists in C# Using LINQ

Finding common values in two lists is simple in .NET 3.5 thanks to LINQ!  I found this example here and here.

In practice here is what I did:

var currentOperatorFacilityIds = from fac in Facility.Facility.ListAllForOperator(this.OperatorId) select fac.ID; var deleteOperatorFacilityIds = from fac in Facility.Facility.ListAllForOperator(operatorId) select fac.ID; var commonFacilityIds = currentOperatorFacilityIds.Intersect(deleteOperatorFacilityIds);

Really simple!  I love LINQ.