How to write more efficient and maintainable C# code

Published on August 23, 2013 in C# · Read time 4 minutes

When you are writing your C# applications, you are aware that you want to make your applications as efficient, maintainable, and as scalable as possible. These are some great keywords that look great on blog posts like this, but how do you actually set about achieving this goal? We will look at some simply guidelines which try to make this goal more achievable. Write single purpose methods Your methods should have a single purpose… a single task to perform. For example, a single method should not try to access a database, create objects, read text files, and create images. Instead, create a single method for each task. Why? Multipurpose methods are typically long, hard to read, difficult to maintain and inefficient. Pros: Reduces reuse, improves readability and maintainability. Cons: None Keep methods short A method should be an absolute maximum of 50 lines long. Methods that are longer than this are typically multipurpose, which is bad. You want your code to be reusable where possible. Pros: More maintainable code, which is more reusable. Cons: None. Keep classes short Classes should be no more than 300 lines long. If you have a “DocumentHelper” class which creates Microsoft…

WPF Entity Framework Code First migrations

Published on August 16, 2013 in Entity Framework · Read time 6 minutes

Recently I wrote a WPF application using Entity Framework Code First and released it into the wild. Shortly after, the business requirements changed and I had to make changes to the model. Thus, I was introduced to Code First migrations. I did a lot of research on Code First migrations and could only really find the most simple of examples, and almost robotic documentation on MSDN that provided minimal help. This post aims to provide a clearer view on migrations, and how to properly create them. Before we can start with migrations, we need to create our normal C# code and then add in Entity Framework using NuGet. Once done, we can create our first migration and then explore a second migration. The Code The code is going to revolve around the common idea of customers and orders. Code structure; An has a collection of and a , the has a . Any properties that are marked as virtual are navigation properties, and are lazy loaded by Entity Framework at runtime. With the code structure in place, we can now introduce Entity Framework. Go ahead and use the package manager console, or the library package manager to install Entity Framework. Enabling Migrations Before enabling…

Using StructureMap setter injection to inject repositories into your View Model

Published on August 09, 2013 in WPF MVVM · Read time 2 minutes

This short post is a follow on from an earlier series of posts that discussed how to achieve MVVM purity in WPF using Structure Map. You may want to check out those posts first before reading this post. However, if you just generally want to know how to do setter injection using Structure Map, you are in the right place! A Scenario It is very common in modern applications to have a repository that does the job of retrieving and saving data from/to your database. Your repository may look like this; In the above sample we are simulating retrieving the number of customers that currently exist in our database. Our view model logic may look something like this; When the view model has finished loading, we want to retrieve the number of customers from the repository and display this number to the user. How do we get an instance of ? Well there are two approaches, this is the first; You can use the static method to get an instance of the repository and set the public property to that value. Don’t get me wrong, this approach works… but it is widely considered to be an anti-pattern. The second approach is to use setter injection, which basically means that StructureMap will inject a…

Getting started with SQLite and C#

Published on August 07, 2013 in C# · Read time 2 minutes

SQLite is a powerful, extremely light-weight transactional SQL database provider. SQLite is free and open source, and it does not require any server processes to handle it. SQLite is cross platform and runs on multiple architectures, making it ideal for use on the major operating systems such as Windows, Mac OS, Linux, and even lower powered devices such as PDAs, MP3 players etc. Prerequisites It is possible to get up and running with SQLite quickly in C#, by adding the System.Data.SQLite package to your project. Open the package manager console window run the following; Or alternatively, right click your project and click Manage NuGet Packages and search the online gallery for System.Data.SQLite . A Helpful Wrapper Class I have put together a very simple wrapper class that will get you up and running quickly. Here it is; This wrapper is loosely based on some code written by Mike Duncan . Usage The constructor takes the database path (the extension is completely up to you); To execute a standard non query command, you first create your command and call the method; You can retrieve data from the database in the form of the timeless object; Summary SQLite gives us the…

WPF MVVM IoC containers - Part 2 of 2

Published on August 02, 2013 in WPF MVVM · Read time 9 minutes

The ultimate goal of MVVM is to achieve true separation of concerns, meaning that the various elements of your project know nothing about each other. It’s virtually impossible to achieve this in an elegant way in WPF without some outside help. You are reading the second part of this blog post where we will discuss how to use an IoC container properly in a WPF MVVM application. It is assumed you have either read the first post, or are familiar with the concept of IoC/dependency injection with StructureMap. Part 0 It’s worth noting just before we get started that you will need to invest a little time to get this set up initially. That investment will pay off almost immediately as your application begins to scale. Please take the time to, if you haven’t already, install PropertyChanged.Fody and StructureMap from NuGet. Core Classes Start by creating some core classes for use within the rest of your application. Getting these in a good state early will make architecting the rest of the application a lot easier. I like to have all my classes inherit from a class called BaseNotification, so that I (thanks to PropertyChanged.Fody) get all the change notification functionality added…