James Hollingworth’s Adventures in Code

Icon

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 ,

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 ,

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

my bookmarks