Tag Archives: C#

Fluent Interface Helper (C#)

I’m practicing my C# so I thought I would take some of my most complex code and convert (most) of it by hand. In previous posts I explored how to make a fluent interface. I had to use lambdas, delegates and a whole lot of custom generics. It was a lot of fun. Here is how to use it in C# and a link to browse the code.

Let’s imagine that you have a collection of people.

List People = new List 
{
    new Person(){ name="Bentley", birthDate=new DateTime(1963, 3, 7)},
    new Person(){ name="Christy", birthDate=new DateTime(1973, 9, 19)},
    new Person(){ name="Benjamin", birthDate=new DateTime(2001, 9, 19)}
};

And let’s Imagine that you wanted to get a list of the people who’s name begins with the letter B and were borne sine 2000. You might do the following:

IEnumerable q = People.Where(p => p.name.StartsWith("B")).Where(p => p.birthDate > new DateTime(2000, 1, 1));

Which certainly gets the job done.

Now imagine that you are responsible for creating an maintaining a large list of business rules that look like this. Oh, and we might have some business analysts write the rules to help you out. Suddenly all the p => gets in the way of readability and sharing the task with less technical people.

One solution is a fluent interface. It’s a little more readable. Here is the same query done fluently.

PersonList q = People.Name().StartsWith("B").BirthDate().Greater(new DateTime(2000,1,1));

What do you think? Is it worth adding some extra code to clean up the text?

You can browse all the code or get a copy with Subversion at: svn checkout https://bentleys-code-samples.googlecode.com/svn/samples/FluentHelper FluentHelper

Simple Screen Shot of all Multiple Monitors

I had a previous post on how to do this in VB. I’m boning up on my c# so I converted it by hand.

Story

Don’t you love it when you take a heaping mess of code and simplify it to it’s essence. It’s even better when you find code in libraries that you can use and delete your own. The less code you own the better. I went searching for a simple code sample to take a screen shot of all screens regardless of the configuration of the monitors. I couldn’t find anything small and easy. by the time I got done figuring it out I thought it should be out on the interwebs just to make it easier for the next guy.  Here it is:

using System;
using System.Drawing.Imaging;
using System.Windows;
using System.Drawing;

namespace Helpers
{
    class ScreenHelper
    {
        public static void SaveAllScreens( string fileName)
        {
            int screenWidth = Convert.ToInt32(SystemParameters.VirtualScreenWidth);
            int screenHeight = Convert.ToInt32(SystemParameters.VirtualScreenHeight);
            int screenLeft = Convert.ToInt32(SystemParameters.VirtualScreenLeft);
            int screenTop = Convert.ToInt32(SystemParameters.VirtualScreenTop);
            using (Bitmap bmp = new Bitmap(screenWidth, screenHeight, PixelFormat.Format32bppArgb))
            {
                using (Graphics gr = Graphics.FromImage(bmp))
                { 
                    gr.CopyFromScreen(screenLeft, screenTop, 0, 0, bmp.Size);
                    bmp.Save(fileName);
                }
            }
        }
    }
}

You can browse the project on Github.

That looks nice and easy. You will need references to System.Drawing if you are using WPF. This works even in the primary window is to the right of any other monitors making the area of the capture be negative. Hopefully this will help someone. Let me know if you have any suggestions.