Wednesday, June 17, 2009 #

Announcing the CFlat compiler

While I have been backpacking through Europe my urge to code has not been completely ignored. On trains and planes and buses I have been working my way through “The Dragon Book” and “The Definitive ANTLR Reference”.

As of a few minutes ago I am probably too proud of the fact that the command line:

cflat.exe SimpleTest.cflat -o SimpleTest.dll

Compiles this:

public class SimpleTest
{
    public int GetValue()
    {
        return 1;
    }

    public int Main()
    {
	int a = 2;
	if(true){
		return a + GetValue();
	}
	return a;
    }
}

into this:

SimpleTests

There is lots more to do but I finally feel like after much experimentation I am on track to having my own compiler for a very small subset of C#.

I built a compiler once before for possibly my favorite university class: Programming Language Implementation. I became interested in compilers again after playing around with the first Oslo release and the combination of M/IntelliPad.

Of course this compiler will never be of any use except as a learning tool for me.  Currently I am using ANTLR for parsing/lexing and Reflection.Emit to target the CLR.  Once I am happy with the feature set of my C# subset I am planning to look into other options for parsing including MGrammar and F#. Before that I am going to need to add at least one looping construct :-).

All the source and test are available in my github repository in case anybody wants to follow along.

posted @ Wednesday, June 17, 2009 3:59 AM | Feedback (1)

Monday, January 26, 2009 #

Going to Europe

I am heading off to Europe for the next six months.  If you would like to follow along I'll be putting up some posts and photos as we go on my Europe Blog.  I am taking a laptop and have a project in mind for any spare time I have.  Hopefully that means there will be some technical posts for this blog as well.

posted @ Monday, January 26, 2009 12:05 AM | Feedback (0)

Monday, May 19, 2008 #

What's your Circle of Interest?

I've been pondering how to write this up for over a week now.  It started with Paul Stovell and I was been tagged by Jonas Folleso.

The idea is to visually divide technologies into Core, Non-Core and I don't care.  Its probably something I should have done before now as I tend to want to know everything about everything which isn't very feasible. 

 

MyCircleOfInterest

Andrew Core: Some of these are fun some are for work. These are things I not only care about but work with regularly and have a reasonable proficiency in.

Andrew Non-Core: These are things I care about but don't use all the time/haven't used enough.  Some of them I would like to push into my core circle when I find the time.

I don't care: There is really no "development" stuff in here. I think my java days are probably over but I still "care" a little.  There are some notably missing technologies like WCF and WF.  I didn't feel they belonged here but they also aren't things I care about (yet?).  Maybe I should have added fourth ring of "neutral" technologies?  I'll keep my life simple by omitting these things for now.

I'd love to see a circle of interest from:

I've spent a bit of time chatting to both the Chrises about some of the things in my circles of interest over the last few weeks.

posted @ Monday, May 19, 2008 8:13 PM | Feedback (1)

Monday, May 05, 2008 #

CodeCampOz 2008

theatre_pan_medium

So there's nothing like a code camp to make me feel energised about what I do!  I remember feeling roughly the same way after CodeCampOz 2007.  Myself and a couple of hundred other programmers spent last weekend in Wagga Wagga talking about all things geeky.

This year I drove up with Jonas Folles who did an awesome talk demonstrating an interactive video application with Silverlight.  My other travelling companion was Ian Thomas who regulars on the AusDotNet mailing list will be familiar with.  Thanks to both of them for making the six hours there and back not only bearable but interesting and enjoyable.

I really enjoyed all the sessions but the standouts for me where:

I also got a heap out of Jonas's silverlight talk and Scott Berry's WPF business applications talk from looking at the way they use blend.  I've been doing some WPF bits and pieces over the past 12 months but have not been using Blend anywhere near as much as I should.  Although as Scott pointed out maybe its good to go through enough XAML pain to really know what is going on.  image

My free copy of Blend 2 is coming in 18 days!

As is always the way with these things many of the most interesting content was to be had between sessions and at the pub.  It was also great to put a face to many names that I've seen in blog or on mailing lists.

While I'm feeling inspired here is my list of tech goals for the next few months:

  • Get back to contributing to CodeCampServer
  • Get involved in a side project that isn't web based (probably WPF?)
  • Get a demo together for the Heros Happen demo comp.
  • Actually post something interesting on this blog every now and again.
  • Get my MCTS (Web)

Thanks to Greg Keogh for permission to use the photo at the top of this entry.  The theatre looks a little bear so my guess is the photo might have been from early Sunday morning.

posted @ Monday, May 05, 2008 5:54 PM | Feedback (1)

Monday, November 26, 2007 #

DataReaders and Stored Procs - The List

Me: I'm going to write some example code to properly compare table adapters with my other options.

Marc: But you already hate table adapters how are you going to be objective?

...a day later...

Me: Okay I'll start with stored procedures and data readers, nothing can be less fun than that.

So I proceeded to have lots of fun implementing my web site data layer using data readers and stored procedures.

To begin with I wanted to fill my GridView with some data. This is something I'm sure we've all done so many times. When I was first working with ASP.NET I begin with Scott Mitchell's data tutorials. These tutorial use dynamic SQL.  After lots of advice and help from the guys on the SQLDownUnder list I have eliminated any dynamic SQL from my stored procedure.  I think the result is nice and reasonably maintainable. This stored procedure has the following features that I find are usually requested in the systems I build:

  1. Paging
  2. Sorting
  3. Searching

To do paging two queries are required one that returns the page of results and one that gets the total number of results.  I have chosen to make the total rows an output parameter for my stored procedure.  This has two advantages I can see.  The first advantage is that the where clause is repeated across both queries and this way they are in the same place for maintenance.  The second plus is that it allows a single database round trip to get the list.

    1 -- Author:        Andrew Browne

    2 -- Create date: 12 November 2007

    3 -- Description:    Retrieves a product list one

    4 --             page at a time

    5 -- =============================================

    6 CREATE PROCEDURE [BrowniesBikes].[GetProductList]

    7     @SearchString NVARCHAR(50) = '',

    8     @StartRow INT = 1,

    9     @EndRow INT = 10,

   10     @SortColumn NVARCHAR(50) = '',

   11     @TotalProducts INT OUTPUT

   12 AS

   13 BEGIN

   14     SET NOCOUNT ON;

   15 

   16     DECLARE @LIKE_SEARCH as nvarchar(50)

   17     SET @LIKE_SEARCH = '%' + lower(@SearchString) + '%'

   18 

   19     SELECT @TotalProducts = COUNT(*)

   20         FROM Production.Product

   21         WHERE(lower(Name) LIKE @LIKE_SEARCH

   22             OR lower(ProductNumber) LIKE @LIKE_SEARCH)

   23 

   24     SELECT RowRank, ProductID, Name, ProductNumber

   25         FROM

   26         (SELECT ProductID,

   27             Name,

   28             ProductNumber,

   29             ROW_NUMBER()

   30             OVER(

   31                 ORDER BY

   32                     CASE WHEN

   33                         @SortColumn = 'ProductNumber'

   34                     THEN

   35                         ProductNumber

   36                     ELSE

   37                         Name

   38                     END

   39                 )

   40         AS RowRank FROM Production.Product

   41         WHERE(lower(Name) LIKE @LIKE_SEARCH OR

   42             lower(ProductNumber) LIKE @LIKE_SEARCH)

   43         ) AS ProductsWithRowNumber

   44         WHERE RowRank > @StartRow

   45             AND RowRank <= @EndRow

   46 END

This stored procedure will only work with SQL 2005 because of the over clause.

The code to retrieve this was fairly straight forward if you want all the details grab the source code.  The main pain point that I found was this routine for manually mapping the results from the reader:

   78         private static Product

   79             GetProductSummaryFromReader(

   80                 IDataReader reader

   81             )

   82         {

   83             Product product = new Product();

   84             product.ProductID =

   85                 Convert.ToInt32(reader["ProductId"]);

   86             product.Name =

   87                 Convert.ToString(reader["Name"]);

   88             product.ProductNumber =

   89                 Convert.ToString(

   90                     reader["ProductNumber"]

   91                     );

   92             return product;

   93         }

This code feels a little fragile with all the convert calls in there but at least it's all in one place.

posted @ Monday, November 26, 2007 10:25 AM | Feedback (0)

Saturday, January 05, 2008 #

In Search of the Ultimate Data Access Layer - Summary

This post will serve as a summary of my posts related to my not very scientific analysis of data access layers.  These posts won't be tutorials but more pointers to things I liked/didn't like about each method as well as the main bits of interesting code. 

For more detail grab the code from: http://code.google.com/p/browniesbikes/.  Along with the code is also my NUnit tests.

DataReaders and Stored Procs:

  • Listing Data
  • Retrieving a single item - implemented post coming soon
  • Inserting Data - implemented post coming soon
  • Updating Data - implemented post coming soon

TableAdapters:

  • Listing Data
  • Retrieving a single item - implemented post coming soon
  • Inserting Data - implemented post coming soon
  • Updating Data - implemented post coming soon

NHibernate:

  • Implemented - posts coming soon

LinqToSQL

  • Implemented - posts coming soon

SubSonic

  • partially implemented

Data Access Application Block (Enterprise Library)

  • meaning to implement it someday.

posted @ Saturday, January 05, 2008 8:43 PM | Feedback (0)

TableAdapters - The List

Most of my post on retrieving my Product list focused on the Stored Procedure.  When it came time to implement the same list with a TableAdapter all that work was already done for me.  My steps became:

  1. New dataset.
  2. Drag my stored procedure from server explorer.
  3. Rename the table and tableadapter
  4. And there we have it:

image

I then double clicked on the designer and edited the codebehind file to allow my DataRows to implement my IProduct interface:

    1 using System;

    2 using BrowniesBikes.DTO;

    3 

    4 namespace BrowniesBikes.Data.TableAdapter {

    5 

    6 

    7     partial class ProductDS

    8     {

    9         partial class ProductListRow : IProduct

   10         {

   11             #region IProduct Members

   12 

   13             public int? PhotoID

   14             {

   15                 get { throw new System.NotImplementedException(); }

   16                 set { throw new System.NotImplementedException(); }

   17             }

   18 

   19             ...etc...

   20             #endregion

   21         }

   22     }

   23 }

My DAL class ended up with very little code:

   18         public IEnumerable<IProduct> GetProductList(

   19             string searchString,

   20             string sortExpression,

   21             int startRow,

   22             int maxRows)

   23         {

   24             int endRow = startRow + maxRows;

   25             ProductListTableAdapter productListTableAdapter =

   26                 new ProductListTableAdapter();

   27 

   28             int? totalProducts = 0;

   29 

   30             ProductDS.ProductListDataTable listDataTable =

   31                 productListTableAdapter.GetData(

   32                 searchString,

   33                 startRow,

   34                 endRow,

   35                 sortExpression,

   36                 ref totalProducts);

   37 

   38             foreach (ProductDS.ProductListRow row in

   39                 listDataTable.Rows)

   40             {

   41                 yield return row;

   42             }

   43         }

Deceptively easy. Keep an eye out for my post on inserting and updating as well as TableAdapter maintenance. There are some major pain points coming your way.

posted @ Saturday, January 05, 2008 8:19 PM | Feedback (0)

Getting formatted code onto the web

This post is mostly for me as I recently switched machines and forgot the steps and software needed to get nicely formatted code into my blog:

  1. Download CopySourceAsHtml (as recommended by Hanselman)
  2. Extract the files from the MSI using Less MSIérables as the installer doesn't work with VS 2008.
  3. Copy the files to the VS 2008 plugin directory as per these instructions.
  4. Open Visual Studio Enable the plugin from Tools > Add-in Manager
  5. Select text and choose "Copy AS HTML" from the context menu.
  6. Right click in windows live writer and select "Paste Special". Choose "Keep Formatting" from the dialog.
  7. 1 Console.WriteLine("Beautifully formatted Code!");

    I almost didn't bother trying this tool as I didn't like the way Visual Studio put boxes around blocks of SQL code.  I forgot of course that these boxes couldn't be carried over the HTML even if I'd wanted them too.  The SQL looks just as nice as it does in management studio:

        1 -- Comment: Comment goes here

        2 SELECT * from Products

    The only problem I am finding is that code quite quickly gets too wide and wraps messing up the line numbers. That is probably more to do with my blog template.

    Update: I have just come across a neat way of solving the wrapping problem. 

    1. Uncheck wrap words in the copy as html dialog.
    2. Wrap my code in overflow divs:
    <DIV style="OVERFLOW: auto">

    I can now have nice scrollbars:

       11 [Test]
       12         public void ShouldRetrieveSpeakerByEmail()
       13         {
       14             Conference anConference = new Conference("tea party", "");
       15             using (ISession session = getSession())
       16             {
       17                 session.SaveOrUpdate(anConference);
       18                 session.Flush();
       19             }
       20             string email = "brownie@brownie.com.au";
       21             Speaker speaker =
       22                 new Speaker("Andrew", "Browne", "http://blog.brownie.com.au", "the comment", anConference,
       23                             email, "http://blog.brownie.com.au/avatar.jpg", "Info about how important I am to go here.","password", "salt");
       24 
       25             ISpeakerRepository repository = new SpeakerRepository(_sessionBuilder);
       26             repository.Save(speaker);
       27 
       28             Speaker rehydratedSpeaker = null;
       29             //get Attendee back from database to ensure it was saved correctly
       30             using (ISession session = getSession())
       31             {
       32                 rehydratedSpeaker = repository.GetSpeakerByEmail(email);
       33 
       34                 Assert.That(rehydratedSpeaker != null);
       35                 Assert.That(rehydratedSpeaker.Contact.FirstName, Is.EqualTo("Andrew"));
       36                 Assert.That(rehydratedSpeaker.Website, Is.EqualTo("http://blog.brownie.com.au"));
       37                 Assert.That(rehydratedSpeaker.Comment, Is.EqualTo("the comment"));
       38                 Assert.That(rehydratedSpeaker.Conference, Is.EqualTo(anConference));
       39             }
       40         }

    posted @ Saturday, January 05, 2008 7:54 PM | Feedback (0)

    Sunday, November 18, 2007 #

    First Post!

    This blog is primarily the result of the idea that "there should be a better way". I think most of us are constantly looking for faster/easier/more maintainable ways of doing things.

    In my case I am currently working in ASP.NET on public facing websites. After many conversations with colleagues about datasets/tableadapters vs everything else I decided that all the talk in the world wasn't going to get a well considered result. Over the last few months I have been exploring different .NET data access strategies.

    My method of comparison so far has been to use each of the technologies in the data layer for my very simple List/View/Add/Edit sample app. The example application is based on the AdventureWorks Cycles and I've named it Brownie's Bikes.  The example is probably a bit simple at the moment but we'll fix that over time.

    My work is up at http://code.google.com/p/browniesbikes/

    My plan is for my first few posts to compare the different strategies and technologies I have used to implement the data access in Brownie's Bikes.  While doing this work I have also become very excited by better ways of developing the UI and architecture for websites so expect some posts about these topics too.

    Disclaimer: I only know as much as I know right now. I am very interested in better ways of doing things.  If you don't like my ideas I'd love to try yours. If you have a better idea or I am wrong let me know in an email/comment/link to your blog.  I have just finished the fourth data layer for my sample app and am planning more. I'm very open to trying any idea that looks reasonable.

    posted @ Sunday, November 18, 2007 4:59 PM | Feedback (0)