Mocking

 

In object-oriented programming, mock objectsare simulated objects that mimic the behavior of real objects in controlled ways. A computer programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts.

It is possible to perform integration testing on the Membership Provider, take a look at this blog entry to find out more Integration testing the MembershipProvider

Within the Project we are using Rhino MocksOren Eini is doing a great job with this framework, and I think it's the only one that seems to be keeping up with the latest features in .NET, other than TypeMock. Plus, it's totally free to use. It's also the only one other than TypeMock which does not require you to input strings for method names you'd like to mock, which is cool. It's compelling to use it but the project is currently maintained by just one person. If Oren decides to drop it, or for any reason stop working on it, the book might turn out outdated quite quickly. Plus, it's not very widely used, though it seems to be gaining in popularity.

References

Introduction to Mocking with Rhino Mocks

Mocking

Code Samples

 

Continuous Integration

Task today is to decide and implement an Continuous Integration build process, I will be looking at two options CruseControl and Team City.  The are a lot more but for the purpose of the current .NET environment these are the two I'll be looking at.

When working in a team, the developer needs to know that any changes he makes and stores in to your repository will work for others.  When the developer submits code to the repository they must first update their code to reflect the changes in the repository since they took their copy. The more changes there are to the repository, the more work the developer must do before submitting their own changes.

Eventually, the repository may become so different from the developer's baseline that they enter what is sometimes called, "integration hell," where the time it takes to integrate is greater than the time it took to make their original changes. In a worse case scenario, the changes the developer is making may have to be discarded and the work redone.

Continuous Integration is the practice of integrating early and often, so as to avoid the pitfalls of "integration hell". The ultimate goal is to reduce timely rework and thus reduce cost and time. When done well, continuous integration has been shown to achieve these goals.

After speaking and Googling for each of the products, I quickly came to the  conclusion that although a number of clients I have work with before have been using CruseControl I have found it quite hard to setup and the configuration of XML configuration files can be a bit of a nightmare, and as such we just need a tool to work and not have to maintain.  So for the rest of this article I will be focusing on Team City by JetBrains.

I will be using version 4.5.1. TeamCity-4.5.1.exe (231 mb)

What I like about this product is the licensing, currently the Professional version is free for up to 20 users and 20 build configurations, and when the project get larger you purchase an enterprise license, currently £1,560, ideal way of working and getting you to use the produce.

It's worth watching this short introduction video from Dime Cast, to find out how easy it is to create and use Team City.

The Server automates the integration process by monitoring the team's source control repository directly. Every time a developer commits a new set of modifications, the server will automatically launch an integration build to validate the changes. When the build is complete, the server notifies the developer whether the changes that they committed integrated successfully or not.

Effectively, integration becomes as easy as checking in code. Using an automated integration server not only makes integration easy, it also guarantees that an integration build will happen. There is no danger of developers forgetting to validate their changes after checking in.

The Team City offers several key features:

• Integration with a variety of Source Control systems

• Integration with other external tools, such as NAnt and Visual Studio

• Can build multiple projects on one server

• Remote management and reporting

So down to the installation, once downloaded (231mb file), I installed using all the default settings and it worked, very easy to setup.

Next was to setup a project, again quite easy, I was attaching to a Visual SourceSafe (VSS) solution, I ran in to my first issue as I was getting directory authentication failed.  This was because the running "TeamCity Web Server" was using my System login and it did not have permissions to the network drive for the VSS.  A quick change to the TeamCity Build Server service login and it all worked.

I ran the build on a simple project and it worked without a hitch.

Next was to install the System Tray Notifier, quite simple, going to the "My Settings & Tools" tab brings a list of download, after a simple download and run, then pointing the HTTP location of the Team City installation, it worked.

What can I say it's all just too easy to setup.

Pre-tested Commit

One issue I had, but not with Team City, is that if you are using MS Test, the built in Visual Studio 2008 testing tool, then you will need to run MS Test.  But to run MS Test it is tightly coupled with Visual Studio.  Which means to run MS test you'll need to install a version of Visual Studio on your Continous Intergration machine.  Therefore it might be worth using NUnit for testing.

One very nice feature of Team City is the ability to perform a Pre-tested Commit, this is possible when you install the Visual Studio Addin

An approach which prevents committing defective code into a build, so the entire team's process is not affected.

Submitted code changes first go through testing. If it passes all of the tests, TeamCity can automatically submit the changes to version control. From there, it will automatically be integrated into the next build. If any test fails, the code is not committed, and the submitting developer is notified.

The TeamCity plugins for IntelliJ IDEA, Microsoft Visual Studio 2005/2008 and Eclipse extend the respective IDE with the remote run and pre-tested commit features. Developers test their changes by performing a Remote Run. A pre-tested commit is enabled when commit changes if successful option is selected. 

 

Reference

http://en.wikipedia.org/wiki/Continuous_Integration

Beyond Continuous Integration: Continuous Monitoring with Owen Rogers

Setting up CruiseControl.NET to be a Continuous Monitoring Server

http://www.stevetrefethen.com/blog/archives.aspx#Continuous+Integration

http://www.stevetrefethen.com/blog/CCNETBasedEDIInvoicingProjectGoesIntoProduction.aspx

http://sourceforge.net/project/showfiles.php?group_id=71179&package_id=83198

http://ccnetconfig.codeplex.com/

http://ccnetbuildstation.codeplex.com/

How to sort a generic List<T>

I came across a little issue today, in that I had an Interface object and I needed to be able to sort the list, I would normally implement the IComparable interface on the concreate type, but I don't have access to the concreate type in my case.

I tried several different methods in the end I ended up with using a delegate, and found in SimoneB Blog, which I have extracted the content below:

Sorting a generic List<T> is pretty straightforward if you know how to do it. With C# 2.0, anonymous methods come at hand, as well as the little known Comparison<T> delegate (check out this post for more information about this class as well as other useful classes new to C# 2.0).

Ok, let's suppose we have a product class (let me save some space by using C# 3.0 syntax).

 

class Product

{

    public int ProductID { get; set; }

    public string ProductName { get; set; }

    public decimal UnitPrice { get; set; }

}

When we have a list of products we may want to sort it on the ProductName property before displaying it to the user. This can be accomplished with the Sort method of the List<T> class, which defines several overloads. The most handy in this case is the Sort(Comparison<Product>) method and the result is easily achieved with a couple lines of code.

 

List<Product> products = new List<Product>();

 

products.Sort(delegate(Product p1, Product p2)

              {

                  return p1.ProductName.CompareTo(p2.ProductName);

              });

So far so good, but what if we need to sort our list in several places during the execution of our program? Do we have to write that code each time? Actually no, since we can use the parameterless Sort() method of our list class. What this method does is use the "default comparer" to sort the list. So what's this default comparer? It's the comparer that's automatically created if we implement the IComparable<T> interface. This way we can centralize the sorting logic into our class, and just call the parameterless Sort() method on it whenever we need it sorted on the ProductName property.

 

public class Product : IComparable<Product>

{

    [...]

 

    public int CompareTo(Product other)

    {

        return ProductName.CompareTo(other.ProductName);

    }

}

Ok, now what if we want to be able to sort it on the other two properties, ProductID and UnitPrice? Do we have to write an anonymous method each time as we did in the beginning? Of course no, since there's a useful trick which prevents us from needing to do that. We can define two static Comparer<Product> properties in our product class, and supply them as parameters to the Sort(Comparer<T>) method of our list whenever we need it sorted on something which is not the default sorting logic.

 

public class Product : IComparable<Product>

{

    [...]

 

    public static Comparison<Product> PriceComparison =

        delegate(Product p1, Product p2)

        {

            return p1.Price.CompareTo(p2.Price);

        };

 

    public static Comparison<Product> IDComparison =

        delegate(Product p1, Product p2)

        {

            return p1.ProductID.CompareTo(p2.ProductID);

        };

 

    [...]

}

Since they are static they can be used simply like so: products.Sort(Product.PriceComparison) or products.Sort(Product.IDComparison), which will respectively sort the list by price and id.

 

Below is the full code of the Product class.

 

public class Product : IComparable<Product>

{

    private int id;

    private string prodName;

    private decimal price;

 

    public static Comparison<Product> PriceComparison = delegate(Product p1, Product p2)

                                                        {

                                                            return p1.price.CompareTo(p2.price);

                                                        };

 

    public static Comparison<Product> IDComparison = delegate(Product p1, Product p2)

                                                     {

                                                         return p1.id.CompareTo(p2.id);

                                                     };

 

    public int ProductID

    {

        get { return id; }

        set { id = value; }

    }

 

    public string ProductName

    {

        get { return prodName; }

        set { prodName = value; }

    }

 

    public decimal UnitPrice

    {

        get { return price; }

        set { price = value; }

    }

 

    public Product(int id, string prodName, decimal price)

    {

        this.id = id;

        this.prodName = prodName;

        this.price = price;

    }

 

    #region IComparable<Product> Members

 

    public int CompareTo(Product other)

    {

        return ProductName.CompareTo(other.ProductName);

    }

 

    #endregion

 

    public override string ToString()

    {

        return string.Format("Id: {0} Name: {1} Price: {2}", id, prodName, price);

    }

}


Well all love a good Hack

Been playing around with this hack for some time, it's fun, I can't remember where I first got the application from, but I've had some harmless fun with it in the office.

This small hack allows you to write information to most HP printers LCD screen.

HPHack.zip (6.49 kb)

Copy one object to another Object

I was in need to copying one concreate object to another concreate object, but I needed the flexability that when a new property is added the method would not need to be changed, also I needed the ability for the method to take in any types.

So the first take was to generate a method that uses reflections to go over the obecjts properts and set the returning object properties.  That was quite simple, then to make the method more flexable I used generics to help with my method, to allow for any types to be used.

One thing to note is that you require the try catch, in case the GetValue or SetValue fails, such as if the object is null.

This is what I ended up with.

Here is the VB.NET method

Here is the C#

 

PDB, what and why?

If you are like me when you are developing you create a Debug version of your application and in turn .NET provides you with .PDB files for debugging, that is as far as I know, want to know more about it then take a look at John Robbins' Blog on PDB files: what every developer must know

Returning all data from a DataSet Row

If you are like me and you are using DataSets and you need to transfer a table rows collection in to a predefined interface then here is your answer

Here is its usage

Only get the data that is needs to be displayed

I have been on the look out for the ability to display and search a customers database, but having the ability to display millions of records if that what the end users wants.  But the trouble is that it can take a long time to return a million records from a data source.

I've talked about paging before in my article LINQ-to-SQL-and-Paging, which works fine for internet based paging, but what about WPF and WinForm Grids, well I came across Vitrual Mode for the Microsoft Data Grid, which can provide an answer.  What this allow you to do is, load the data you can see on the screen very quickly and as you scroll through the data is fetched when required.

By setting the VirtualMode = true for a data grid, it allows you to add a handler for deail with CellValueNeeded 

Here is the main body of the code

Each time the Data grid needs some values it will call the following:

And of course the NameListCache class

 

I have attached the source, you will also find a PopulateData project to populate the database with data.

 

DataGridViewVirtualModePaging.zip (19.43 kb)

PopulateData.zip (11.43 kb)

Here are a few of the database scripts that yu will need for the sample

Names.sql (581.00 bytes)

GetNames.sql (786.00 bytes)

Hidden but found on the iPhone

I've had an iPhone for a year and a half now, and I'm still finding things it can do, here are a few things that I have found:

  • Pressing the Home button twice quickly takes you to your favorites (this can be configured in the setting)
  • Press Home button while pressing the on/off button causes a Print Screen
  • If you continue to home the Home and on/off button it will perform a reboot of your iPhone
Have you found any more features that I have missed?

 

How to get an EULA displayed for your clickonce application

Stuck with this issue, if needing a ClickOnce installation to display an EULA, I final found a solution, on MSDN

Here's a round about way to get your clickonce applications to install with an End user license agreement. Basically you build a redistributable component that can be seen in your prerequsites dialog box under the publish window. This allows a nice way to have all your apps reuse the same agreement, if you want. It's very easy, you need only to create three files ("eula.txt", "product.xml", and "package.xml") and two folders in this case ("EULApackage", and "en"). I documented everything below on how I set mine up. it works great. the only thing you'll have to change is the Name of the component and of course you'll need your own end user licence agreement saved as eula.txt. The component needs to be put in the following path:

Visual Studio 2005

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages

Visual Studio 2008

C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages

in this folder you should see some sub folders for other redistributable components. First make a new sub directory for your component. I called mine EULApackage. in this new folder you need the following. -A file called product.xml and a sub folder called "en" (for english) you can do various things with the product.xml file, but here's the way mine looks

<?xml version="1.0" encoding="utf-8" ?>

<Product
 
xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper"
 
ProductCode="EULA.Bootstrap.Component"
>
 
<!-- Defines list of files to be copied on build -->
 
<PackageFiles>
   
<PackageFile Name="en/eula.txt"/>
 
</PackageFiles>

 
<Commands>
   
<Command PackageFile="en/eula.txt"
     
Arguments='' >
       
<ExitCodes>
<ExitCode Value="0" Result="Success"/>          
<DefaultExitCode Result="Fail" FormatMessageFromSystem="true" String="GeneralFailure" />
       
</ExitCodes>
   
</Command>
 
</Commands>
</Product>

*in this case the file eula.txt was the text file that was my license agreement. Note that it's not an rtf file. Rtf won't display propertly using this method.

Now inside my "en" subfolder i put the eula.txt file and another xml file called package.xml, again this xml file can be used to do all kinds of stuff

heres the contents of my version*

 

 <?xml version="1.0" encoding="utf-8" ?>

<Package
 
xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper"
 
Name="DisplayName"
 
Culture="Culture"
 
LicenseAgreement="eula.txt">

   
<PackageFiles>
       
<PackageFile Name="eula.txt"/>
   
</PackageFiles>

 
<!-- Defines a localizable string table for error messages and url's  -->
 
<Strings>
   
<String Name="DisplayName">Companies End User License Agreement</String>
   
<String Name="Culture">en</String>

   
<String Name="CancelFailure">User Failed to Accept Companies End User License Agreement.</String>
   
<String Name="GeneralFailure">A fatal error occurred during the installation of ELUA Component Execution</String>
   
<String Name="AdminRequired">You do not have the permissions required to install this application.  Please contact your administrator.</String>
 
</Strings>    
</Package>

Note: whatever you put in the DisplayName field is what your user will see when he's confronted with the eula text If you have all this put together correctly and in the right folders, the next time you start up VS2005 and go to your publish tab -> prerequisites you should see the DisplayName field. Just check this as a prerequisite for you app. when the user clicks install on the publish.htm file it'll present the user with the conents of your eula.txt file inside of a standard license acceptance dialog box. if the choose accept your stuff installs, if they decline then it exits rather nicely and nothing is installed on their systems. If you mess up the formatting for either of the two files or if you leave out the "en" sub folder then the component won't show up in the prerequisites dialog (when publishing)

Additional notes: although this works great it is a round about method and their are ways around the eula, such as if your publish.htm file allows them to run the application directly (I guess without the bootstrapper starting)but if they click the install button it will run. This also has the benifits of not running every time you publish an update to your clickonce application. They have to run the boot strapper to get the eula to show up (by clicking the install button on publish.htm) I figured out this method by looking at some of the other redistributable components that were already in the path

Visual Studio 2005

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages

Visual Studio 2008

C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages

You can look in those other components product.xml and package.xml files to see what cool things they've done with them.

 

If you are using Visual Studio 2008, you can also set up your project file to include the Package, here is an example below

<BootstrapperPackage Include="EULA.Bootstrap.Component">
  <Visible>False</Visible>
  <ProductName>RPS End User License Agreement</ProductName>
  <Install>true</Install>
</BootstrapperPackage>

About the author

You have probably figured out by now that my name is Bryan Avery (if not, please refer to your browser's address field).  Technology is more than a career to me - it is both a hobby and a passion.  I'm an ASP.NET/C# Developer at heart...

Month List