Friday, 9 November 2007

Important Tips for budding Software Developers & Computer Science Students

Some of the most important things you should learn before you get out into the big bad world of software development:
  1. Learn about money and accounting. Debit & Credits, Balance Sheets and Profit & Loss Statements. Pretty much everything you do in the real world (unless you are a low-level system or driver developer) deals with business concepts - so you should learn it as soon as possible. It will give you a head start against all the other technically proficient programmers. A combined accounting/IT degree is ideal.
  2. Learn one of the things they don't teach you at uni - to talk to customers. Just because you can code in your sleep doesn't make you a good software developer. I have seen this time and time again. The "soft skills" are what set you apart from the other job candidates. Make sure this comes through in your job interview. Confidence will get your 50% of the way to setting you apart from the pack.

Wednesday, 7 November 2007

The power of Lambda Functions

(Tip thanks to my mate Eric Phan):

Lambda functions are essentially anonymous functions (in-line functions) that you can write instead of creating a new method to perform this once off operation.

A good example is filtering a list

I have a list of customers and I want to know who is more than 25 years old and female.
Traditionally (using a filtering method):


   1:  static void Main(string[] args)

   2:  {

   3:     List<Customer> customers = GetAListOfCustomer();

   4:     List<Customer> matureFemaleCustomers = FindMatureFemaleCustomers(customers);

   5:  } 

   6:   

   7:  // Find female customers over 25 years old 

   8:  public List<Customer> FindMatureFemaleCustomers(List<Customer> customers)

   9:  {

  10:     List<Customer> matureFemaleCustomers =new List<Customer>();

  11:     foreach (Customer c in customers)

  12:     {

  13:        if (c.Age > 25 && c.Gender == Gender.Female)

  14:        {

  15:           matureFemaleCustomers.Add(c);

  16:        }

  17:     }

  18:     return matureFemaleCustomers;

  19:  } 


In C# 2.0 with generics:



   1:  static void Main(string[] args){ List<customer> customers = GetAListOfCustomer();

   2:  List<customer> matureFemaleCustomers = customers.FindAll( delegate(Customer c) { return c.Age > 25 && c.Gender == Gender.Female; } );}



In C# 3.0 with Lambda Functions



   1:  static void Main(string[] args) 

   2:  { List customers = GetAListOfCustomer(); 

   3:    List matureFemaleCustomers = customers.Where(c => c.Age > 25 && c.Gender == Gender.Female);

   4:  } 





Simple and elegant!

Dynamic Queries in LINQ - and LINQPad

If you are creating a keyword-style search in LINQ, you can use this wrapper to make your life easier
http://www.albahari.com/expressions/

You can also use LINQPad to test out your LINQ to SQL queries: http://www.albahari.com/linqpad.html. One of the drawbacks I found is that it doesn't currently support views.

Labelling VS Branching in TFS - Source Control Best Practices

TFS provides both branching and labelling. The question is: when should I use branching and when should I use labelling?.
As a general rule:

If you just want to mark a set of code as a milestone, then label. Otherwise use branching - you never know when you will need to patch your code!

See http://blogs.msdn.com/team_foundation/archive/2005/02/23/379179.aspx
for a more detailed explanation. Buck Hodges says:


"For anything that you don't want to service or patch, labeling is sufficient. For a release that you do want to service or patch, branching is the way to go. By having a separate branch, you'll have a place to check in your changes and maintain the code while continuing development on the main branch. Using merge, you can move changes between branches."

Issues with LINQ Order By Queries when using Distinct

Encountered an unusual issue today whereby the .Distinct() extension method call on a LINQ query will actually mean that the output SQL will NOT have an order by clause in it. Distinct basically forces an ignore on any orderby statements that you have:

- This will NOT generate the Order By


   1:  var test = (from c in db.CreditRequests

   2:  where c.DateRejected != null

   3:  orderby c.DateRejected

   4:  select c).Distinct();



- This WILL generate the Order By


   1:  var test = (from c in db.CreditRequests

   2:  where c.DateRejected != null

   3:  orderby c.DateRejected

   4:  select c);





A way around this is to use Lamda Expressions



   1:  var test = (from c in db.CreditRequests

   2:                          where c.DateRejected != null

   3:                          select c).Distinct().OrderBy(d => d.DateRejected).OrderBy(d => d.CreditRequestID);

How to concatenate strings from mutliple rows into one in SQL Server 2000 or 2005

"How to concatenate (aka join) strings from mutliple rows into one in SQL Server 2000 or 2005". This is a very common question, so here is a simple answer:


   1:   

   2:  CREATE TABLE Colors 

   3:  ( 

   4:  Color VARCHAR(32) 

   5:  ) 

   6:  GO 

   7:  SET NOCOUNT ON 

   8:  INSERT Colors SELECT 'red' 

   9:  INSERT Colors SELECT 'orange' 

  10:  INSERT Colors SELECT 'blue' 

  11:  INSERT Colors SELECT 'green' 

  12:  GO 

  13:  DECLARE @colors VARCHAR(1024) 

  14:  SELECT 

  15:  @colors = COALESCE(@colors + ',', '') + Color 

  16:  FROM 

  17:  Colors 

  18:  SELECT Colors = @colors 

  19:  GO 

  20:  DROP TABLE Colors 

  21:  GO


Thanks to http://databases.aspfaq.com/general/how-do-i-concatenate-strings-from-a-column-into-a-single-row.html

Monday, 5 November 2007

Keeping all your connection strings in the Web.Config when using library classes (aka simplified deployment and configuration model)

One of the problems with using multiple projects e.g. DataAccess, BusinessLogic, etc classes is that the Data Access Classes need to be able to read from the Web.Config in the Web.UI layer. However, we sometimes cannot Reference the WebUI layer directly because (apart from being a bad design), it would create a circular reference.

There are a few possible solutions to this. One is to use the VS 2008 Client Application services as per http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/06/03/9463.aspx
However, this is dependent upon the service name being consistent. This is a problem on a local Cassini development machine which can change port numbers. If you want a low maintenance approach to reading connection strings from your associated web project:
  1. Add A Reference to System.Web in your library project. This will give you access to the WebConfigurationManager class.
  2. Go to your data access project e.g. "Data", and go to the settings section.
  3. Create your connection string in the settings section.
  4. Add the following code so that your values are set when the settings are initialised.




   1:  using System.Web.Configuration;

   2:   

   3:  namespace Pfizer.CreditReturns.Data.Properties {

   4:      

   5:      

   6:      // This class allows you to handle specific events on the settings class:

   7:      //  The SettingChanging event is raised before a setting's value is changed.

   8:      //  The PropertyChanged event is raised after a setting's value is changed.

   9:      //  The SettingsLoaded event is raised after the setting values are loaded.

  10:      //  The SettingsSaving event is raised before the setting values are saved.

  11:      public sealed partial class Settings {

  12:          

  13:          public Settings() {

  14:   

  15:              //

  16:              this["CreditReturnsConnectionString"] = WebConfigurationManager.ConnectionStrings["PfizerCreditReturnsConnectionString"].ConnectionString;

  17:              this["DWHS_CreditReturnsConnectionString"] = WebConfigurationManager.ConnectionStrings["PfizerCreditReturnsDataWarehouseConnectionString"].ConnectionString;

  18:   

  19:              //System.Console.WriteLine(this.CreditReturnsConnectionString);

  20:              // // To add event handlers for saving and changing settings, uncomment the lines below:

  21:              //

  22:              // this.SettingChanging += this.SettingChangingEventHandler;

  23:              //

  24:              // this.SettingsSaving += this.SettingsSavingEventHandler;

  25:              //

  26:                          

  27:   

  28:          }

  29:          

  30:          private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) {

  31:              // Add code to handle the SettingChangingEvent event here.

  32:          }

  33:          

  34:          private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) {

  35:              // Add code to handle the SettingsSaving event here.

  36:          }


This example will load your configuration settings from the Web.Config at runtime.

Databinding in Page_Load VS LINQ Data Source Selecting Event

While it is nice to have all your databinding code in Page_Load (e.g. in a LoadData() method), it is preferable to use the DataSource Selecting Event. e.g.


   1:  linqDSProducts_Selecting(object sender, LinqDataSourceSelectEventArgs e)


This is because you retain full support for Data source pagination and sorting - which equals less code (declarative or programmatic). For more details on using the Selecting event with LINQ datasources, see
http://www.visualbuilder.com/viewdetail.php?type=1&id=1591&group_id=11