Skip to main content


Showing posts from October, 2009

12 ASP.NET MVC best practices

People keep posting their best practices about ASP.NET MVC usage . Simone Chiaretta made a presentation about the stuff he finds important when using ASP.NET MVC. Maybe it becomes time that I post my list of best practices? Will be continued...

NHibernate knol

As a frequent user of NHibernate, I find it a little bit disappointing that there's only one book out there, although it's a very good one. Luckily for us, Fabio Maulo did a great job creating a knol about NHibernate . A must read for every NHibernate lover!

Configure an MSBuild script to read file data

Today I had to configure an MSBuild script to read some data from a file. First I was looking for a custom task to achieve this goal. But I found that this functionality is out-of-the-box available in MSBuild. You can read from a file into a variable using: 1: <ReadLinesFromFile File= "c:\ReleaseNumber.txt" > 2: <Output TaskParameter= "Lines" 3: ItemName= "ReleaseNumber" /> 4: </ReadLinesFromFile> This will store the contents of the file "ReleaseNumber.txt" in a  property "ReleaseNumber". You can then use this property in other places inside your build file: 1: "@(ReleaseNumber)"   You can also write to files: 1: <WriteLinesToFile File= "Log.txt" Lines= "This is a log value." Overwrite= "false" />

Improving type safety for INotifyPropertyChanged without expressions

Type safety is important. Isn’t it one of the reasons why you should use a static typed language like C#?  But if you’re using the INotifyPropertyChanged interface, you allready have left that type safety. Why? Because it expects you to pass on the propertyname. Easiest way to do this is just using a string parameter. 1: public event PropertyChangedEventHandler PropertyChanged; 2:   3: private void NotifyPropertyChanged(String propertyName) 4: { 5: if (PropertyChanged != null ) 6: { 7: PropertyChanged( this , new PropertyChangedEventArgs(propertyName)); 8: } 9: } But what if you refactor your code and change the name of the property? The string value will remain the same and the compiler will not warn you. But your code will no longer work. A solution could be the use of expression to pass on the propertyname in a type safe way. 1: protected void NotifyPropertyChanged(Expression<Func&l

VSTO here I come

For one of my current clients, I’ll have to start looking at the  Microsoft Visual Studio Tools for the Microsoft Office System(VSTO). I used to work with macro’s and the COM API’s for Office before. It was not really my best experience , so I was not too eager to start using VSTO. How could I be more wrong? It’s amazing how good these tools are integrated in the Office system. I’m really starting to like it. Some invaluable resources that helped me a lot: the VSTO team blog . Microsoft Visual Studio Tools for the Office System Power Tools v1.0.0.0 Office development with Visual Studio Portal Expect to get a lot more posts about VSTO in the near future.

ReportViewer got an upgrade in Visual Studio 2010

Microsoft plans to release significantly updated and improved versions of both the Winforms and ASP.NET Report Viewer controls in Visual Studio 2010 .  This is going to include the long awaited local mode support for the new report processing engine , originally released with SQL Server Reporting Services 2008.  Most importantly, this provides RDL 2008 features (e.g. tablix, enhanced charts, gauge, rich text) in local mode without connection to a report server.  If you wanted to use RDL 2008 features with the report viewer controls available before, server mode was your only viable option, because report processing is performed remotely on a Reporting Services 2008 server.  Below are some of the other new features or changes who are coming as well. Support for the 2008 RDL schema in local mode.  This will give you all of the new features available in RDL in SQL Server 2008, including tablix, rich text, updated chart visualizations, gauge, and many others.  The updated report d

C# and it’s lifecycle

Everything becomes obsolete sooner or later. The same thing counts for programming languages.  The lifecycle of a programming language can be divided in 7 phases: Conception Adoption Acceptance Maturation Inefficiency Deprecation Decay Scot Allen is starting an in interesting discussion about the language phase where C# is today. Can things only get worse after C# 4.0? Or will there still be some interesting evolutions for the C# programming language? Maybe the dynamic keyword is just a sign for what will be next…

VSTS 2010 Beta 2 Training Kit released

The Beta 2 version of Visual Studio 2010 Training Kit is now live. This training kit includes presentations, hands-on labs, and demos. This content is designed to help you learn how to utilize a variety of Visual Studio 2010 and .NET Framework 4 technologies. The Beta 2 release of the Training Kit contains 15 presentations, 19 hands-on labs, and 13 demos. Many technologies are covered in this release, including: C# 4, VB 10, F#, Parallel Extensions, Windows Communication Foundation, Windows Workflow, Windows Presentation Foundation, ASP.NET 4, Entity Framework, ADO.NET Data Services, Managed Extensibility Framework, and Visual Studio Ultimate.

Laws of dependency injection

As a heavy user of dependency injection, I’m always careful when applying it. “If you have a hammer, everything looks like a nail.”   Nikola Malovic mentions on his blog the following laws about the usage of an IOC container. Nikola’s 5 laws of IoC: “Store in IoC container only services. Do not store any entities.” "Any class having more then 3 dependencies should be questioned for SRP violation" “Every dependency of the class has to be presented in a transparent manner in a class constructor.” “Every constructor of a class being resolved should not have any implementation other then accepting a set of its own dependencies.” “IoC container should be explicitly used only in Bootstrapper. Any other “IoC enabled” code (including the unit tests) should be completely agnostic about the existence of IoC container." Although I don’t agree with all of his laws, he makes some good points.

Generic Comparer

A requirement in most of our applications is a default sorting for our codetable data. For example if you want to populate a combobox with all the countries in the world, it must be sorted by default in alphabetical order. I heard my colleagues discussing about it during one of our whiteboard design meetings. Possibilities passed by from using a custom attribute to a metadata class. While they were discussing I build the following solution; a generic comparer. Specify the properties to sort on using expressions and pass it on to the Sort method. No direct coupling between a class and it's sorting, type safety and extensible. What do you need more? The implementation of the Comparer is very simple, it just iterate over the expressions array and compares each property in both objects:  1: public class GenericComparer<T> : IComparer<T> 2: { 3: private readonly Func<T, IComparable>[] _expressions; 4:   5: public GenericComparer

Database Change Notification in Oracle

The SQL Server driver for ADO.NET has the SqlDependency class. It enables applications to receive notifications when there is a change in a query result set, schema objects, or the state of the database. Using SqlDependency, an application can maintain the validity of the client-side cache easily. At one of my clients we aren't using SQL Server but Oracle. As I searched throught the standard ADO.NET driver for Oracle (System.Data.Oracle) I couldn't find a similar class. After some digging on the Oracle site I found that Oracle has a separate driver for .NET next to the standard ADO.NET driver. If you download the Oracle Data Provider for .NET (Oracle.DataAccess) you'll find the OracleDependency class that has the same functionality as the SqlDependency class. A simple example to show this functionality: 1: public class Notification 2: { 3: public static bool IsNotified = false ; 4: 5: public static void Main( string

How to Run Programs as a Domain User from a Non-domain Computer

As a consultant I spend my days in multiple companies. Given that I work for multiple clients, I can’t join my laptop to any particular client’s domain. In most cases, not being joined to a client’s domain doesn’t make one iota of difference. You need to access a network share or printer, browser to it and you will be prompted for domain credentials. But if you have to connect for example to a SQL Server Instance through the Management Studio, you cannot enter domain credentials. I found this blog post from James Kovacs where he found a solution to access SQL Server Management Studio using a domain account. Nice!

Why can't I edit my WinForms control?

Last week, one of my developers came to me with a strange question. He had to change one of our existing WinForms views. There was one label that he couldn't edit. When he tried to access this control in code, the Intellisense would not show up. I came by to have a look. As I found out, a control has a GenerateMember property. If you change this property to false, the control code is generated by the designer as a local member of the InitializeComponent() method and therefore is not accessable outside the context of this method. And that was exactly the problem he had. He accidently changed the default value from true to false. By changing it back, a class level member is generated and he could access the control again from other places.

Enable code coverage on your TFS Build server

One of the nice features of Visual Studio is the integrated code coverage. It gives you a nice view how much of your code is covered by (unit) tests. After enabling this feature on my local Visual Studio environment, I noticed that this setting was not automatically duplicated to our build environment. You have one extra step to enable the code coverage functionality on your build server. Add the following line in TFSBuild.proj after <RunConfigAnalysis> tag. 1: < RunConfigFile > $(SolutionRoot)\YourAppDir\LocalTestRun.testrunconfig </ RunConfigFile > By default, the Microsoft.TeamFoundation.Build.targets files defines the SolutionRoot property as: SolutionRoot = $(BuildDirectoryPath)\$(TeamProject)\$(TeamBuild)\BuildType\..\Sources So if you have created a new Build Type under a team project named TestProject and set the New Build Type Wizard’s values to the following: Team Build Name = CI Build BuildPath = C:\Build the resulting final SolutionRoot property wou

Using the HelpProvider to show an HTML help page

In .NET WinForms a HelpProvider component is available to let you add help functionality to your application. While I tried to use this component in one of my applications, I noticed it is poorly documented. I hope this post can help you get started. The HelpProvider HelpProvider is an Expander Provider class that contains a number of hashtables to externally add help related properties to windows forms controls. The HelpNamespace string property identifies a help system and must be set to use HelpKeyword/HelpNavigator. It is NOT a namespace. It can be a uri or local file name or possibly other things as well. The four directly manipulated hashtable properties are: ShowHelp – a Boolean flag that enables the display of help through the HelpProvider. HelpString – a string to display in a pop-up window. Typically activated by clicking on the control when the cursor is a “?” or when the control has focus and HelpNamespace is null. HelpKeyword – a string to identify the help request pass

Diving into the StoryTeller trunk

Björn Rochel has written a series of blog posts on elements of the StoryTeller code base. If you want to learn more about presentation patterns, autowiring and UI management have a look. Diving into the StoryTeller trunk, Part 9: ScreenSubject & ScreenFactory Diving into the StoryTeller trunk, Part 8: The screencollection Diving into the StoryTeller trunk, Part 7: Screens Diving into the StoryTeller trunk, Part 6: The main players in the screen activation lifecycle Diving into the StoryTeller trunk, Part 5: The EventAggregator Diving into the StoryTeller trunk, Part 4: Registration of Generics Diving into the StoryTeller trunk, Part 3: The INeedBuildUp convention Diving into the StoryTeller trunk, Part 2: The IStartable convention Diving into the StoryTeller trunk, Part 1: Convention based registration

Limiting the users in the Assigned To field on a Work Item

By default, the Assigned To field on a work item shows all users known to TFS (members of the TFS Everyone group). Most of the time you want to limit this list to the users of a specific Team Project. To do this export the work item type from your project with the WITExport tool. If you want to assign work items to groups, replace the definition of Assigned To with the following XML. This expands all users and groups defined in the project contributors group in the Assigned To pick list. 1: < FIELD name ="Assigned To" refname ="System.AssignedTo" type ="String" > 2: < ALLOWEDVALUES expanditems ="true" > 3: < LISTITEM value = "[Project]\Contributors" /> 4: </ ALLOWEDVALUES > 5: </ FIELD > Afterwards you can import the changed workitem definition by using the WITImport tool.

Why project managers are underpaid?

Last week, Mitch Lacey was in Belgium to deliver a Scrum training at Ordina . The last day of this training I had the opportunity to give a session about the role of a project manager in a Scrum project. If you're interested in the slidedeck, it's available here .

TDD for Dummies

As I was reading through some old stuff to prepare a presentation, I found this old blog post of Jeremy Miller about TDD. He really was cranky the day he wrote it, but the message he wants to deliver has a lot of value.

How do we get ClickOnce working with Team Build?

By default, the Team Build server will only compile your assemblies. Creating the necessary manifests, application and setup files necessary to deploy a clickonce application is not executed by default. Enabling this feature is very easy. Edit the TFSBuild.proj file and copy the “SolutionToBuild” line, and put it below, and rename it to “SolutionToPublish”. 1: < SolutionToPublish Include ="$(BuildProjectFolderPath)/Source/MySolution.sln" > 2: < Targets ></ Targets > 3: < Properties ></ Properties > 4: </ SolutionToPublish > Team Build will then build the code and place a versioned ClickOnce directory in the drop folder that you can then copy over to your deployment location.

Hosting an ASP.NET 3.5 website on IIS 7

Although using IIS is not that hard, I had some issues to get my website deployed. As I didn't install the webserver, it took me a while to find out which features were missing. The following steps can help you checking if your server is ASP.NET 3.5 ready: Install the Web Server Role In Role Services, select ASP.NET and continue. Install the .NET 3.5 Framework On your webserver, install the .NET 3.5 framework . Publish Your Application In Visual Studio, publish your web project to a local folder, like on the desktop. Copy that folder to a local folder on the webserver, maybe somewhere like c:\apps. From the IIS Manager, right click your Default Web Site and "Add Application". Enable ASP.NET Impersonation on your Website IIS -> Your website -> Authentication -> Enable ASP.NET Impersonation. Test your Application Open a browser, and try http://localhost, verify that

97 things every programmer should know

After 97 Things Every Architect Should Know the 97 Things series continues with the things that every programmer should know. The Things are collected in a wiki , and everybody is welcome to contribute and comment on the entries.

12 Practices to be a Better Developer

The 12 Practices Source Control - If you don’t store your code in source control, you’re putting your company at risk. Test-Driven Development produces higher-quality code. Behavior Driven Design - Behavior is documentation for other developers and users. Build Systems - Building is more than just compiling code. Continuous Integration - Check in early and often. Real Time Code Review - Two heads are better than one. Refactoring: Easy as red light green light. Read Code - "You don’t become a better writer by just writing." Scott Hanselman Code In Increments - Real programmers work for hours and hours straight, without a break, without even looking up. Don’t break your flow to stop and compile: just keep typing! :) Sharpen your skills by learning a new language. Learn SOLID Single Responsibility Principle - There should never be more than one reason for a class to change. Open Closed Principle - A class should be open

IP address restrictions in IIS

At one of my clients we wanted to prevent that test clients could call our production services. Thanks to the fact that we are using WCF services hosted in IIS, this could easily be accomplished. Open the  IIS Manager(InetMgr). Select the website where you want to apply the IP restrictions, right-click and select Properties. Go to the Directory Security Tab. Click on the Edit button in the IP address and domain name restrictions groupbox. Change the default configuration to  Denied access. Afterwards clickon on Add… to configure the range of IP addresses who connect to this website. Complete the correct subnet mask. Click 3 x Ok, and the changes are applied.

TFS Workflow Control

One of the nice features in Team System Web Access is the visualization of the workitem workflow. Team Explorer users had to miss this feature. Michel Perfetti just released a custom work item control that does exactly what TSWA does… Download the control from codeplex:

.NET 4.0 becomes lazy...

If you just think, you have discovered all the exciting new features in .NET 4.0, you always find some new bits of nice functionality. Lazy<T> is one of those features. It brings lazy loading to the core of the .NET framework. Suppose that you have an object that is very expensive to create and use. You don’t want to create it everytime your application runs. You only want to create it when you need it. Lazy<T> makes this easy.  You just create a lazy wrapper on the expensive object: 1: Lazy<ExpensiveResource> ownedResource = new Lazy<ExpensiveResource>(); To get the expensive object just use the ownedResource.Value property. The first time you access ownedResource.Value, the expensive resource will get allocated, but not before. And the API designers did a really good job and added two very useful constructions: First, Lazy<T> has an overloaded constructor that allows you to specify a Func<T>. This makes it possible to use a factory method to