James Hollingworth’s Adventures in Code

Icon

Muney: Financial Management for Students

Over the past year, I have spent quite a long time on my dissertation. I’m pretty proud of it and I have had quite a few positive comments from various lecturers (I’ve even been asked to write a paper on it for a journal). I’ve decided to write a few articles about my work, hopefully it will hope someone out. If your interested in it, my final report can be found here.

My project was a personal financial management application for students (the actual title was FAST: Financial Analysis for STudents although my final application was called Muney). Essentially I was having problems with my finances a while ago, being the good programmer that I am, I had a look at what software was available. To be honest, from a students perspective, Microsoft Money & Quicken are pretty terrible. Users are required to have a significant amount of financial knowledge to use them effectively. In their defense, these app’s aren’t really aimed at the student demographic.

This was obviously a known problem since I found a few web applications such as wesabe & buxfer, which were developed to solve just this problem. Although these applications are much more student friendly they were really basic, not offering solutions for tasks students are commonly pretty poor at performing (e.g. bill management, budgeting)

So based on this, I decided to develop a financial application which automates important monetary tasks and does so in a way which is easy for a student, with no prior financial knowledge, to understand and use. The application was written using C# & Castle Project’s Monorail framework a screen shot is shown below

The application had a variety of features including

  • Bulk importing financial information via an OFX parser (currently open sourcing the parser I had to write to achieve this)
  • Automatically renaming and categorizng transactions
  • Bill managment (including automatically discovering new bills and recognizing transactions as payments for bills via clustering techniques)
  • Automatic budgeting (including time series forecasting to predict a users expenditure)

Since i covered quite a few topics devloping these features, i’m going to split this blog into a series of articles. The application can be split up into three tasks, organizing, managing and planning a users finances:

Here are the articles discussing these tasks

  • Organization
  • Managment (coming soon…)
  • Planning (coming soon…)

Filed under: .net, c#, castleproject, dissertation, monorail, muney, ofx , , , , , , , ,

Subsonic like NHibernate Query Generator button in Visual Studio

One thing I love about SubSonic, in fact the reason i switched, was how awsome the querying is. I don’t know why but something like new UserCollection().Where(“UserID”, id).Where(“Password”, password) just does it for me. Going from that back to either HQL or ICriterion was a rather painful experience. A very clever guy called Oren, has come up with a solution to this called NHibernate Query Generator (NQG). You can do some rather nifty things like FindOne(WHERE.User.Name == “James” && WHERE.User.Password == “NotGoingToTellYou”). Many (including Oren) belive that you shouldnt have strings in your code and thus the code to create all these cool queries is autogenerated.

The problem I found was I had to constantly change back & forth between the command line & VS to update the code. To solve this i borrowed a trick from SubSonic. I have created a little button in VS (see below) which will run the query complier for you.

Nhibernate Button

To do this, firstly download the latest version of NQG, then copy the exe file NHQG.exe to an safe location e.g. “c:\Program Files\NHibernate\”. Next you need to open up visual studio, and click Tools -> External Tools and then click add. Give whatever name you want e.g. NHibernate, and then for the command the location of NHQG.exe, in my case “c:\Program Files\NHibernate\NHQG.exe”. In the arguments field, you have a few choices:

  • /Lang: language used, either cs | vb
  • /InputFilePattern: location of either Nhibernate mapping file or the dll (if your using Castle Projects ActiveRecord)
  • /OutputDirectory: Directory you want the files to go to e.g. ./Models/Queries
  • /BaseNamespace: Base namspace for queries, e.g. Priority.Queries

so a complete example would look like “/Lang:cs /InputFilePattern:bin/Priority.dll /OutputDirectory:Models/Queries /BaseNamespace:Priority.Queries”

For Inital directory you just add $(ProjectDir) which sets the starting location to be the root of the project directory. Finally, make sure that it is the first in the menu (it will become clear why in a second).

externaltools2.png

Once you click ok, you will now be able to run the application by clicking on Tools -> NHibernate. To create a button for this, you need to click Tools -> Customize -> Toolbars & then click new, a new menu should appear. Next you need to switch back to Commands, Select Tools and the scroll down until you see all the External Command x’s. Drag External Command 1 onto your new toolbar and then click close. It should change the text from External Command 1 to NHibernate (or whatever name you gave it) and your done!

Hope this helps someone!

Right now that we have

Filed under: NHibernate, c#, castleproject, visual studio, visual studio 2008 , , , , ,

Get the currency symbol for the ISO 4217 Currency Code (C#)

Had a bit of a problem today. After importing a bunch of transactions I needed to display the corresponding currency symbol. The problem was when you import OFX transactions, the currency is in the ISO 4217 three letter format. Basically the little bit of LINQ below will search through all the culture infos until it finds one with the correct ISO Currency symbol. Its little things like this that make me love linq!

RegionInfo regionInfo = (from c in CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures)
let r = new RegionInfo(c.LCID)
where r.ISOCurrencySymbol == "GBP"
select r).First();

Hope this is helpful to someone!

Filed under: .net, c#, linq, ofx ,

NVelocity: Get Current Position In ForEach Loop

Just a reminder for myself, To the get the current iteration number in a foreach loop $velocityCount

Filed under: c#, monorail, nvelocity

Problems with the Linq2Sql

I have just been reading Scott Guthrie’s post on the new ASP.Net MVC framework. Firstly, for the most part I am really amazed with what Microsoft have come up with and I am really looking forward to having a play with it. I am worried though about the methods Scott uses for creating models for data access. In the examples he creates a Linq2Sql model for data access and then use the the database context class to encapsulate all the business logic. Anyone see any problems with this? Say you have a User table in the database, Linq2Sql will create a user object which you can perform all the basic CRUD operations. Say you want to add a method to check if a given user has valid username & password (basic business logic), which of these would you think is logical?

DataBaseContext db = new DataBaseContext();
db.IsUserValid(username, password);

or

User.IsValid(username, password);

IMHO the second is far better since firstly when writing code you don’t need to be aware of other objects (i.e. database context) to be access the business logic of that object. Also having all the business logic encapsulated in the actual object enforces code separation so it is easier to maintain since there will only ever be one file which contains all the code relating to the object. Also

So what should you do instead of encapsulating the business logic in the context? Well there are 2 methods which would allow you to encapsulate all your business logic, the first being using partial classes. Using the User example again, say Linq2Sql has created a User object, you would add another file in the Model directory and create a partial class which you can then include all business logic, as shown below:


partial class User
{

public static bool IsValidLogin(string userName, string password)
{

//Some business logic
return true;

}

}

This method will allow separation of code and you don’t need any knowledge of extra classes to use it. Unfortunately this method is soooo .Net 2.0, so for everyone who is needing an excuse to use the new extension methods, here it is:


public static class UserBusinessLogic
{

public static bool IsValidLogin(this User user, string userName, string password)
{

//Some business logic

return true;

}

}

Both methods work fine and will enforce code separation. Its a shame that decided on having a single .dbml file for the models and Context file for the business logic. I would have thought having an individual class for each table in the db (with a partial class containing all auto generated code) would be more logical and scalable.

There are fundamental flaws with this like how do you apply rules to properties (e.g. ensuring that a password is > 7 characters)? Currently this sort of business logic is enforced at the database end which is a bit crap since I thought the whole point of Linq2Sql or any ORM was so the developer doesn’t have to deal with the database!

While I am critical of Linq2Sql, it is still early days and I’m sure as time passes they will rectify the problems highlighted here. Until then, I hope the methods shown here will help you write maintainable code!

kick it on DotNetKicks.com

Filed under: .net, c#, linq ,

C# PostCode GeoCoding

Part of my project needs me to get the coordinates for a given post code. I found FreeThePost.org which allow you to perform geocoding querys pretty easily by doing something like http://www.freethepostcode.org/geocode?postcode=rg20eu. Decided to whip up a quick C# object to do it. Its all pretty simple, do something like:

PostCodeGeoEncoder encoder = new PostCodeGeoEncoder( postCode );
Coordinates coords = encoder.GetLocation();

The assembly for it is here, i hope this saves someone a couple of minutes of hassle!

Filed under: .net, c#, geocoding, postcode

LINQ trying to be clever

So have been playing around with LINQ today which I have been pretty excited about. I loved all the ORM’yness in Ruby but hated the floating point arithmetic so I can now have a fast language with simple database access! Anyway, was designing the db the ruby way, i.e. pluralizing all the table names, e.g. Car becomes Cars. I built my LINQ to SQL class, and then get down to some ORM fun. I look through the list of tables created, and find that LINQ pluralizes the databases automatically which is pretty clever, unfortunately I dont think they have fixed all the bugs. They seem to have missed out the “ple” plural since they have rename tblPeople to tblPeoples.

Linq trying to be clever

Filed under: c#, linq

C# OFX Importer

I have put the code up on google code

So i didn’t have much to do yesterday so I wrote an OFX importer. I used Jason’s sharpCash as a starting point but ended up doing my own implementation (always easier to rewrite than to understand someone else’s code!). Its pretty compliant to the main bulk of the OFX specification, although it only supports Credit Card and Bank account types at the moment.

It was written in C# and I have compiled it to a class library, the dll for it is here. Basically because this is for my dissertation, I don’t really know where I stand with releasing the code, I will speak to my project supervisor, worst case scenario I will release it early next year but if anyone wants the source, just email me.

To use it, you simply do:

    OFX ofxFile = new OFX(filePath);

Hope this saves someone a couple of days of trouble!

**Update **

Sorry, hadn’t realized  that the link had expired, I have put it in my university web space hosting which you can get here. Will be releasing the code as soon as possible but may be a month or 2 before thats possible. I have one request to make for anyone using, could you just drop me an email with the names of banks you are using, and if feeling very generous any performance metric’s you happen to have collated. I need to show this component works properly on a range of banks but can only personally get hold of OFX documents for a couple of banks. Any help would be greatly appreciated.

Filed under: c#, dissertation, ofx

Importing OFX troubles

For part of my project, i need to do import data in the OFX format. Trouble is that no one has really done much, apart from one library (http://www.nsoftware.com/ibiz/ofx/) but from what i heard, it wasn’t very good. I found this article, http://www.west-wind.com/WebLog/posts/10491.aspx, where some guy seems to be having similar troubles. Had a look around and hes started a project called sharpCash where he has done an implementation of OFX import. Spent the day poking around and its seems to be a good starting point. Currently working on my own implementation, will keep posted on any discoveries i have!

Filed under: c#, dissertation, ofx

my bookmarks