Fake binary trees
... rants, ramblings and occasional good idea ...

Converting Pascal case to sentences using regular expression

Here's another reminder to myself: a nifty one-liner regex which transforms Pascal/camel case string into a sentence. I always write it from scratch so I decided to put it here. It might also be a good idea to write it as an extension method for string class.

static string PascalCaseToSentence(string input)
{
   return Regex.Replace(input, ".[A-Z]", m => m.ToString()[0] + " " + char.ToLower(m.ToString()[1]));
}

Following line

Console.Out.WriteLine(PascalCaseToSentence("MyVeryLongSentence"));

will result in:

My very long sentence
Posted at 22:56 on April 29, 2009
Categories: .NET   E-mail | del.icio.us | Permalink | Comments (0) | Post RSSRSS comment feed

Build automation issues wit NAnt and .NET 3.5

On our project, we are experimenting with continuous integration, and so we set up a build server based on CruiseControl.NET. The server is running on Windows Server 2008, and we try to keep it as clean as possible. Development machines typically run on Win XP.

The project tree contains tools folder which contains copy of NAnt and other tools which are used to run the build (NUnit, NCover, Simian, etc). This way, when a developer downloads project files to development machine, he just runs the build script and same version of tools is used to perform a local build, thus avoiding the risk of using mismatching version of tools. NAnt build script internally uses <msbuild> task to compile the msbuild solutions.

This has all worked great until we recently switched from .NET 2.0 to .NET 3.5. During this, our development machines were updated to Visual Studio 2008, and our build server was updated to 3.5 framework and SDK (we don't install Visual Studio on build server,so we have to install SDK separately after the .NET framework is installed).

This initially created issues with some of our build scripts: builds started failing with error messages pointing to the fact that <msbuild> task doesn't understand format of the solution file (new version is 10 and build complained about understanding only 7 and 9). After updating NAnt to latest version and testing on a dev machine, problem seemed to went away.

However, builds on our CI build server started failing with same error.

After some investigation, it turned out that Visual Studio 2008 installs version 6.0A of Windows SDK. However, standalone Windows SDK installs version 6.1, thus resulting in mismatch between build server and dev machines.

The heart of the problem is that nant.exe.config internally uses ${sdkInstallRoot} variable which is initialized to the value read from the registry key SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.0A\WinSDKNetFxTools\InstallationFolder. Now, we couldn't just change the entry in nant.exe.config because the same file is used both on server and dev machines, and if we fix it for one it won't work for the other.

We also didn't want to install Visual Studio 2008 on build server.

The solution (or more precisely, the hack) was simple:

  • Open regedit
  • Go to 'SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.1\WinSDKNetFxTools\InstallationFolder' key
  • Export it into a file
  • Open the file and replace all references in key names from "v6.1" into "v6.0A". Leave the file paths with 'v6.1' unchanged.
  • Save the file and import it into regedit

Now you have the exact copy of the SDK 6.1 keys, saved under 6.0A key. This way, build server can use the same nant.exe.config file as dev machines.

If there is a cleaner way, I would be glad to hear it, but this is working for us without any problems.

Posted at 14:03 on September 24, 2008
Categories: .NET | Continuous Integration | NAnt   E-mail | del.icio.us | Permalink | Comments (0) | Post RSSRSS comment feed

Using timestamps in batch files

After failing many times before, I have finally found out how to create user-friendly timestamps in batch scripts. For many people this may be yesterday's news, but I am posting it here as a reminder to myself and in hope that it might help someone else.

Windows command shell (sometimes erroneously called DOS prompt) provides %DATE% and %TIME% environment variables which, unsurprisingly, return current date and time. On the other hand, it is possible to extract a group of characters from any environment variable using following syntax:

%VARIABLE:~START,LENGTH%

where VARIABLE is the name of the environment variable, START is the zero-based index of the first character to be retrieved and LENGTH is the length of the string to be retrieved. E.g. %USERNAME:~1,3% would return second, third and fourth letter of the current user's name.

So in order to create a time stamp, we define a temporary environment variable with value defined by parts of %DATE%  and %TIME%. Then we use this variable to create file names. Please note that the exact indices will depend on your time format settings. In a batch file or a script it would look like this:

set backup_time=%date:~10,4%_%date:~7,2%_%date:~4,2%
set backup_detailed=%date:~10,4%_%date:~7,2%_%date:~4,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%
md d:\zzz_%backup_time%
md d:\zzz_%backup_detailed%

Output of the batch file is:

D:\tmp>set backup_time=2008_09_23
D:\tmp>set backup_detailed=2008_09_23__15_50_31
D:\tmp>md d:\zzz_2008_09_23
D:\tmp>md d:\zzz_2008_09_23__15_50_31
Posted at 15:46 on September 23, 2008
Categories: General   E-mail | del.icio.us | Permalink | Comments (0) | Post RSSRSS comment feed

Using assembly resources as NVelocity templates

For those who don't already know, NVelocity is a .NET port of Jakarta Velocity template engine. Castle team took it over, due to the lack of releases, support and bug fixes on the original port, and I strongly suggest to use this improved version.

NVelocity is a fine tool, but it is not quite obvious how to set it up so that embedded assembly resources can be used as templates. This is done by setting a group of properties which control what implementation of ResourceLoader abstract class is used to load the templates. To use embedded resources, you need to register the NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader class as ResourceLoader, and specify the assembly which contains the templates as parameter "assembly.resource.loader.assembly". Here's how: 

private static void InitializeVelocity()
{
  ExtendedProperties properties = new ExtendedProperties();

  properties.AddProperty("resource.loader", "assembly");
  properties.AddProperty("assembly.resource.loader.class",
          "NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader, NVelocity");
  properties.AddProperty("assembly.resource.loader.assembly",
          Assembly.GetExecutingAssembly().GetName().Name);
     
  m_velocity = new VelocityEngine(properties);
}

You can find the full sample here: NVelocitySample.zip (87.13 kb)

Posted at 18:34 on September 19, 2008
Categories: .NET | NVelocity   E-mail | del.icio.us | Permalink | Comments (0) | Post RSSRSS comment feed

C#-like events in C++

Since most of my daily work is done in C++,  every now and then I miss some niceties which are available in C#. One such example are .NET events, which are really an elegant solution for implementing observer design pattern.

Idiomatic C++ implementations of observer pattern are based on defining abstract interfaces, which have to be implemented by subject and observer classes. Sometimes it would be nicer to bind to an event just based on event/method signature instead of building all the needed scaffolding.

Here is a quick take at this problem. This is by no means a complete solution, but can be a basis for more elaborate one.

Events are defined and fired like this:

class Subject
{
public:
    event<int> ValueChanged; // same template is used, regardless of number of arguments
    event<int, const char*> ValueAndNameChanged;  // no need for having event1<A> and event2<A, B>

    void SetValue(int x)
    {
        // .. do something
        ValueChanged(x); // then fire ValueChanged event
    }

    void SetValueAndName(int x, const string& name)
    {
        // do something...
        ValueAndNameChanged(x, name.c_str()); // fire ValueAndNameChanged event
    }
};

Clients consume events like this:

// implementation of observer object
class Observer
{
public:
    void OnValueChanged(int i)
    {
        printf(" - non-static handler for SetValue called, new value = %ld\n", i);
    }

    static void OnValueChangedStatic(int i)
    {
        printf(" - static handler for SetValue called, new value = %ld\n", i);
    }
};

// global functions work, too:
void OnValueChangedGlobal(int i)
{
    printf(" - global handler for SetValue called, new value = %ld\n", i);
}

// test
int _tmain(int argc, _TCHAR* argv[])
{
    Subject subject;
    Observer observer;

    subject.ValueChanged += Observer::OnValueChangedStatic; // static member method as handler
    subject.ValueChanged += OnValueChangedGlobal; // global function as handler
    subject.ValueChanged += event_handler(&observer, &Observer::OnValueChanged); // non-static member

    subject.SetValue(3); // this will fire the event

    // detach from event. This is not necessary when lifetime of observer is shorter than lifetime
    // of the subject, but if subject outlives observer, handler must be unregistered
    subject.ValueChanged -= Observer::OnValueChangedStatic;
    subject.ValueChanged -= OnValueChangedGlobal;
    subject.ValueChanged -= event_handler(&observer, &Observer::OnValueChanged);

    return 0;
}

Output of this snippet is:

d:\dev\events>events.exe
  - static handler for SetValue called, new value = 3
  - global handler for SetValue called, new value = 3
  - non-static handler for SetValue called, new value = 3

It is possible to use static and non-static members as handlers, as well as global functions. Note, however, that there is one difference compared to event behavior in .NET: if you register same handler twice, it will be called only once. This can easily be changed in the code, but it seems more logical to me.            

Attached archive contains a Visual Studio solution with implementation of event class (events.h) and some additional examples. 

events.zip (5.14 kb)

Posted at 16:12 on April 29, 2008
Categories: C++   E-mail | del.icio.us | Permalink | Comments (2) | Post RSSRSS comment feed

Automatic memory management myth

At a recent interview, a job candidate ticked me off when we reached one of the topics which is very dear to my heart. We are mostly C++ shop, but many of our job applicants come from .NET background. So during the interview we came to the topic of explicit memory management vs. automatic memory management (a.k.a. garbage collection), and the guy (with substantial C++ experience) started ranting about how troublesome explicit memory management is, with all this extra caution required to call delete for every new, as opposed to simplicity of platforms which support GC.

Well, if you happen to agree with this guy, then you are not doing it right :)

Although GC can be done in C++ (see here), that is not the point: the main issue here is that many people are still doing C code using C++ compiler.

Modern C++ is very different beast from C, and as such provides different patterns for common problems. The concept of smart pointers alleviates much of the manual work that is otherwise needed to handle allocations correctly. The STL comes with std::auto_ptr, which makes it trivial to ensure the proper deallocation in face of exceptions or normal scope exit. 

If you need to handle an array, STL is your friend again: there is std::vector, and it also takes care of transparent resizing/reallocation, so you don't have to worry about it.

What is (currently) lacking in STL is an implementation of smart pointer with shared semantics. However, there is plenty of such implementations, most notably the excellent boost::shared_ptr, which is a reference counted implementation that makes sure that the allocated object is deleted when the last reference to it goes away. It will also become a part of new C++ standard, and some vendors support it already.

One issue where GC (at least when implemented with mark-and-sweep algorithm, like in Java or .NET) has an advantage to explicit memory management is the problem of circular references, i.e. when two objects hold a reference to each other. This is really a problem for reference counted implementations, but most cases can be handled by judiciously using weak pointers, and such an implementation is provided by boost::weak_ptr, which will also be included in new C++ standard.

If you think that this problem is a 'deal breaker' to prefer platforms with GC, bear in mind that the similar problem also exists even there, and that there is a very good reason why Java and .NET both provide WeakReference class (e.g. see here for one such problem).

What I really find annoying is the fact that the only resource which is deemed to be important enough to be automatically handled is memory. For everything else, like database connections, kernel or GDI objects, clients have to explicitly call Dispose, and it seems that most developers on managed platforms have no problems with that. A coworker who recently switched from VB to .NET found out hard way that you really have to call Dispose() on your bitmaps. What happens when you have to share such an object between multiple clients (yes, I know that there are idioms to avoid this resource sharing)? Who calls Dispose? Can you be sure that it has not already been disposed? 

GC is a nice and useful thing, but it is not a silver bullet, and has its own problems.

Deterministic finalization is what makes it possible in C++ to treat all resources equally: whether it is a memory, a bitmap, a COM object or database reference, with a simple wrapper around it, you can rest assured that the object will be properly released as soon as it is not referenced anymore. Actually, smart pointers are only a special case of what is one of the most powerful concepts in C++, although a bit unfortunately named: RAII

If you are a C++ programmer, take some time to learn C++ idioms. It will make you a better programmer, and your code a better code.

When you program in C++, write C++, not C.

Posted at 14:32 on April 17, 2008
Categories: C++ | Software Design   E-mail | del.icio.us | Permalink | Comments (0) | Post RSSRSS comment feed

Access control based security

Natural question to ask after previous post is: that's all fine and dandy but how do you combine this with access control list (ACL) based security?

First, let's explain the issue here: What I refer to as 'ACL based security' is defining permissions (access rights) for individual resources, similar to the way operating systems allow access to file system. E.g. user 'xy' can see all tasks for projects he manages, but also all other tasks in other projects where their managers have allowed access to 'xy', or tasks which are assigned to 'xy'. This changes our imaginary security API from HasPermission(user, permission) to HasPermissionFor(user, permission, object)

Although security is usually not considered a business logic, the line starts to get blurry here. In my opinion, this is both a business and infrastructure concept.

One possible solution, which unfortunatelly can be seen too often, is to retrieve data as usual and then throw away resources which don't match the permissions. This approach fails miserably in many aspects: performance, filtering, paging, etc.

I am not sure if it is even possible to create a 'one size fits all' solution for this problem. However, in most systems that I had to deal with, following solution was able to get me quite far.

Note: I presume that the infrastructure is already set up: additional database tables store ACL entries which define who is allowed or denied access to individual resources, so that queries which retrieve items from the database can join on these tables. This is not a trivial thing and can become quite complex, especially when you take into account resource hierarchies (e.g. project-task), but it is out of scope of this post.

Anyway, suppose that we have ITaskManagementService which exposes following method:

[RequiresPermission(Permission.Edit)]
void GetTasks( ... parameters...)
{
    // m_taskRepository is an instance of ISecureRepository<T>
    m_taskRepository.GetAll(); 
}

Service has SecurityInterceptor implemented through Windsor/DP2, which checks for RequiresPermission attribute and do something like:

class SecurityInterceptor : IMethodInterceptor
{
    public object Intercept(IMethodInvocation invocation, params object[] args)
    {
            CallContext[Context.Security] = new SecurityContext(CurrentUser, attr.Permission);         
            return invocation.Proceed(args);
    }
}

than in the SecureRepository implementation, permissions are added to the query:

public IList<T> GetAll()
{
    ICriteria criteria = BuildCriteria();
    return criteria.List<T>();
}

public void BuildCriteria<T>()
{
    ICriteria criteria = Session.CreateCriteria(typeof(T));
    SecurityContext security = CallContext[Context.Security];
    // now we modify criteria to join entity tables with ACL tables...
    AddPermissions(criteria, security.UserId, security.RequiredPermissions);
}

This will make sure that GetAll() method returns only those tasks for which the caller has sufficient permissions. The drawback of the solution is that it only works if the resources and permissions are stored in tables in the same database so you can join them, but this usually isn't an issue for most small to medium solution.

Posted at 18:01 on February 6, 2008
Categories: .NET | Software Design   E-mail | del.icio.us | Permalink | Comments (0) | Post RSSRSS comment feed

Implementing security (and other cross-cutting concerns) through AOP

A common problem with implementing security is that you end up with bunch of repeated code blocks which check the current user's permissions and then allow or disallow the execution of some method. Example:

public class OrderManagementService : IOrderManagementService
{
    public Guid CreateOrder(string orderCode)
    {   
        if(!CallContext.CurrentUser.HasPermission(Permissions.CanCreateOrder))
        {
            throw new SecurityException("Only users with CanCreateOrder permission can create an order.");
        }

        Order order = new Order(orderCode);
        order.Save(order); // ActiveRecord-like implementation
        return order.Id;
    }
}

The problem with this approach is that you have to implement the check in every method which requires some combination of permissions. While this is not overly hard to do, it becomes a maintenance hell as the number of such methods grows. Also, if you want to change the behavior in case of missing permissions you have to modify all those functions. Of course, you can encapsulate it in some common utility method, like this:

    ...
    if(!CallContext.CurrentUser.HasPermission(Permissions.CanCreateOrder))
    {
        HandleMissingPermissions("Only users with CanCreateOrder permission can create an order.");
    }
    ...

This is also not perfect: If you decide that you need more context info in the utility method (e.g. required permissions or method name) you have to modify it:

    ...
    if(!CallContext.CurrentUser.HasPermission(Permissions.CanCreateOrder))
    {
        HandleMissingPermissions("CreateOrder", Permissions.Admin, "Only users with CanCreateOrder permission can create an order.");
    }
    ...

Now you have to modify all calls to HandleMissingPermissions method, etc.

The issue with application security is that it is a cross-cutting concern: it applies to all parts of system and not to a specific context, therefore, it doesn't make sense to implement it at each point where it is needed. In other words it is an application aspect (in AOP sense), and it is often best implemented in such manner.

There are many ways to implement AOP in .NET world: (Aspect#, NAspect), IL weaving (PostSharp) etc. In my opinion, one of the easiest is using interception features which are provided by some IOC container (e.g. Windsor and Spring.NET have it, StructureMap added it recently). In this example, I will use Windsor, because I am more familiar with it than with the others. For those who somehow missed it, Windsor is a quite popular IOC library, which is a part of Castle, an open source set of tools for easier development of enterprise and web applications.

So, here is how we are going to implement security:

  • We are going to define security requirements for each method using attributes
  • Instances of the target service (IOrderManagementService) will be retreived through the IOC container
  • IOC container will inject the security interceptors
  • Interceptor will check whether the method caller has required permissions


Our OrderManagementService class will now look like this:

[Interceptor(typeof(SecurityInterceptor))]
public class OrderManagementService : IOrderManagementService
{
    [RequiredPermission(Permissions.CanCreateOrder)]
    public virtual Guid CreateOrder(string orderCode)
    {   
        Order order = new Order(orderCode);
        order.Save(order); // ActiveRecord-like implementation
        return order.Id;
    }
}

OrderManagementService class is decorated with [Interceptor] attribute which defines the interceptor class which will be used to wrap the methods. You can define multiple interceptor classes, but usually it is better to do this in configuration file (see attached sample) than directly in the code, because you can switch the interceptors on/off without recompiling the code. This can be useful for debugging purposes or trouble-shooting.

Back to the implementation: security related logic is not checked in CreateOrder method anymore. Instead, SecurityInterceptor reads metadata for each method which is executed and according to RequiredPermission attribute of the method and permissions granted to the current user decides whether the method should be executed or not. Here is the implementation of the SecurityInterceptor:

class SecurityInterceptor : IMethodInterceptor
{
    public object Intercept(IMethodInvocation invocation, params object[] args)
    {
        MethodInfo method = invocation.Method;
        if(NeedsAuthorization(method) && GetRequiredPermission(method) != Context.Caller.Permission)
        {
            string message = string.Format("Method {0} requires {0} permission",
                                            method.Name,
                                            GetRequiredPermission(method));
            throw new SecurityException(message);           
        }

        return invocation.Proceed(args);
    }

    private bool NeedsAuthorization(MethodInfo method)
    {
        return method.IsDefined(typeof(RequiredPermission), true);   
    }

    private Permission GetRequiredPermission(MethodInfo method)
    {
         RequiredPermission attribute = (RequiredPermission)method.GetCustomAttributes(typeof(RequiredPermission), false)[0];
        return attribute.Permission;
    }
}

The only thing left to be done is to modify the client code to retrieve the IOrderManagementService instance via IOC container and call the method:

private void OnCreateOrderClicked(object sender, EventArgs e)
{
    // caller permissions have already been set
    IOrderManagementService svc = m_container.Resolve<IOrderManagementService>();   
    svc.CreateOrder("#ord-1-01");
}

This covers all the interesting parts of such implementation. Of course, in real-life scenarios this is not enough. The sample code is a bit more elaborated: you can define custom message for every RequiredPermission attribute instance, permissions can be combined etc.

The same approach can be used for other cross-cutting services, like logging and transaction handling.

You can download the sample here:  AopSecurity.zip (123.55 kb)

Posted at 12:31 on February 4, 2008
Categories: Castle.Net | .NET | AOP   E-mail | del.icio.us | Permalink | Comments (0) | Post RSSRSS comment feed

Otis 0.2 Released

I have just uploaded version 0.2 of Otis library. It supports a few handy features: aggregate functions, simplified string expressions and projection mappings.

It took more time than I expected, mostly due to lots of work on my dayjob and resulting lack of free time, but now it's done. It is now quite usable for most mapping scenarios. I hope you'll find it useful.

Posted at 11:12 on January 28, 2008
Categories: Otis   E-mail | del.icio.us | Permalink | Comments (0) | Post RSSRSS comment feed

Yet another blog, yet another open source project

Who needs one more blog, you might ask, and i wouldn't be able to provide a good answer.

I decided to stick a toe in blog waters mostly to have a place where I can post programming-oriented reminders to myself, and hopefully to share some useful tips/code with other people.

Anyway, it coincided with the public release of Otis, my pet project, so this is also an announcement for yet another open-source project of questionable future. Otis is an open-source .NET object mapper library and you can read (and discuss) more about it on its homepage. I surely hope that some of people who somehow manage to find it in the sea of other projects will find it useful.

Posted at 18:01 on November 23, 2007
Categories: Otis   E-mail | del.icio.us | Permalink | Comments (10) | Post RSSRSS comment feed