Eventually in your development career you will have to create Mock objects to get your work done, especially if you are working with distributed systems. You don’t always have access to some parts of the system, in fact they may not even exist yet.

That just happens to be the case with some work I started today but I don’t want to let that get in the way.

I need to simulate the system that (will) eventually exist at the other end of the wire and and I want to do so in a realistic fashion that will simulate what I require.

Enter the Mock.

The work I am doing at the moment will be performing searches against a third party system and then logging transactions against that same system.

So, I need a Web Service that will enable me to do the search and I need a Web Service that will enable me to log a transaction.

I also need a decent amount of data to simulate the potential performance and tune my solution accordingly.

To do this I have recruited some folks that I am becoming very friendly with these days:

  • Visual Studio 2010 Data Generation Plans
  • WCF Data Services
  • ADO.NET Entity Data Models
  • OData

These 4 chaps all working together have allowed me to get my mock up and running in around 90 minutes with a random database of 500,000 members.

Mock Test Data

First port of call was to find some list of First and Last Names, Companies and Australia Suburbs. 30 minutes on Google had me good size lists with ~5,500 first names, 65,000 last names, 17,000 suburbs and 250 company names.

I imported those in to my sample database and then I was ready to generate my data.

Fire up Visual Studio 2010 and create a new Database Project, use the import wizard to create my database schema and then we are ready to create the data generation plan shown below:

Data Generation Plan

So a quick explanation of the above.

I have a Mock database that holds my Member Table. I then have a sample database that I imported all my data from the web search in to.

I then select the ‘data bound generator’ for the columns I want to fill and point them at my sample data. A quick test with 50 rows to make sure it all works and then I just set my ‘Rows to insert’ to 500,000 and I am good to go with a sizeable database of data.

Mock Services

Now that I have my sample data it’s time to create the Web Services.

I don’t want to spend ages on this I just want to get up and running quickly with my Mock Service so I can crack on with the real development.

Step 1. Create an empty ASP.NET Web Application:

Empty ASP.NET Web Application

Step 2. Create a new ADO.NET Entity Data Model

ADO.NET Entity Data Model

Follow through the wizard, point it at your database and tables and hey presto done!


Step 3. Create a new WCF Data Service

WCF Data Service

And add just that little bit of code we need for the Mock Service

namespace Mock { publicclass RedemptionsService : DataService { // This method is called only once to initialize service-wide policies.publicstaticvoid InitializeService(DataServiceConfiguration config) { // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.// Examples: config.SetEntitySetAccessRule("Members", EntitySetRights.AllRead); config.SetEntitySetAccessRule("Redemptions", EntitySetRights.AllWrite); config.SetServiceOperationAccessRule("Search", ServiceOperationRights.All); config.SetServiceOperationAccessRule("Redeem", ServiceOperationRights.All); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; } [WebGet] public List Search(int memberNumber, string firstName, string lastName, string businessName) { using (MockEntitiesctx = new MockEntities()) { var members = (from m in ctx.Members where m.MemberNumber == memberNumber || m.FirstName.StartsWith(firstName) || m.LastName.StartsWith(lastName) || m.BusinessName.StartsWith(businessName) select m).ToList(); return members; } } [WebGet] publicvoid Redeem(int memberNumber, int amount, string receiptNumber) { try { using (MockEntitiesctx = new MockEntities()) { Redemption redemption = ctx.CreateObject(); redemption.MemberNumber = memberNumber; redemption.ReceiptNumber = receiptNumber; redemption.Amount = amount; redemption.CreatedOn = DateTime.Now.ToUniversalTime(); ctx.AddToRedemptions(redemption); ctx.SaveChanges(); } } catch (Exception e) { string message = e.Message; } } } }

And that’s it, I am now up and running with my Mock Services, a good selection of real random data and it took me all of 90 minutes, most of which was finding the sample data.

To access my new service I simply use the OData Protocol to execute my custom methods for searching and redemptions.

Search Example


xmlversion="1.0"encoding="utf-8"standalone="yes" ?> - <feedxml:base="http://localhost:81/CMSMock/RedemptionsService.svc/"xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"xmlns="http://www.w3.org/2005/Atom"><titletype="text">Searchtitle><id>http://localhost:81/CMSMock/RedemptionsService.svc/Searchid><updated>2010-08-19T07:38:10Zupdated><linkrel="self"title="Search"href="Search"/> - <entry><id>http://localhost:81/CMSMock/RedemptionsService.svc/Members(14)id><titletype="text"/><updated>2010-08-19T07:38:10Zupdated> - <author><name/>author><linkrel="edit"title="Member"href="Members(14)"/><categoryterm="Model.Member"scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/> - <contenttype="application/xml"> - <m:properties><d:MemberNumberm:type="Edm.Int32">14d:MemberNumber><d:FirstName>Kareemd:FirstName><d:LastName>Cobernd:LastName><d:BusinessName>Harvey Norman Holdings Ltd.d:BusinessName><d:Suburb>WEHLAd:Suburb><d:CurrentBalancem:type="Edm.Int32">1035109842d:CurrentBalance>m:properties>content>entry>feed>


Not only does the combination of these technologies let me get up and running really quickly for a Mock object they also let me get up and running real quick for real life solutions.

The end product is Web Friendly, standards compliant and the tool set that let me create this is brilliant.