Showing posts with label Best Practices. Show all posts
Showing posts with label Best Practices. Show all posts

2013-08-09

Mocking an internal interface with Moq

If by some reason you need to Mock an internal interface using Moq, you might get the infamous exception while running your unit tests:
System.TypeLoadException : Type 'ISomething`1ProxyRandomGuid' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=YetAnotherGuid' is attempting to implement an inaccessible interface.

Assuming that you already added the InternalsVisibleTo from your library to your unit test project, you'll also need to add an InternalsVisibleTo to DynamicProxyGenAssembly2. However, unlike the author in the blog post, in my machine it only works if I remove the PublicKey, something like:

[assembly:InternalsVisibleTo("DynamicProxyGenAssembly2")] 

2013-05-14

Parsing ISO 8601 periods

If you have to deal with ISO 8601 duration periods, such as "P1Y2M10DT2H30M", you can just parse it to a TimeSpan using the XmlConvert.ToTimeSpan() method. I'm not sure why this is isn't supported by TimeSpan directly, so thanks to Stack Overflow.

2012-10-29

Forcing a Culture in the business logic

Not exactly a new topic, but a reminder. In my opinion, it's a good practise for your application business logic not to assume that it will be run in a particular Windows configuration, namely a specific culture. An "innocent" ToString() or Parse() method call in a double or DateTime struct is all that takes to break your code. Last week that happened at the office, some of our unit tests were only failing on some development machines. It turned out that some of team members have Windows configured to use GB English regional settings, while others have been using PT Portuguese settings. One approach to fix this is to force a culture in all calls of these methods (US English, for instance), but I think that is boring and easy to overlook. I prefer to force the culture for all calls, something like:

Thread.CurrentThread.CurrentCulture = 
   new CultureInfo("en-US");

 Here's how I have been doing it, depending on the component type.

WCF Service

Usually, I would put the code line in the Application_Start() method of the Global.asax class, since it would apply to every service in the project. However, the last time I've did it, we were using Castle Windsor for dependency injection, and this doesn't seem to work, as the container instantiates the service object in another thread. Solution: put the line in the service constructor.

Since we are also using ThreadPool for some of the tasks, I also had to add the line to the callback method.

Yes, I could also have used the <globalization> tag in the web.config, but that would imply activating ASP.NET compatibility in WCF, which I didn't want to (no problem on doing so, but I just wanted to avoid unnecessary entropy).

Console Application / Windows Service

Usually, I would just put the line in the Program class Main method, but since we are using TopShelf to abstract some of the Windows service boilerplate code, I had to put it elsewhere. Basically, in the WhenStarted() callback.

NUnit Test

Unit tests are basically class libraries, so there is no central point method. The best I came up with was putting the code in the StartUp method, for every unit test class. Since I'm being lazy, in reality I've only added it in every test class that was failing because of culture settings.

2012-09-07

SessionState and ViewState in ASP.NET

Fine tuning ASP.NET applications is a very extensive topic. However, some features are enabled and may not be needed. For example, Session and ViewState are not really needed for simple pages (i.e. if your ASP.NET page is basically hosting a single page application in Silverlight). To do this, just open the .aspx (markup) file and add the last two attributes:

<%@ Page Language="c#" AutoEventWireup="true" CodeBehind="Foo.aspx.cs" Inherits="Acme.Foobar" EnableSessionState="false" EnableViewState="false" %>

Note: this is basically a repost from my previous blog.

2008-12-21

Patterns & Practices: Application Architecture Guide 2.0

This week the book Application Architecture Guide 2.0 - Designing Applications on the .NET platform was released.



This book is from the Patterns & Practices (P&P) team and is edited by Microsoft Press. It is available for free download. I have known that the P&P team has developed some interesting work on software engineering, by I haven't found the time to actually read something from them. This book might be my chance to do so. I opened it up a little bit and found a foreword from non other than Scott Guthrie, a very active engineer in the .NET community.

By the way, some previous reading material from P&P include two interesting topics: Performance Testing Guidance for Web Applications and Team Development with Visual Studio Team Foundation Server.

Until next time, may you enjoy some free reading.