Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

A Simple Aspect for NHibernate and the virtual keyword

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
14 Sep 2008BSD2 min read 33.2K   154   14   6
An article that shows how to use a simple Aspect to automatically make class members virtual.

Introduction

People who use NHibernate know that in order to take advantage of its lazy-loading facility, all public members of the persisted classes must be marked as virtual. Compared to all the benefits the lazy-loading brings, this seems to be a very insignificant price. However, for some people, including me, this extra virtual keyword is still some annoyance. It is very easy to forget about it when writing a new code, it makes Resharper complain sometimes ("virtual member call in constructor" warning), and it is ugly (subjective one, I know). In this article, I would like to show how to solve these issues with a simple Aspect that makes members virtual even if the keyword is not used in the source code.

Background

The solution presented in this article is most useful to people using NHibernate (also indirectly via Active Record). If you don't know what NHibernate is, I would recommend checking it out here.

Using the Aspect

In order to use the Aspect, you will first need to download and install PostSharp. The remaining part of the article assumes that PostSharp is installed with the MSBuild integration enabled.

Download Unsealer, and copy the Unsealer.Weaver.dll and Unsealer.psplugin files into the Plugins directory under your PostSharp installation folder. Copy the Unsealer.dll into the folder where you keep the external libraries for your solution.

Open Visual Studio, and add the following references to the project that contains your persistent classes:

  • PostSharp.Public.dll
  • Unsealer.dll

At this point, Unsealer is enabled on the project. Now, you can either:

  • Decorate every persistent class with the [Unseal] attribute, or
  • Use a shortcut, and add the:
  • C#
    [assembly: Unseal(AttributeTargetTypes = "*")]

    line to the AssemblyInfo.cs file. This will allow you to forget about the virtual at all.

This is it - the keyword can be removed from the classes. NHibernate will continue to run as if nothing changed, and if you inspect the DLL with Reflector, you will see that all public members are virtual.

Notes

Although the name of the Aspect is Unsealer, it does not remove the sealed keyword from the methods. Actually, it causes the build to fail if the [Unseal] attribute is used on a class that has sealed members (or is sealed itself). This is because I assume that the sealed keyword is always used on purpose - as opposed to virtual, which may be skipped accidentally.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralPostSharp 1.5 update Pin
ogauchard24-Sep-09 2:19
ogauchard24-Sep-09 2:19 
GeneralRe: PostSharp 1.5 update Pin
Alexander Arques13-Jun-14 3:14
Alexander Arques13-Jun-14 3:14 
GeneralGreat Find! Pin
DamonCarr6-Oct-08 1:42
DamonCarr6-Oct-08 1:42 
GeneralVery often i implement interfaces and don't want/don't need the virtual there, how are you handling it Pin
Tavo Ringel16-Sep-08 0:38
Tavo Ringel16-Sep-08 0:38 
GeneralRe: Very often i implement interfaces and don't want/don't need the virtual there, how are you handling it Pin
Brendan Kowitz16-Sep-08 11:34
Brendan Kowitz16-Sep-08 11:34 
GeneralRe: Very often i implement interfaces and don't want/don't need the virtual there, how are you handling it Pin
Gael Fraiteur17-Oct-08 9:04
Gael Fraiteur17-Oct-08 9:04 
Interface implementations are *always* virtual, btw.

Gael Fraiteur
Lead developer of PostSharp, a platform for AOP on .NET.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.