Sergio and the sigil

Video - IoC with StructureMap

Posted by Sergio on 2008-09-23

I finally got some time to import and upload the videos of September's Chicago ALT.NET meeting that happened almost 2 weeks ago.

In this first video jdn shows how DI and IoC containers can be used to add flexibility to an application design.

Video - ORM Discussion/Fishbowl

Posted by Sergio on 2008-09-23

After the IoC talk we had the monthly discussion. This time we tried the fishbowl format (or some approximation of that). The topic was ORMs.

Tip: export to CSV using ADO.NET

Posted by Sergio on 2008-09-17

A friend of mine was telling me about a bug he found in one of his applications caused by a simple lack of escaping quotes when producing CSV files. It immediately reminded me of an old trick in .NET.

If you really want, you can create CSV files using ADO.NET and OLE-DB (or ODBC.) I wouldn't necessarily recommend this approach but it is definitely one of those things that when you see for the first time you go "I never thought this could be done this way."

The idea is simple — open an OLE-DB connection using the Jet OLE-DB provider, create a table (which is really just a file) and insert rows in that table (or lines in that file.)

//create a temp directory for the CSV output
string tempFile = Path.GetTempFileName();
File.Delete(tempFile);
tempFile = Path.GetFileNameWithoutExtension(tempFile);
string dir = Path.Combine(Path.GetTempPath(),  tempFile );
Directory.CreateDirectory(dir);
string csvFile =  Path.Combine(dir, "data.csv");

string cnStr = 
    "Provider=Microsoft.Jet.OLEDB.4.0;" + 
    "Extended Properties='text;HDR=Yes;FMT=Delimited';" + 
    "Data Source=" + dir + ";";

using (var cn = new OleDbConnection(cnStr))
{
    cn.Open();

    //define the file layout (a.k.a. the table)
    var cmd = new OleDbCommand(
        "CREATE TABLE data.csv (CharColumn VARCHAR(30), IntColumn INT)", cn);
    cmd.ExecuteNonQuery();

    //start pumping data
    cmd = new OleDbCommand(
        "INSERT INTO data.csv (CharColumn, IntColumn) VALUES (?, ?)", cn);

    //in a more realistic example this part
    // would be inside some type of loop

    //1st record
    cmd.Parameters.AddWithValue("?", "11111111");
    cmd.Parameters.AddWithValue("?", 1234);
    cmd.ExecuteNonQuery();

    //2nd record
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("?", "22222\"22222");
    cmd.Parameters.AddWithValue("?", 6789);
    cmd.ExecuteNonQuery();

    //etc...
}

//read the csv formatted data
string csv = File.ReadAllText(csvFile);

//cleanup
Directory.Delete(dir, true);

Console.WriteLine("Result:");
Console.WriteLine("--------------------");
Console.WriteLine(csv);
Console.ReadLine();

The output of this will be as follows. Note the escaped double quote in the last line.

"CharColumn","IntColumn"
"11111111",1234
"22222""22222",6789

Of course, you can also read from a CSV file with simple SELECT statements against the file. You can let ADO.NET take care of all your typical CSV tasks.

Fabrice Marguerie wrote about this same topic a long time ago (check the comments and links too).

You may also want to check a more structured approach to the Export to CSV problem by way of the FileHelpers Library.

Chicago ALT.NET - IoC Containers

Posted by Sergio on 2008-09-03

Next Wednesday, September 10th, is the next meeting of the Chicago ALT.NET user group. This time IoC containers will be the topic du jour.

Expect to see an introduction to this technology and learn why IoC isn't just overhead. Don't expect to leave with just another introduction to some technique that you'll never want to use.

Inversion of Control for the masses

6:00 pm
Pizza and networking time

6:30 pm
John Nuechterlein (a.k.a. jdn) puts Inversion of Control (IoC) containers under the spotlight. If you have been noticing the increasing buzz around this technology but never had a chance to see what it is and how it can be used, this is your chance to see:

  • An overview of what IoC is, using StructureMap as the example
  • Why one might want to use IoC
  • How one can implement IoC
  • When one might not use IoC

7:45 pm
You may want to stick around after the presentation portion of the meeting and take part in our monthly open discussion. The topic is never arranged in advance but it's common that it reflects the content of the presentation.

Silverlight at the LCNUG

Posted by Sergio on 2008-08-29

Last night I attended another LCNUG meeting. I think it's great that we have this new group forming in the northern suburbs of Chicago. It's definitely not easy for us suburbanites to attend CNUG's meeting in the West 'burbs or even the Chicago ALT.NET meetings downtown. It's special convenient for me because it's only 10 minutes from home.

This time the presentation was about Silverlight. Tim Stall gave us a very nice and engaging rundown of this new technology — it was not your off-the-mill introduction talk. The questions from the audience were very interesting and Tim was very honest about the strengths and shortcomings of the tool. It was especially interesting to discuss ways to bring SilverLight into your organization.