Browse > Home / Archive by category 'C#'

| Subscribe via RSS

Super Lambda Bananas

June 29th, 2008 | No Comments | Posted in C#, LINQ

302279198_06564a7141_m I’m a big generic collection user and I can’t express how much the C# 3 Linq expressions have improved my coding experience, especially in the form of lambda syntax. I used to spend a lot of time trying to bend predicates to my will in order to pull the good stuff out of my collections, and some of the new expressions make this a thing of the past.

Some of the expressions do need a bit of extra thought to understand what is going on though. For example, what is the difference between .Where, .TakeWhile and .SkipWhile? They all return a subset of your collection, but what exactly do you get? Let’s investigate!

So say we start with this:

string[] names = {“dave”, “dee”, “dozy”, “beaky”,
“mick”, “titch”, “darius”};

names.Where(name => name.StartsWith(“d”));

 .Where will return “dave”, “dee”, “dozy” and “darius”, matching everything that starts with ‘d’. TakeWhile and SkipWhile are different though, working on your sequence only until a specified condition is deemed false. So…..

names.TakeWhile(name => name.StartsWith(“d”));

will return “dave”, “dee” and “dozy”: the search is called off when StartsWith(”d”) becomes false. Conversely

names.SkipWhile(name => name.StartsWith(“d”));

 

will get you “beaky”, “mick”, “titch” and “darius”, skipping the items in the sequence until StartsWith(”d”) becomes false.

Try doing that with predicates!

ADO.NET Entity Framework Quickstart Tutorial

June 3rd, 2008 | No Comments | Posted in ASP.NET, C#, EntityFramework, LINQ

The ADO.NET Entity framework received another update last week as part of the Visual Studio 2008 and .NET Framework 3.5 Service Pack 1 Beta, so to coincide with this I give you a quick run through to get you started. Being honest, if you have used an OR mapper in the past this will be familiar territory. If not, this new implementation is a good place to start.

screenshot1 So to begin, create a new ASP.NET website, Add New item, and then choose ADO.NET Entity Data Model. Name it EntityModel.edmx and click Add to create a new model.

The Entity Data Model Wizard will pop up and give you a chance to configure your new model. For the purposes of this walk through, choose Generate from database and click Next.

screenshot4 Set up your data connection, I won’t go into this as it’s simple enough to figure out if you’ve never done it before. Click the checkbox to save entity connection settings into your web.config and name it MyEntities.

screenshot6The wizard will then tootle off and retrieve the names of all the tables, views and stored procedures in your database. Choose a table ( I’ve chosen a table from my DB called ‘Log’ ) and name the Model Namespace MyModel.

Click Finish and the designer for your model will open. As a side note have a look at the Model Browser on the right hand side - it looks a bit ‘rendered’ for want of a better description, as though it’s drawn via GDI+ compared to the Solution Explorer….strange!

Now add a new web page and go into the code behind to start the real work. It’s just a simple bit of code to CRUD the Log entities in my database.


// instantiate a new Log entity
MyModel.Log log = new MyModel.Log();

// populate it’s properties
log.Date = DateTime.Now;
log.Exception = “Test Exception”;
log.Level = “Test Level”;
log.Logger = “Test Logger”;
log.Message = “Test Message”;
log.Thread = “Test Thread”;

//instantiate the entity ‘context’ - the object used
//as a ‘gateway’ to the DB
MyModel.MyEntities entities = new MyModel.MyEntities();

// Save the new log entity to the DB
entities.AddToLog(log);
entities.SaveChanges();

// Load it back via a little LINQ query
// ( funny how you must use .First instead of .Single )
MyModel.Log loadedLog = entities.Log .Where(ent => ent.Level == “Test Level”).First();

// make a change and save it back to the DB
loadedLog.Message = “Message has changed!”;
entities.SaveChanges();

// Finally delete the object from the DB
entities.DeleteObject(loadedLog);
entities.SaveChanges();

Notice how you have to call .SaveChanges() to persist back to the DB. That’s it! A super fast run through of CRUD with the ADO.NET Entity Framework! Enjoy!

kick it on DotNetKicks.com

Come and work for Qire in Liverpool

June 3rd, 2008 | No Comments | Posted in ASP.NET, C#

Qire LogoFancy coming to work in Liverpool - European Capitol of Culture 2008 - to build the next generation of voice driven applications?
Qire is the acknowledged leader in intelligent voice messaging systems with many top notch public and private sector clients.
We create applications that manage security, business process and call centre automation via the power of ASP.NET and C# ( plus some other exciting technologies that you can learn on the job ). We are looking for a permanent mid/high level developer to join our small team of happy coders to help shape the future of telephony. You will be adept at asp.net c# sql, have good soft skills and want to work in a friendly and creative coding environment. If you are interested, send me note ( or even better a link to your CV) via the contact form on this site.

How to Build Your Very Own VOIP Telecommunication Platform

January 4th, 2008 | 1 Comment | Posted in C#, Telecommunications, VOIP

I thought Christmas was over, but it obviously isn’t. Some hardware arrived in the office this week - but not just any old hardware. 04012008172For the price of a decent size family car, my employers ( the mighty Qire ) have bought me and the team everything we need to build our very own VOIP/ SIP/ IVR / VXML / CCXML telco!!!!

The kit is a development environment to enable us to build the worlds biggest and best Voice 2.0 telecommunication platform. It’s a reference architecture for us to get to grips with the finer points of SIP call initiation and interaction.

04012008171If you want to do the same, all you need is a handful of top flight super fast servers, some expensive text to speech, speech recognition licenses, a friendly SIP provider and some high spec military grade DSP boards.

04012008173It’s all powered by a custom build high performance C# management and delivery system that we have put together.

Once it is all working together, we will have a system that will give our clients access to the planets most advanced unified communications platform!

Exciting stuff I’m sure you will agree! Stay posted and I’ll keep the blog updated with our progress.

What an awesome idea!

October 3rd, 2007 | No Comments | Posted in C#, Productivity

Like most developers I’ve written a hell of a lot of code in my time: including lots of little utility classes to do things like send emails, validate bits and bobs, format currency etc. Well this enterprising chap has gathered together 5 years worth of his utility classes into 1 handy little library that he is sharing here. Thoroughly recommended!

Also, as a side note on cool little libraries, I’ve been messing about with the free version of FusionCharts. They are great for little ‘dashboard’ style reports where you don’t want to use a large reporting framework. Again, another addition to the toolkit that I’m happy to recommend.

C# 3 Conversion Extension Method Library 1.0

September 19th, 2007 | 8 Comments | Posted in C#

If you have had a chance to muck around with the new language features of C# 3 in the latest beta of Orcas, I’m sure you will be as much of a fan of extension methods as I am. The ability to package little ‘lumps’ of functionality and add them onto objects that I use every day is a lot of fun.

I use the .ToString() method habitually so I thought I’d build a little library to perform some other .ToWhatever methods on some of the basic datatypes. So for example, the String datatype has .ToBool, .ToInt32, . ToDecimal etc. extended into it. The int32 datatype has .ToChar, .ToFloat etc. etc. It’s a nice little utility class that I hope you will find of some use. Download it and have a little dig around to see what the library is capable of.

Also, I’ve added in a couple of string manipulations as extensions too.

To reverse a string we have string.Reverse, to proper case a string we have string.ProperCase ( so for example, “jon davies” becomes “Jon Davies” ). I’ve also added string.Left and string.Right extensions, just because VB has them and C# doesn’t!! string.Left(3) returns the first 3 characters in a string, string.Right(3) returns the last 3.

Anyway, it was a lot of fun writing this - I’m going to keep adding new useful extensions to the library, so don’t forget to bookmark this site and keep an eye out for updates.

Edit: I’ve changed the name of the article thanks to the advice of posters on DotNetKicks and James to be 3.0 centric not 3.5. Apparently there is a bit of confusion as to the naming of the new updates, so it’s the .NET framework 3.5 and C# 3. Thanks for the tip off!

[download#1#image]
kick it on DotNetKicks.com

Productivity tools for the busy C# developer

August 2nd, 2007 | 1 Comment | Posted in C#, Productivity

There’s no getting away from it….I’m a bit of an add-in junky. The visceral thrill of a new bit of productivity kit is one of life’s greatest pleasures. Here’s a short list of tools I couldn’t live without.

GhostDoc - A VSNET addin that generates XML comments based on your method and class names. Normally the results are pretty good and only require a minor amount of tweaking

Google Desktop - Switch off all the visual panels and stick with the utter magic of the ‘double ctrl’ search box.

Launchy - A new one on me. Very like the ‘double ctrl’ Google function but for launching apps and navigating your file structure. You can even coerce it into doing much cleverer things as outlined here on Lifehacker.

WinDirStat - Other disk visualization apps just can’t compete. Bow down to the master.

Subsonic - This bit of kit entirely changed the way I prototype applications. The ability to just throw away large chunks of code and then re-generate new versions is so liberating I can’t explain. It makes entirely usable Active Record based objects and editors ( called scaffold ) to get your projects from 0 to 60 without breaking a sweat.

I heart System.Activator!

June 27th, 2007 | 2 Comments | Posted in C#, Framework

If I had to pick a single class from the wide range that the .Net framework makes available, it would have to be System.Activator. This bit of code has served me well over the years to pull off all kinds of flashy stunts that never fails to impress co-workers and clients.

Well what is it? Briefly, System.Activator can take a couple of strings as input ( along with a whole host of other invaluable overloads ) that specify an assembly name and a class, and re-constitute a fully working instantiated object from them. Impressive huh?! I’ve used it to create extendible plug-in frameworks and super configurable class libraries. Just drop a new DLL in your bin folder, and existing code that has no reference to it can instantiate and use the objects within it.

If you want a crash course on using Activator to create a plug-in architecture for your application, check out Roy Osherove’s article on the subject here.

Here’s a link to the MSDN docs. So why not make today the day that you make acquaintance of System.Activator too?