Blog: .NET

Cheat Sheet: Best practices for writing XML documentation phrases in C#

March 11, 2015, (updated on September 16, 2015), 3 comments, Software Development

The XML documentation tags of C# are described very well in the MSDN. However, the article does not explain how the contained phrases and sentences should be written. This article tries to fill this gap by providing rules and some sample phrases. I recommend using StyleCop because its rules enforce some of the XML documentation recommendations from this article. Also check out the Visual Studio extension GhostDoc which automates and simplifies the writing of XML documentation. General All XML documentation phrases should end with a period (.) and no blank: <summary>Represents an XML document.</summary> public class XDocument { The summary […] Read more...

Tags: , , , , , ,

How to implement and register a custom dependency resolver in ASP.NET MVC 5

March 6, 2015, (updated on March 26, 2015), Software Development

The ASP.NET MVC 5 framework supports the integration of 3rd party dependency injection frameworks. This can be done by implementing the IDependencyResolver interface and registering an instance of the interface on application startup. The registration is made in the Application_Start method which can be found in the Global.asax.cs file: protected void Application_Start() { ... var compositionContext = new CompositionContext(); compositionContext.AddPart<IMyService, MyServiceImpl>(); // TODO: Register your own services here DependencyResolver.SetResolver(new MyDependencyResolver(compositionContext)); } Most dependency injection frameworks provide a NuGet package which already contains an IDependencyResolver implementation (for example Ninject.MVC3). However, because I wanted to use the IoC container from my own […] Read more...

Tags: , , , , , , ,

Elegant method parameter validation with Code Contracts support

February 25, 2015, (updated on February 26, 2015), 1 comment, Software Development

Each C# developer knows the drill: Each method parameter has to be validated against null values, wrong value ranges or other contract constraints. When also validating using Code Contracts, the resulting code may take up most of the method body: public void Foo(object a, string b) { Contract.Requires(a != null); Contract.Requires(b != null); Contract.Requires(b != string.Empty); if (a != null) throw new ArgumentNullException("a"); if (string.IsNullOrEmpty(b)) throw new ArgumentNullException("b"); ... } Note: When Code Contracts runtime validation is enabled, the call to Requires may throw an exception and the own code for throwing an ArgumentNullException is not needed. However, often the […] Read more...

Tags: , , , , , ,

A transparent undo/redo mechanism for XAML applications

February 23, 2015, (updated on February 25, 2017), Software Development

In one of my recent projects, I had to implement an undo/redo mechanism for an observable object graph. The usual approach to this problem is to implement commands which operate on the object graph and provide Do() and Undo() methods. The problem was, that a great portion of the business logic was already implemented and was not command-based. This is why I had to find a solution which works transparently on the property change events of an object graph whose objects implement the INotifyCollectionChanged interface and use the ObservableCollection class for collection properties. The solution to the problem was to […] Read more...

Tags: , , , , ,

Recommendations and best practices for implementing MVVM and XAML/.NET applications

January 30, 2015, (updated on April 11, 2017), 27 comments, Software Development

In this article I’ll describe the rules and practices I’m following for XAML and MVVM application development. I’m using the described techniques since multiple years and they have proven themselves for me. Most of the described rules apply for all types of XAML projects – Windows Store, Windows Phone, Silverlight and WPF projects. Sample code: The source code of the application Visual JSON Editor contains sample implementations for most of the discussed recommendations. Some sample code snippets in this article use the MVVM classes from the MyToolkit library which is developed by myself. Project file structure All MVVM/XAML applications should […] Read more...

Tags: , , , ,

Implementation of “hybrid” (console or GUI) .NET applicatios

December 29, 2014, (updated on October 19, 2015), 1 comment, Software Development

Have you ever had the need to use an application (.exe) as a command line tool when started in the console (cmd.exe) and as GUI application when started from the Explorer shell? This article explains a possible implementation for this requirement. To be clear: I didn’t find the perfect solution; a Windows .NET application is either a console application or a Windows application. Either type can be used in the opposite mode, but both variants have some drawbacks. This article describes the solution which I think has the least disadvantages. The solution works by defining the application as console application […] Read more...

Tags: , , , ,

The correct handling of a WCF client’s life cycle

December 21, 2014, (updated on February 25, 2015), Software Development

The most intuitive way of handling a generated WCF client’s life cycle is by putting it into a using block: using (var svc = new MyServiceClient()) { var result = await svc.SumAsync(1, 2); } Note: You should not use static or singleton client objects. However, the problem with this code is, that IDisposable is not implemented correctly in the base class ClientBase. The problem is that the Dispose method always calls Close but Close may throw an exception. If this exception is thrown an additional call to Abort is needed but which is not made. Missing this Abort call may […] Read more...

Tags: , , ,

How to create an MSI installer using the WiX toolset

November 25, 2014, (updated on December 20, 2014), Software Development

This article provides a simple guide about how to quickly create a WiX installer project for an existing WPF application or another Windows desktop application. Continue reading at my company’s blog… Read more...

Tags: , , , , ,

Write detailed C# XML documentation and still keep the source code clean

October 20, 2014, (updated on January 12, 2015), Software Development

For my open-source project MyToolkit I write a lot of documentation. Currently the documentation for a class has to be updated in two locations: The source code and the project’s wiki. I asked myself how to avoid this and document everything in the source code file – while keeping the source code simple by not putting too much documentation in it. To write detailed documentation in your C# code, you can use the tags <remarks> and <example> (<summary> should always be there). To avoid polluting the source code file with documentation, you can additionally use the <include> tag to “outsource” […] Read more...

Tags: , , ,

Use JSON files with schema for application configuration

September 12, 2014, (updated on February 25, 2017), 2 comments, Software Development

Often, we face the problem that our customers need highly configurable applications but there is no time to develop rich configuration GUIs. This article describes a possible solution which is a compromise between editing raw configuration files and implementing rich configuration GUIs. The idea: First, implement some simple POCO classes (Plain Old CLR Object) representing the structure of the required configuration. Using a JSON serializer these classes can then be written and read from a JSON configuration file. Additionally, store an auto-generated JSON schema file alongside the configuration file. To edit the configuration file after its creation, use the tool […] Read more...

Tags: , , , ,