Journal of a software dev

May 16, 2009

log4net FallbackAppender – under the hood

Filed under: .net, advice, log4net, log4net contrib, software design — Tags: , — Michael Cromwell @ 6:51 pm

Some Background

I recently put together log4net contrib project up on google code the only item currently up there (though hopefully there should be more stuff from the community up there) is a custom appender I wrote to address the need for having an appender that is clever enough to fallback to other appenders if one fails, this originally came from a forum post of someone wondering how to do it and after some investigation I was that NLog has this built-in.

Investigating how to add it

So my first step was to grab the log4net source and see how I could integrate this appender in, one thing I really didn’t want to do was to change the log4net code and instead allow the appender to be plugged in like you would do normally.

First step was to see how to create an appender that holds other appenders and stumbled across IAppenderAttachable and then the ForwardingAppender which implements it, this interface is what allows you to user the following xml:

<appender name="FallbackAppender" type="log4netContrib.Appender.FallbackAppender, log4netContrib" >
      <appender-ref ref="FileAppender" />
      <appender-ref ref="ConsoleAppender" />
</appender>

And the configuration makes sure that the appender is populated with the referenced appenders. Looking at the behaviour of the existing ForwardingAppender it gave me a lot of what I was after so I inherited from this.

Hitting the main problem

Now I had the FallbackAppender which got most of it’s behaviour from the ForwardingAppender the next step was to setup the following logic:

  1. Get first appender in the collection
  2. Try and append to it
  3. If succeeded exit
  4. Otherwise try and append to the next appender in the collection
  5. Rinse repeat until one succeeds or all are exhausted

On the face of it this seems to be quite simple, loop the appenders and put a try…catch around the call to Append then have some logic in the catch to use the next appender and repeat, something like this:

while (apppenderQueue.Count > 0)
{
    try
    {
        var appender = appenderQueue.Dequeue();
        appender.Append(loggingEvent);
        break;
    }
    catch (Exception thrownException)
    {
        // log to internal log about failure
    }
}

When I tried this and tested against log4net it didn’t work only the first appender was being appended to if I setup the config incorrectly to simulate an issue.

After some digging the issue became obvious the appenders don’t let exceptions bubble up through the stack and instead log to an IErrorHandler that they get via a property from the AppenderSkeleton object they inherit from and with this being the only way to track errors from appenders I had to then impose a limitation.

Adding the AppenderSkeleton limitation to go forward

With the ErrorHandler property coming from AppenderSkeleton being the only way to discover errors I came to the conclusion that I needed to hook into this property luckily the property can be set, this also imposed the limitation that any of the appenders being referenced had to inherit from AppenderSkeleton in the inheritance tree at some point in order to get access to the ErrorHandler property.

Putting in the hooks

The next step I did was to create my own IErrorHandler that simply recorded if an error occurred, and at appender activation I replace each of the referenced appenders ErrorHandler with that one, now the code can see if an error has been raised for a particular appender and move onto the next one.

Summary

I guess this post demonstrates that even if a third party component doesn’t necessarily provide you with an API that makes extending behaviour easy in this example if log4net appenders let the exception bubble up or the Append method returned a bool whether it succeeded or not, there are still tricks we can use to try and achieve it.

April 11, 2009

RouteValueDictionary and why we need Hash Literals

Filed under: .net, c# — Tags: , — Michael Cromwell @ 12:26 pm

When working with ASP.NET MVC you will find quite a lot of code that needs key value pairs, especially for routing and providing html attributes, and in most of these places you will see a trend whereby no Dictionary type is created but instead an anonymous type is used:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" }

So how can it take our anonymous type and create a dictionary from it? Well the object that does the work resides in System.Web.Routing and is called RouteValueDictionary (I think this should be called something like AnonymousTypeDictionary and placed somewhere else as it doesn’t really have anything to do with routing and is used in other sections), this object implements IDictionary<string, object> and has an overloaded constructor that takes a plain object type, it then uses reflection to grab the properties associated with the object and uses the property name as the key and the property value as the value.

So when any of the ASP.NET MVC objects want to create a key value pair they simply pass the anonymous type to the RouteValueDictionary object’s constructor:

// routeValues and htmlAttributes come in as object type.

return htmlHelper.ActionLink(linkText, actionName, null, new RouteValueDictionary(routeValues), new RouteValueDictionary(htmlAttributes));

And voila instant IDictionary<string, object>!

So if this works what’s the problem… well in IMO it’s hack to get round the issue that C# doesn’t have support for hash literals and it would seem I’m not alone, it suffers from a few problems:

  1. Because it uses anonymous types this means the parameter to the method has to be of type object, and from an API point of view that gives us nothing to go as to what needs to be passed in.
  2. It uses reflection to build the key value pairs which takes a performance hit.

For instance take Boo, we could write the following:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    { "controller" = "Home", "action" = "Index", "id" = "" }

Or by using the open compiler wizardry:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    { controller = "Home", action = "Index", id = "" }

Maybe this is asking too much from the compiler and this is as close as were going to get but that would be a shame given the problems above this approach suffers from.

November 28, 2008

Getting to grips with NHibernate: Stored Procedures Redux

Filed under: .net, advice, c#, code example, nhibernate, sql, tips — Michael Cromwell @ 8:23 pm

Update: Due to popular demand I have included using stored procedures for insert/update/delete

I hope this post saves someone the amount of time it took me to try and run a stored procedure using NHibernate.

Selecting from a Stored Procedure

Create Stored Procedure

The first step is to create your stored procedure, if we take a basic example:

CREATE PROCEDURE SearchStaff
	(
	@LastName VARCHAR(255) = NULL,
	@FirstName VARCHAR(255) = NULL
	)
AS
	SET NOCOUNT ON

	SELECT s.*
	FROM Staff s
	WHERE (s.LastName LIKE @LastName OR @LastName IS NULL)
	AND (s.FirstName LIKE @FirstName OR @FirstName IS NULL)
	ORDER BY FirstName, LastName

	SET NOCOUNT OFF

Note the SET NOCOUNT ON/SET NOCOUNT OFF

Create a named query

In order for NHibernate to be able to run the stored procedure, we need to create a named query, in our hbm file:

<sql-query name="StaffSearching">
	<return class="Foo.Core.Staff, Foo.Core">
	  <return-property name="Id" column="Id"/>
	  <return-property name="LastName" column="LastName"/>
	  <return-property name="FirstName" column="FirstName"/>
	  <return-property name="MiddleName" column="MiddleName"/>
	</return>
	exec SearchStaff :LastName, :FirstName
</sql-query>

In this example because the stored procedure is actually returning a staff object I set the return to the Staff class, if I was only returning something like an integer value I could use the following:

<sql-query name="TotalStaff">
    <return-scalar type="Int32" column="Count"/>
    exec StaffCount :LastName, :FirstName
</sql-query>

The name does not need to be the same name as the stored procedure.

Create the NHibernate code

// session set here

IQuery searchQuery = session.GetNamedQuery("StaffSearching")

if (!string.IsNullOrEmpty(LastName))
	searchQuery.SetString("LastName", LastName);
else
    searchQuery.SetString("LastName", null);

if (!string.IsNullOrEmpty(FirstName))
    searchQuery.SetString("FirstName", FirstName);
else
    searchQuery.SetString("FirstName", null);

IList foundStaff = searchQuery.List();

Notice that the stored procedure can deal with or without filtering, so if the fields have not been set we can simply set them to a null value and NHibernate will pass the parameters as a NULL which is what we want.It’s worth noting that the above is quite a simple example and that for the above I would not use a stored procedure and instead just use NHibernates own querying objects. The case were I used a stored procedure was for a paging routine for SQL server 2000.

Using Stored Procedure for Insert, Update & Delete

Create Stored Procedures

CREATE PROCEDURE InsertStaff
	(
	@LastName VARCHAR(255),
	@FirstName VARCHAR(255),
             @EmailAddress VARCHAR(512)
	)
AS
             INSERT INTO Staff
             (
              LastName,
              FirstName,
              EmailAddress
             )
             VALUES
             (
              @LastName,
              @FirstName,
              @Email
             )

Note: SET NOCOUNT is not set for these stored procedures

I’m not going to include the SQL for the update and delete stored procedures as I don’t think it adds any value, and is the easy part.

Update NHibernate XML Mapping

We need to tell NHibernate the SQL we want to execute for our insert/update/delete, we do this inside the class element:

<class name="MCromwell.StaffIntranet.Core.Staff, MCromwell.StaffIntranet.Core" table="Staff">
    <id name="Id" column="Id" type="Int32">
      <generator class="native" />
    </id>
    <property name="LastName" column="LastName" type="String" length="255"/>
    <property name="FirstName" column="FirstName" type="String" length="255"/>
    <property name="EmailAddress" column="EmailAddress" type="String" length="512"/>

    <sql-insert>EXEC InsertStaff ?,?,?</sql-insert>
    <sql-update>EXEC UpdateStaff ?,?,?,?</sql-update>
    <sql-delete>EXEC DeleteStaff ?</sql-delete>
</class>

Caveats

  • The last parameter will be the id for the update.
  • The ordering of the parameters is determined by NHibernate, so the best way to find out the ordering would be to view the generated SQL, bit pants but hay ho.

Your code will remain the same, so no changes needed there.

November 22, 2008

Why does GridView…

Filed under: .net, rant — Tags: — Michael Cromwell @ 8:41 pm

…not support inserting a record out of the box? Just seems like something so obvious you would want to do.

Instead you end up having to do something like this where you use a FooterRowTemplate and wire up your own insert call.

September 14, 2008

Dogfooding with Lightweight Object2Object Mapper

Filed under: .net — Tags: , , — Michael Cromwell @ 6:28 pm

After gettting some feedback about my Object 2 Object Mapper on codeproject I made some amendments over the weekend and wanted to put them into practice, thats when I decided to go down the dogfooding route with the Staff Intranet project being my dog so to speak.

Mapping in the Staff Intranet was handled by having a Mapper object for each mapper permutation each deriving from an IMapper generic interface. So I began by making sure all my tests ran ok then one by one replacing each bit of code that referenced the mappers and instead setup the mappings using a SimpleObjectMapper and registered it into the SimpleObjectMapperContainer, i could then replace the code using the existing mapper to use the registered SimpleObjectMapper, re-run the tests to make sure everything was still expected.

There were a few little areas that needed to be looked at but I was very pleased with the result of making the switch, in the end using the SimpleObjectMapper made the following objects redundant:

And was replaced by the following which is ran inside the Application_Start method:

SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<Administrator, AdministratorDTO>());
SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<Location, LocationDTO>());

SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<Staff, StaffDTO>()
                                            .Explicit((staff, staffDTO) => staffDTO.LocationId = staff.Location.Id)
                                            .Explicit((staff, staffDTO) => staffDTO.LocationDescription = staff.Location.Value)
                                            .Explicit((staff, staffDTO) => staffDTO.Photo.Data = staff.Photo));

SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<StaffDTO, Staff>()
                                            .Explicit((staffDTO, staff) =>
                                            {
                                                if ((staffDTO.Photo.SaveOption == PhotoSaveOption.Update) && (staffDTO.Photo.Data != null))
                                                    staff.Photo = staffDTO.Photo.Data;
                                            })
                                            .Explicit((staffDTO, staff) => staff.Location.Id = staffDTO.LocationId));

SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<DomainObjectValidationResult, NotificationDTO>()
                                            .Explicit((domain, dto) =>
                                            {
                                                foreach (var item in domain.Results)
                                                    dto.AddError(item.ValidationId, item.Message);
                                            }));

With me being on holiday I hope to get the codeproject article updated in the next few days.

September 6, 2008

Lightweight Object Mapper Infomercial

Filed under: .net, c# — Tags: , , , — Michael Cromwell @ 4:31 pm

Tired with writing monotonous & repetitive mapping code, wish their was an easier way to map object A to object B, well look no further, introducing the Lightweight Object to Object Mapper.

As Seen on TV

The Lightweight Object to Object Mapper is designed to make object mapping a doddle! By employing the latest techniques in convention over configuration reduced coding is guaranteed[1] and for those tricky to reach places explicit property mapping can be achieved with minimal effort and fuss.

So don’t lose out, download Lightweight Object Mapper right now and start enjoying object to object mapping, but that’s not all for a limited time period if you download the Lightweight Object to Object Mapper you will receive accompanying unit tests… so don’t delay download now!

  1. Guarantee is subject to both the mapped from & mapped to object structure

August 19, 2008

Null Object Pattern by example

I feel sorry for the Null Object pattern, while Abstract Factory Pattern, Strategy and the other patterns from the GoF book get all the attention the Null Object pattern doesn’t get a look in. This is a shame because this pattern can come in very useful, saving you many scattered ==null checks across your code, I’m going to show a real world use of Null Object pattern (and Adapter pattern as a by-product) using example logging objects:

Lets get to it, we have an ILog it looks like this:

public interface ILog
{
    void Debug(string message);
}

Ok granted I have made it a simple example at least it ain’t an ICar or IAnimal ;) how do we get hold of our ILog with an ILogFactory of course:

public interface ILogFactory
{
    ILog GetLog(Type type);
}

We can make this easier to get hold of by having a static gateway so we aren’t having to pass around an ILogFactory to every object or having to new up a ILogFactory instance everytime we want to log:

public static class Log
{
    private static logFactory;

    public static void InitializeWith(ILogFactory logfactory)
    {
	this.logFactory = logFactory;
    }

    public static ILogFactory Current(Type type)
    {
        return logFactory.GetLog(type);
    }
}

You could also leverage a IoC container here instead.

Now we want some implementations I’m a fan of log4net so I would implement the following:

public class Log4NetLogFactory
{
    public ILog GetLog(Type type)
    {
	//retrieve an adapted log4net log something like...
	log4net.ILog rawLog = log4net.LogManager.GetLogger(type);
	return new Log4NetLog(rawLog);
    }
}

With Log4NetLog looking like this:

public class Log4NetLog : ILog
{
    private log4net.ILog log;

    public Log4NetLog(log4net.ILog logToAdapt)
    {
	this.log = logToAdapt;
    }

    public void Debug(string message)
    {
	if (log.IsDebugEnabled)
	    log.Debug(message);
    }
}

Now we want to be able to log in our client code like this:

Log.Current(this.GetType()).Debug("some debug message");

The problem of course is that if we try to do this without hooking up our static Log class to an ILogFactory we get the dreaded null reference exception, this is bad! Our logging code should not throw exceptions, enter the Null Object pattern:

public static class Log
{
    private static logFactory = new NullLogFactory();

    public static void InitializeWith(ILogFactory logfactory)
    {
	if (logFactory != null)
	    this.logFactory = logFactory;
    }

    public static ILogFactory Current(Type type)
    {
        return logFactory.GetLog(type);
    }
}

We instantiate a new NullLogFactory object and assign it to the static logFactory member, we also perform a check to make sure no nulls are sent to the InitializeWith method, here is the NullLogFactory:

public class Log : ILogFactory
{
    public ILog GetLog(Type type)
    {
        return new NullLog();
    }
}

All this does is new up a NullLog object:

public class NullLog : ILog
{
    public void Debug(string message)
    {
    }
}

The NullLog does nothing whatsoever apart from prevent us from receiving null reference exceptions, the neat thing about this though is that we could potentially capture attempts to call methods the NullLog and perform some sort of internal debugging:

public class NullLog : ILog
{
    public void Debug(string message)
    {
        if (Config.IsInternalDebugEnabled)
            Debug.Write("'{0}' sent to NullLog", message);
    }
}

That way users of the logging objects can be notified if logging has taken place but a log factory has not been hooked up.

July 16, 2008

Multiple instances of same windows service

Filed under: .net, advice, code example, tips — Tags: , , , — Michael Cromwell @ 7:36 pm

There are times were you want to be able install multiple instances of the same windows service onto the same server (i.e. to support different environments but you have limited servers at your diposal) this poses a problem, you are probably aware windows will not allow more than 1 windows service to be installed with the same service name and when you create a windows service project and an installer you assign the service name at design time.

We need is someway to assign the service name at runtime, well after some extensive googling around I found a way of achieving this, the ProjectInstaller has 2 methods you can override Install and Uninstall which enables us to make changes to the service installer at the at runtime :-) Yeah that’s great however how do we assign the service name?! Fear not! at our disposal is the Context class that happens to have a Parameters dictionary so we can now capture custom command arguments passed to installutil.exe, enough yapping time for some code:

public override void Install(System.Collections.IDictionary stateSaver)
{
	RetrieveServiceName();
	base.Install(stateSaver);
}

public override void Uninstall(System.Collections.IDictionary savedState)
{
	RetrieveServiceName();
	base.Uninstall(savedState);
}

private void RetrieveServiceName()
{
	var serviceName = Context.Parameters["servicename"];
	if (!string.IsNullOrEmpty(serviceName))
	{
		this.SomeService.ServiceName = serviceName;
		this.SomeService.DisplayName = serviceName;
	}
}

In this instance I have made the servicename argument optional in which case it will use the default assigned at design time, you could enforce it in your version.

We can now use this like this:

installutil /servicename=”My Service [SysTest]” d:\pathToMyService\Service.exe

and for uninstall:

installutil /u /servicename=”My Service [SysTest]” d:\pathToMyService\Service.exe

Note for the uninstall you need to supply the servicename so windows how to resolve the service to uninstall

One thing that suprises me is how hidden this implementation is, most results bring up people wanting the same functionality and being told it ain’t possible and articles that suggest messing with config files this to me seems to be a much simpler and nicer way to achieve multiple instances.

June 27, 2008

New Staff Intranet Release

I have found enough spare time to put up a new release of the staff intranet project, for those who are not aware of this project it is a demonstration of using best practices, principles & patterns in a real world web application so if your looking for pointers or some code to use for your own applications go give it a look on codeplex.

In this newest version I have added AOP support to cut down on cross cutting code and also the ability to delete staff members from the GridView, most of the time spent was fighting against the asp.net controls (suprise, surprise) such as the GridView and the ObjectDataSource, I’m not sure what the guy(s) who created the ObjectDataSource object was smoking at the time but it must have been stronger than just tobacco :-)

My next release I want to demonstrate adding some service support showing how we can re-use existing code so they become little more than a remote facade (in theory!).

June 25, 2008

Iterative Book Review

Filed under: .net, books — Tags: — Michael Cromwell @ 7:00 pm

While at the Software Architect conference I picked up a copy of Tim McCarthy’s .NET Domain-Driven Design with C# I’m a little way into the book and thought I would have a post that iteratively reviewed how I found chapters as I come across them as this should give more insight.

Firstly this is the first time I have really looked into DDD properly I have read a number of books and articles that demonstrate using patterns & best practices but nothing that has been so specific to DDD such as defining Aggregate & Value objects, so I’m looking at this having not read the infamous Eric Evans or Jimmy Nilssons books which may not be a good thing as Tim does recommend reading them first, ah well :-)

Things I like so far

  • The whole book is devoted to building a real world application so you get a good feel as to how you would apply the techniques to your own projects, great stuff!
  • The source code is up on codeplex
  • Nice introduction to the project and how it is going to be architect ed along with answers to the decisions made
  • Unit tests
  • Use of the Model-View-ViewModel using WPF (is this the same as Presentation Model?)

Things I ain’t to keen on

  • The Custom OR/M I think it would have been better and more clearer to use nHibernate or another available OR/M the custom one seems to add extra complexity with very little of the power gained by using an established OR/M (in defence Tim does mention had they not dropped it from vs2008 he would have used the Entity Framework)
  • Too much use of static factories, I would have preferred to have seen an existing IoC container being used this would have led to more testable code favouring DI rather than asking static factory classes for instances
  • Repository classes having lots of responsibility, I would be inclined to have a separate query builder object that the repositories just use, maybe in the later chapters this might be a refactoring
  • The service layers are static making them hard to unit test against

 These are early days and maybe some of the nuances may be sorted in later chapters, fingers crossed

Halfway update

Well I’m pretty much halfway through so I thought I would post some updates, one thing straight off the bat was some code I found to be a little off putting this is taken from the ProjectRespository class:

public override void PersistNewItem(IEntity item)
{
    Project project = item as Project;
    if (project != null)
    {
        this.PersistNewItem(project);
    }
    else
    {
        ProjectContact contact = item as ProjectContact;
        this.PersistNewItem(contact);
     }
} 

 Along with a quote of:

Everything is still cleanly broken out and easy to maintain

I’m not so sure see to me the above looks to me like a bit of a code smell it breaks OCP, if we need to add other entities to save for this repository we then need to extend this method plus the other PersistXXX methods to accommodate. The problem stems from code that the repository classes inherit from and in this case the code above has to dodge against the code in the base class using a combination of method overrides and downcasting.

I have to say that going through the chapters it’s losing it’s appeal each feels pretty much the same as the last with so far nothing new being introduced since the first chapters.

Up till now I haven’t downloaded any of the code and have just been using the book as the guide, however today I went to codeplex to get the code as I wanted to come up with a good strategy for solving the code above and was regrettably disappointed nothing seems to work properly and the layout seems to be broken when saving projects I’m getting a MethodNotImplemented exception and when I think a save has worked it doesn’t actually persist to the DB I think that maybe this was too big of a project to use as an example and maybe only half of the features in there should have been implemented.

Summing up

I have reached the end of the book so here is a roundup, overall I was disappointed which is a shame as I very much liked having a real world example use of DDD but was let down by the design of some of the objects (overuse of Template pattern, too many static methods & strange use of Specification pattern spring to mind) and would certainly not use the same solutions as a most do not lend themselves to unit testing

I also became increasingly agitated by the amount of errors in the book wereby code was shown with a completely wrong description of what the code was doing in some cases it was clearly a copy & paste from the previous code section that had not been picked by the proof readers.

I could not recommend this for someone wanting to learn about DDD and instead I would probably just say  go and buy the Eric Evans book.

Older Posts »

Blog at WordPress.com.