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

 

Configuration for NLog

When I am writing an application it is so important to make sure you record and log information correctly, there are a number of free loggers you can use

I have come to use NLog, by Jaroslaw Kowalski, it’s so easy to use and get up and running, which is what you really need if you are using a logger of any type.

But what is the best practice when using a log in a live environment?

Or more importantly what and how do you configure you configuration file to make sure you are logging all your information?

The way I see it is, capture your logging to the Event Log and a Database for information and tracing.

When it comes to Errors and Fatal logging then, write it to as many places as you can, an Error file, the Event Log, a Database Log, and perhaps a special Exception Database Log, because if something has failed, you’ll want to shout about it, and you want to record it in as many places as possible.

Here is an example of an NLog configuration

NLog.config (2.63 kb)

To setup the database use the following creation creation script

NLogDatabase.sql (1.16 kb)

Make sure within the SQL Database you have a user “nloguser”, with a password of “nlogpassword”, these are both defined in the NLog configuration file, and can be changed if need be.

That is it, all you have to do now is wait for your live application to log some information.

I am sure there are a few other configuration options available and I welcome anyones comments on how to improve this configuration file.

London

 

Arrive on Friday night, preferably at city airport and head east to Brick lane for a curry. After dinner have drinks at the Vibe bar or 93 feet east. If you are feeling super energetic see what is going on at Sosho, Cargo or Electricity showrooms before getting a good night of sleep.

Saturday morning, get up early and have breakfast at Patisserie Valerie in Knightsbridge. Have a wander around Harrods for an hour or two making sure that you check out the food hall. Jump on the tube and head over to Camden. You may enjoy the market – I don’t – but if the chaos isn’t for you get off at Chalk Farm station rather than Camden to avoid it all. Grab lunch at Thanh Binh one of the best cheap Vietnamese restaurants in London, I recommend the Pho.

After lunch, walk to the top of Primrose hill for one of the best views of the whole city. On the way back to the tube, stop for a pint or two outside The engineer or The queens.

I’d suggest a rest at this point so you can make the most of your evening. For dinner book Latium and have cocktails before/after at the Sanderson.

You are on your own for the evening. I don’t really do clubbing any more, but the one piece of advice I give is don’t just wander to Leicester square and choose a venue, there are some absolute hovels. If you are just after a few more drinks after dinner I like the bar at the Charlotte street hotel.

Sunday morning, make sure you enjoy a full English breakfast to start your day. Your hotel probably serves one and to be honest there isn’t any particular culinary skill involved in making one so it is hardly worth hunting down an ‘authentic greasy spoon’ cafe.

Get a tube to Westminster and walk over the bridge enjoying the uber tourist views of Big ben and the houses of parliament. Get a boat from Waterloo pier next to the London eye to Bankside pier, browse Tate modern for a while then cross the river using the millennium foot bridge, head up to St. Pauls Cathedral and check out thewhispering gallery.

Make sure you use the Thames Clippers commuter boats. The tourist boats are over priced and very slow.

Back on the boat to Greenwich, from Blackfriars pier. Walk through the grounds of the naval college and into the park, up to the observatory for another view over London. If you have time check out the three markets in Greenwich.

Finish your trip with a late Sunday roast and a beer at the Plume of feathers (you’ll need to book a sitting at 2 or 4pm). Get on the DLR and head back to city airport!

That is just one of many ways of spending a weekend in London. I missed out Fish and Chips, some great markets and many of my favourite pubs simply because of time constraints. I also tried to leave plenty of spare time between my recommendations so you can wander off the beaten track and discover your own London – in my experience, just wandering is one of the true joys of travel.

My final tip, here is what  to avoid. Leicester Square, Theatre Land, The London eye, Buckingham palace. Some Londoners may passionately disagree, but I think you’d be wasting valuable time.

So, if you are reading this and thinking of visiting, enjoy my city. Arguably the greatest on earth.

 

Windows Service

I need to generate a Windows Service application, I’ve done this before, but could I remember how to do it.  In my searching I found this code sample very useful Creating a Basic Windows Service in C# on The Code Project

WindowsService_Src

 

Another useful article I found was from Microsoft Windows Services: New Base Classes in .NET Make Writing a Windows Service Easy

 

Caching from a non Web applications

Okay you are developing a WinForm application and you are looking at performance, one area is Caching of information, but wait a minute I’ve been using caching in my web applications for sometime, but how do you do it from a WinForm application as you don’t have a host to store your cache.

Would you know it you can use the web cache too, just you have to take a few extra steps to retain your cache, either in memory or in temporary files.

You can use the HttpRequest to store the System.Web.Cache objects in your application, and this works well.

I also came across any Blog, providing another method to Caching, however I have a little concern over this method of caching in files as you are likely to get file locking.

Using System.Web.Caching From The Console Or Windows Forms

I’ve written my own simple VB.NET application to show it working and just holding the Cache in memory for the life of the application.

WinFormCache.zip (17.73 kb)

Of course you could run a Windows Service which holds all the Cache information, this way it will be available to all your WinForm applications should you require it.

StructureMap Example

After using StructureMap for a while I thought it would be nice to generate a simple example of how it all works, so I’ve built in Visual Studio 2008, both C# and a VB.NET version.

StructreMap Example.zip (4.47 kb)

StructureMapExampleVB.zip (9.41 kb)

For more information on StructureMap please go to their main website

If you need to know more about IoC’s then it’s worth making a look at the following links:

Inversion of Control

List of .NET Dependency Injection Containers (IOC)

IoC Benchmarks

MVC Storefront: Dependency Injection

NinJect

Small free and replacement for Notepad

Do you still use Notepad that ships with Windows?

I did, until I found NotePad2, it’s fast, it’s easy to use, and more to the point it works.

I’ve been using it for the past 2 months, and I have not got any clue to the features, but what I like about it is it just works and is a very good replacement for the standard Windows Notepad. 

Here are the features, cribbed from the NotePad2 Website:

Customizable syntax highlighting: 

  • HTML, XML, CSS, JavaScript, VBScript, ASP, PHP, CSS, Perl/CGI
  • C/C++, C#, Java, VB, Pascal, Assembler, SQL, Python, NSIS
  • INI, REG, INF, BAT, DIFF
  • Drag & drop text editing inside and outside Notepad2
  • Basic regular expression search and replace
  • Useful word, line and block editing shortcuts
  • Rectangular selection (Alt+Mouse)
  • Brace matching, auto indent, long line marker, zoom functions
  • Support for Unicode, UTF-8, Unix and Mac text files
  • Open shell links
  • Mostly adjustable
  • Auto indentation
  • Bracket matching
  • Encoding conversion between ASCII, UTF-8 and UTF-16 formats
  • Multiple undo/redo; rectangular block selection
  • Newline conversion, between DOS (CR/LF), Unix (LF) and Mac (CR) formats 

How to Replace notepad with notepad2 in Vista?

  1. First you need to download notepad2 from here
  2. First you need to Rename Notepad2.exe to notepad.exe.
  3. Find c:\windows\notepad.exe and c:\windows\system32\notepad.exe and set the owner to ‘Administrators’, and grant Administrators full control.
  4. Using Windows Explorer, drag and drop the renamed notepad2.exe to c:\windows and c:\windows\system32.
  5. Optional replace the permissions on the new notepad.exe to Read & Execute and Read.
 
Another one to look at is Notepad++, it has a lovely set of plugins such as XML have a look at Tim Almonds post on the XML plugin

Structure to Stored Procedure

Everywhere I go from client to client I find that when developers create Stored Procedures that just perform the task in hand, and I so often find that no exception handling, comments or transaction based handling has been put in place.  So with all this in mind I set to write a structured Stored Procedure to be used as a template for future Stored Procedures that I create.

-- =============================================
-- Author: Bryan Avery
-- Create date: 15th July 2008
-- Description: Import the Taxonomy in to WPPs
-- =============================================
ALTER PROCEDURE [dbo].[sp_WaSPs_TaxonomyImport] 
 @Debug bit = 0,
 @ErrorMessage varchar(2000) OUTPUT, 
 @return_code Int OUTPUT

AS
BEGIN
-- ========================================================
-- Declare SProc variables 
-- ========================================================

DECLARE @Error int
 DECLARE @RowCount int

-- ========================================================
-- Initialise SProc variables 
-- ========================================================
 
 SET NOCOUNT ON;

-- ========================================================
-- SProc content 
-- ========================================================

IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
DECLARE @Debug_ProcStartTime datetime 
, @Debug_ProcEndTime datetime 
 
SET @Debug_ProcStartTime = Getdate() 
 
SET NOCOUNT OFF 
 
 PRINT '' 
 
 PRINT '-- ======================================================' 
 PRINT '-- = PROCEDURE sp_WaSPs_TaxonomyImport' 
 PRINT '-- = @Debug = ' + IsNull(Cast(@Debug as varchar),'<NULL>') 
 PRINT '-- ======================================================' 
 
 PRINT '' 
 
 
END

IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * BEGIN TRANSACTION' 
 PRINT '-- ******************************' 
 
END

-- ========================================================
-- Uncomment the section below if you want to remove all Taxonomy information from the application
-- ========================================================

-- DELETE tbl_WaSPs_GroupArea;
-- DELETE tbl_WaSPs_AreaUserName;
-- DELETE tbl_WaSPs_AreaSite;
-- DELETE tbl_WaSPs_Areas;
-- 
-- DELETE tbl_WaSPs_SiteSite;
-- DELETE tbl_WaSPs_Sites;


BEGIN TRANSACTION


BEGIN TRY

-- ========================================================
-- Update the areas
-- ========================================================

IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * Update the Areas' 
 PRINT '-- ******************************' 
 
END

UPDATE tbl_WaSPs_Areas
 SET AreaName = et2.EntityType, EntityTypeCD = et2.EntityTypeCD, myView = 'False'
 FROM WaspFactory.dbo.tblEntityType et2, tbl_WaSPs_Areas a1
 WHERE et2.EntityTypeCD = a1.EntityTypeCD 
 
-- ========================================================
-- Insert the areas
-- ========================================================
 
IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * Insert the Areas' 
 PRINT '-- ******************************' 
 
END

INSERT INTO tbl_WaSPs_Areas(AreaName, EntityTypeCD, myView)
 SELECT et2.EntityType, et2.EntityTypeCD, 'False'
 FROM WaspFactory.dbo.tblEntityType et2
 WHERE et2.EntityTypeCD NOT IN (SELECT EntityTypeCD FROM tbl_WaSPs_Areas)

-- ========================================================
-- Update the sites
-- ========================================================
 
IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * Update the Sites' 
 PRINT '-- ******************************' 
 
END

UPDATE tbl_WaSPs_Sites
 SET title = e1.EntityName, siteid = e1.EntityIdent, entityid = e1.EntityID
 FROM WaspFactory.dbo.tblEntity e1, tbl_WaSPs_Sites s1
 WHERE e1.EntityName IS NOT Null AND
 e1.EntityId = s1.entityid

-- ========================================================
-- Insert the sites
-- ========================================================
 
IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * Insert the Sites' 
 PRINT '-- ******************************' 
 
END

INSERT INTO tbl_WaSPs_Sites(title, siteid, entityid)
 SELECT EntityName, EntityIdent, EntityID
 FROM WaspFactory.dbo.tblEntity
 WHERE EntityName IS NOT Null AND
 EntityId NOT IN(SELECT entityid FROM tbl_WaSPs_Sites)

-- ========================================================
-- Remove all the links first
-- ========================================================
 
IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * Remove all the links first' 
 PRINT '-- ******************************' 
 
END

DELETE tbl_wasps_AreaSite
 FROM tbl_WaSPs_Areas Areas, WaspFactory.dbo.tblEntityType EntityType,
 tbl_WaSPs_Sites Sites, WaspFactory.dbo.tblEntity Entity
 WHERE Areas.EntityTypeCD = EntityType.EntityTypeCD AND
 Sites.EntityId = Entity.EntityId AND
 Entity.EntityTypeCD = EntityType.EntityTypeCD

-- ========================================================
-- Link the sites to the areas
-- ========================================================
 
IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * Link the sites to the areas' 
 PRINT '-- ******************************' 
 
END

INSERT INTO tbl_wasps_AreaSite(AreaId, SiteId)
 SELECT AreaId, Id
 FROM tbl_WaSPs_Areas Areas, WaspFactory.dbo.tblEntityType EntityType,
 tbl_WaSPs_Sites Sites, WaspFactory.dbo.tblEntity Entity
 WHERE Areas.EntityTypeCD = EntityType.EntityTypeCD AND
 Sites.EntityId = Entity.EntityId AND
 Entity.EntityTypeCD = EntityType.EntityTypeCD AND
 (AreaId NOT IN (SELECT AreaId FROM tbl_wasps_AreaSite) AND
 Id NOT IN (SELECT SiteId FROM tbl_wasps_AreaSite))
 
-- ========================================================
-- Remove all the site to site links
-- ========================================================
 
IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * Remove all the site to site links' 
 PRINT '-- ******************************' 
 
END

DELETE tbl_WaSPs_SiteSite
 WHERE SiteId = fkSiteId

-- ========================================================
-- Link the site to themself
-- ========================================================

IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * Link the site to themself' 
 PRINT '-- ******************************' 
 
END

INSERT INTO tbl_WaSPs_SiteSite(SiteID, fkSiteId, WaterDirection)
 SELECT ID, ID, 'Neither'
 FROM tbl_WaSPs_Sites

-- ========================================================
-- Delete all the site to site links
-- ========================================================

IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * Delete all the site to site links' 
 PRINT '-- ******************************' 
 
END

DELETE tbl_WaSPs_SiteSite
 FROM tbl_WaSPs_Sites s1, WaspFactory.dbo.tblEntity e1,
 WaspFactory.dbo.tblEntityLink el1, 
 WaspFactory.dbo.tblEntity e2, 
 tbl_WaSPs_Sites s2
 WHERE e1.EntityId = s1.EntityId AND
 s1.EntityId = el1.EntityID1 AND
 el1.EntityID2 = e2.EntityId AND
 e2.Entityid = s2.EntityId

-- ========================================================
-- Link the site to other sites
-- ========================================================

IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * Link the site to other sites' 
 PRINT '-- ******************************' 
 
END

INSERT INTO tbl_WaSPs_SiteSite(SiteID, fkSiteId, WaterDirection)
 SELECT s1.ID, s2.ID, 'Neither'
 FROM tbl_WaSPs_Sites s1, WaspFactory.dbo.tblEntity e1,
 WaspFactory.dbo.tblEntityLink el1, 
 WaspFactory.dbo.tblEntity e2, 
 tbl_WaSPs_Sites s2
 WHERE e1.EntityId = s1.EntityId AND
 s1.EntityId = el1.EntityID1 AND
 el1.EntityID2 = e2.EntityId AND
 e2.Entityid = s2.EntityId
 
END TRY 
 
BEGIN CATCH 
 
 SELECT @Error = @@ERROR, @RowCount = @@ROWCOUNT ; IF (@Error <> 0 OR @RowCount = 0) GOTO QuitWithRollback -- (check for error) 
 
END CATCH

-- ---------------------------------------------------------------------------------------------------- 
-- Commit Transaction 
-- ---------------------------------------------------------------------------------------------------- 
 
IF (@@TRANCOUNT > 0) 
 
BEGIN 
 
 IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
 BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * COMMIT TRANSACTION' 
 PRINT '-- ******************************' 
 
 END 
 
 COMMIT TRANSACTION 
 
END

GOTO QuitWithSuccess

-- ---------------------------------------------------------------------------------------------------- 
-- Error Handling 
-- ---------------------------------------------------------------------------------------------------- 
QuitWithConcurrencyRollback: 
 
IF (@@TRANCOUNT > 0) 
 
 BEGIN 
 
 IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
 BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * ROLLBACK TRANSACTION' 
 PRINT '-- ******************************' 
 
 END 
 
 ROLLBACK TRANSACTION 
 
 END 
 
GOTO QuitWithConcurrencyError

-------------------------------------------------- 
QuitWithRollback: 
 
IF (@@TRANCOUNT > 0) 
 
 BEGIN 
 
 IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
 BEGIN 
 
 PRINT '-- ******************************' 
 PRINT '-- * ROLLBACK TRANSACTION' 
 PRINT '-- ******************************' 
 
 END 
 
 ROLLBACK TRANSACTION 
 
END 
 
GOTO QuitWithError

-------------------------------------------------- 
QuitWithConcurrencyError: 
 
IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 SET @Debug_ProcEndTime = Getdate() 
 
 PRINT '' 
 PRINT '-- ======================================================' 
 PRINT '-- = sp_WaSPs_TaxonomyImport' 
 PRINT '-- = RETURN VALUE' 
 PRINT '-- = QuitWithConcurrencyError' 
 PRINT '-- = @ErrorMessage = ' + IsNull(Cast(@ErrorMessage as varchar),'<NULL>') 
 PRINT '-- = EXECUTION TIME' 
 PRINT '-- = Start = ' + IsNull(Convert(varchar(24), @Debug_ProcStartTime , 113),'<NULL>') 
 PRINT '-- = End = ' + IsNull(Convert(varchar(24), @Debug_ProcEndTime , 113),'<NULL>') 
 PRINT '-- = Ellapsed Time (ms) = ' + IsNull(Cast(Datediff(millisecond, @Debug_ProcStartTime, @Debug_ProcEndTime) as varchar),'<NULL>') 
 PRINT '-- ======================================================' 
 PRINT '' 
 
END 
 
SET @ErrorMessage = 'Concurrency Error' 
 
SET @return_code = 3 
 
RETURN(3)

-------------------------------------------------- 
QuitWithError: 
 
IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 SET @Debug_ProcEndTime = Getdate() 
 
 PRINT '' 
 PRINT '-- ======================================================' 
 PRINT '-- = sp_WaSPs_TaxonomyImport' 
 PRINT '-- = RETURN VALUE' 
 PRINT '-- = QuitWithError' 
 PRINT '-- = @Error = ' + IsNull(Cast(@Error as varchar),'<NULL>') 
 PRINT '-- = @return_code = ' + IsNull(Cast(@return_code as varchar),'<NULL>') 
 PRINT '-- = EXECUTION TIME' 
 PRINT '-- = Start = ' + IsNull(Convert(varchar(24), @Debug_ProcStartTime , 113),'<NULL>') 
 PRINT '-- = End = ' + IsNull(Convert(varchar(24), @Debug_ProcEndTime , 113),'<NULL>') 
 PRINT '-- = Ellapsed Time (ms) = ' + IsNull(Cast(Datediff(millisecond, @Debug_ProcStartTime, @Debug_ProcEndTime) as varchar),'<NULL>') 
 PRINT '-- ======================================================' 
 PRINT '' 
 
END 
 
SET @ErrorMessage = 'SQL Error' 
 
SET @return_code = 2 
 
RETURN(2)

-------------------------------------------------- 
QuitWithParameterError: 
 
IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 SET @Debug_ProcEndTime = Getdate() 
 PRINT '' 
 PRINT '-- ======================================================' 
 PRINT '-- = sp_WaSPs_TaxonomyImport' 
 PRINT '-- = RETURN VALUE' 
 PRINT '-- = QuitWithParameterError' 
 PRINT '-- = @ErrorMessage = ' + IsNull(Cast(@ErrorMessage as varchar),'<NULL>') 
 PRINT '-- = EXECUTION TIME' 
 PRINT '-- = Start = ' + IsNull(Convert(varchar(24), @Debug_ProcStartTime , 113),'<NULL>') 
 PRINT '-- = End = ' + IsNull(Convert(varchar(24), @Debug_ProcEndTime , 113),'<NULL>') 
 PRINT '-- = Ellapsed Time (ms) = ' + IsNull(Cast(Datediff(millisecond, @Debug_ProcStartTime, @Debug_ProcEndTime) as varchar),'<NULL>') 
 PRINT '-- ======================================================' 
 PRINT '' 
 
END 
 
 
SET @return_code = 1 
 
RETURN(1) 
------------------------------------------------ 
QuitWithSuccess: 
 
IF ( IsNull(@Debug,0) = 1 ) -- (debug info) 
 
BEGIN 
 
 SET @Debug_ProcEndTime = Getdate() 
 
 PRINT '' 
 PRINT '-- ======================================================' 
 PRINT '-- = sp_WaSPs_TaxonomyImport' 
 PRINT '-- = RETURN VALUE' 
 PRINT '-- = QuitWithSuccess' 
 PRINT '-- = EXECUTION TIME' 
 PRINT '-- = Start = ' + IsNull(Convert(varchar(24), @Debug_ProcStartTime , 113),'<NULL>') 
 PRINT '-- = End = ' + IsNull(Convert(varchar(24), @Debug_ProcEndTime , 113),'<NULL>') 
 PRINT '-- = Ellapsed Time (ms) = ' + IsNull(Cast(Datediff(millisecond, @Debug_ProcStartTime, @Debug_ProcEndTime) as varchar),'<NULL>') 
 PRINT '-- ======================================================' 
 PRINT '' 
 
END 
 
SET @return_code = 0 
 
RETURN(0) 
-- -------------------------------------------------- 
END

StoredProcedure