Tuesday, February 26, 2008

Blog moved

This blog has moved over to the asp.net weblogs community.

I will be leaving this here for archival purposes, but I will not be updating it. My new blog can be found here.

Tuesday, January 15, 2008

SubSonic quick brain dump: Part 1

This post is just going to be a quick brain dump on SubSonic as I am in the mood. I am no way an expert at SubSonic, these code snippets are simply what I have implemented in a recent project to get the job done. There may be better ways to do the same thing.

In my last post I briefly described how to get started with SubSonic. So you have generated your classes for the DAL and now you want to get at that data. Well the project I have recently been working on has very simple requirements some of which I will cover here.

1. Simple binding to a GridView. You want to work with a simple collection of data. SubSonic creates these automatically for each of your tables within the db. This code snippet creates a data collection from the TelephoneList table, calls the Load method and finally binds it to a GridView called gvTelephoneList.

MyDAL.TblTelephoneListCollection telephoneList= new MyDAL.TblTelephoneListCollection();
telephoneList.Load();
gvTelephoneList.DataSource = telephoneList; gvTelephoneList.DataBind();

2. Adding a bit of a query to the mix.

TblTelephoneListCollection telephoneListPerson = new TblTelephoneListCollection()
.Where("Type", SubSonic.Comparison.Equals, "Person")
.OrderByAsc("Name");
telephoneListPerson.Load();
gvTelephoneListPerson.DataSource = telephoneListPerson ; gvTelephoneListPerson.DataBind();

3. Table fields as properties. Remember that each field within your database table is exposed as a property so you can iterate through the collection writing out the properties.

for (int i = 0; i < telephoneListPerson.Count; i++)
     {
      Console.WriteLine(telephoneListPerson[i].Name);
     }

4. Ok so I need a more complex query. Simply using what is called the Query Tool to define the query and pass that in to the FetchByQuery method. My table has fields of SubCategory and IsVisable and all I want are the TOP 5 Newsletters ordered by id DESC.

Query qryNewsletters = new Query(Tables.TblDocument);
qryNewsletters.QueryType = QueryType.Select;
qryNewsletters.AddWhere(TblDocument.Columns.SubCategory, "Newsletters");
qryNewsletters.AddWhere
(TblDocument.Columns.IsVisable, "1");
qryNewsletters.Top = "5";
qryNewsletters.ORDER_BY("id DESC");
dlLatestNewsletters.DataSource = TblDocument.FetchByQuery(qryNewsletters);
dlLatestNewsletters.DataBind();

That will do for now. I will cover simple updates and deletes in a later post.

Monday, December 31, 2007

Sub Sub

No this title does not refer to the band which then became Doves. No just recently I have been getting to grips with Subversion and Subsonic (I could have gone for Subtext on our latest project but went for BlogEngine.Net then we would have Sub Sub Sub)

Subversion
You may remember a post back in May when I started to use Source Safe 2005. Everything was working fine until one day it decided to rearrange my directory structure for a web application I was working on. I sorted it out and a while later it happened again. I decided to look around for an alternative to Source Safe and soon came across Subversion. My intention was to use Subversion for web applications while keeping all my other code in Source Safe for the time being. This was mainly because I really didn't want my code just sitting around outside any form of source control. Subversion just does what I want it to do; controlling my source without guessing what I want to do with it which is what Source Safe seemed to do. Setting up Subversion was a breeze, there is so much help on the web. My main sources where:-

Step-by-Step instructions for running a Subversion repository under Windows

This got me up and running with Subversion, the Subversion service and TortoiseSVN. I wanted to integrate with VS2005 so I installed AnkSVN. Everything was running swimmingly until I realised that the repository would be better on a different drive to where I put it (on the C drive stupidly, but this was to be a temp location until we get the final location upgraded to 2003 Server). So using this source, I successfully moved it. I have had it up and running now for about 4 months with no problems. Gradually I have slowly moved my source code over from SS2005 with an intention to ditch it in the new year.

Incase you need any more links for Subversion goodies, these are what I found to be really useful:-

Subversion Part 1 - Installing Subversion as your code repository for Windows users

Subversion & TortoiseSVN: Installed and started on Windows 2003 server and local machines

Software Carpentry - Version Control with Subversion

This last post contains links to video tutorials covering different aspects of Subversion.

Subsonic

The next Sub is Subsonic. An ORM which just works. I cannot believe how simple this was to set up. As this is something I will probably need in the future I will document the process here, who knows someone may find it useful.

1. Download it from CodePlex and run the exe.

2. Once it has installed create a link to the command in Visual Studio by going to Tools>>External Tools>>Add

Tools

This will generate the code for the DAL based on your settings in the web.config file.

3. Speaking of which here is a sample of the important parts of my web.config

<configSections>
<section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" requirePermission="false" />
<connectionStrings>
<add name="connectionname" connectionString="Data Source=servername; Database=databasename; Integrated Security=true;" />
</connectionStrings>
<SubSonicService defaultProvider="providername">
<providers>
<clear/>
<add name="providername" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="connectionname" generatedNamespace="yournamespace"/>
</providers>
</SubSonicService>
</configuration>

Just swap out connectionname, databasename, providername and yournamespace with your values.

4. Now the fun bit. Run Tools>>SubSonic. This will automatically generate the code for your ORM classes in a directory called Generated within your VS project. That is all you need to get up and running. I will post some simple code examples in the new year.


Here are some links I found useful:-


Getting started with SubSonic
For Web Sites - Using the BuildProvider
Video on getting started with SubSonic
Setting up SubSonic


Well enjoy the rest of the holiday and I will get back to blogging in the new year...erm well tomorrow then.

Thursday, October 11, 2007

Am I Agile?

There is a lot of discussion going around the blogosphere about the Agile methodology and ALT.Net. I was reading Billy McCafferty post titled 'Conveying Agile Processes Concisely to Clients' which gives a clear definition to clients (and a good introduction for me) about the Agile process and it dawned on me that I was more agile than I thought. Where I work we do not emphasise any methodology except get it done (gid??, giddy process??) and if a director wants it, get it done quick. As you can probably guess, this leaves no time to implement any TDD type of process and all testing is done at the end (although I would call this bug fixing). Some of the points mentioned in Bills post such as short iterations (they are always happy to see a working prototype) and prioritisation of features we are already doing. I am slowly trying to learn about agile and test first using NUnit, but it is slow going. I would get handed a small project and I think 'great, I can try out creating unit tests for this', alas I still find myself writing the code bypassing any unit tests then thinking 'oh I should of thrown in a test there...well it works'. Although I can retrospectively drop in tests as Derik Whittaker describes here. I don't want to be like that, I would like to test first and have the code covered, am I a bad developer? Well that brings me on to another thing, the ALT.Net conference in Texas. No I wasn't there, but I read a lot of the coverage from various blogs. Scott Hanselman mentioned that alt.net is a group that believe in:

1) Continuous Learning
2) Being Open to Open Source Solutions
3) Challenging the Status Quo
4) Good Software Practices
5) DRY (Don't Repeat Yourself)
6) Common Sense when possible

 

And I

1) All the time, I see myself as a continuous learner
2) I like using open source mainly because of the cost, if it cost me then I usually end up going without (I should emphasise that my latest place have been a bit more acceptable to the fact that a developer needs certain tools and they cost). But also I find they introduce new features faster.
3) Not much of a rebel, sorry
4) Again I am continuously learning about what I should be doing, here is a good guide to coding practices in c#
5) Yep
6) I think we all strive for this

So am I agile? Perhaps not, but I feel I am getting more agile every day.

When I linked from bad developer above to the alt.net conference I wasn't implying they are bad developers, no the link was less sinister; becoming a better developer means continually learning about development techniques and alt.net implies continuous learning. That's all.

Monday, September 03, 2007

Unit testing my web application

As I have recently started a new web application (actually it is going to grow in to our Intranet) I have decided to develop it along an agile path using unit tests. It has been a bit of a struggle at first, but I now have a basic framework. Writing the actual tests was never a problem as I have used NUnit before on a couple of windows based projects, this was however the first time with a web application. The easiest way I have found is by creating a web application instead of a web site in VS2005. By creating a web application, dll's are created at build time which NUnit will run against. Now that is up and running I wanted to use mock objects to basically mock session variables. I highly recommend Rhino Mocks as a mocking framework. You cannot however mock the session as it is sealed. After Googling for a while I came across this post by Phil Haack which uses an HttpSimulator. I thought I was almost there, but there was a problem with a map path interface having restricted access due to its protection level. Reading the comments lower on this page is a simple solution to comment out any reference to ConfigMapPath. So finally I can now unit test my session variables. Sorry for the long story, but it may help someone (myself included when I totally forget about it on my next web application project).

On a side note I finally took the plunge and am now using Windows Live Writer to update this blog. It is still in beta, but if you want to give it a try, go to the URL here.

Thursday, August 23, 2007

SketchPath, Draco.net and more

For anybody who regulary deal with XML and XPath in particular may be interested to try out Phil Fearon's SketchPath. It is a free utility which will easily analyse an xml document allowing you to examine the XPath query required to reach certain elements or attributes. I have only been playing arround with it for a very short time and I have come to the conclusion it could have saved me quite a lot of time on a recent project where I had to parse a large number of xml documents stripping out various bits n bobs. It is currently still in beta, but if this is anything to go by then the alpha release will be awesome.

On another note, Christopher Bennage has an excelent entry on his blog about getting up and running with Draco.Net. For the uninitiated, Draco.net is a continous integration (CI) environment that reports to have minimal set up troubles. To put it simple CI is basiclly an automated method which will check out your code from your code store (Subversion or SourceSafe for example), run any unit tests and run a build script. This is an area I am no where near knowledgable about, but am intending to look into in the near future.

For anybody getting in to WPF, there is a good getting started tutorial over at msdn.

Michèle Leroux Bustamante has a good post over on dasBlonde about getting a system setup for
.NET 3.0 and .NET 3.5 including links for downloads for VS2005.

On a more personal note, has anybody been getting tired dry eyes while working away on the computer? Apparently if you are staring at the centre of the screen then you are putting your eyes at unnecessary strain. In this blog entry the author describes how you should look at the monitor. I don't know if this is scientifically correct but if I work on my laptop which is below eye level and look down at the screen I do not feel as optically tired (is that correct???) as I do if I am looking at my flat screen.

Finally this blog entry from Sarah Ford has a quick shortcut key combination for VS2005 to help reformat code. It also works on web markup which is realy handy for me as I tend to have tags out of line and basically all over the place.

Thursday, August 09, 2007

New 22 inch widescreen

Well my old 19 inch CRT finally blew yesterday and I am now the proud owner of an iiyama 22inch widescreen (well in the office anyway). Takes a bit of getting used to, but the simple fact that there is virtually no reflection gives it the thumbs up straight away. Using VS 2005 with it is a dream.

The rewrite is going well. I have managed to implement code in different and better ways, learning new methods as I go along.

Speaking of VS 2005 here is a link to a blog entry that can help speed it up:-

Speeding up Visual Studio 2005

Reading through the transcript for Hanselminutes show 72 I came across a reference to a Justice Gray who is planning on reading and comprehending an IT related book each week. It beggers belief how he can find time to do that, but I guess it is all down to planning. I have a few books on my reading list, and I must admit it takes me quite a while to get through each one. Perhaps if I organise myself better I could wizz through these and try my best at keeping up to date with everything that is happening in the world of development.

After reading a few posts regarding mouseless computing I am trying my best to use my mouse less and rely on keyboard shortcuts instead. For one thing it may prolong the onset of RSI, but also I have found it quicker and easier to use keys for some things rather than reach out for the mouse. I do keep failing, especially when it comes to web browsing.

Happy programming