Areas, Routes and defaults in MVC 2 RC

by Christoph Menge in Software

Download sample

There has been some discussion over at stackoverflow on setting a default route to an area in mvc, so I want to post some very crude, but working example code here… I will write some explanatory text here if I find the time.

I feel that the fact that the order of operation in Application_Start() matters is quite problematic, but that has been discussed elsewhere (I’ll be searching for the link…) Anyway, when registering the areas first, the default route can be set to an area.

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

In the AreaRegistration,

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "ShopArea_default",
                "{controller}/{action}/{id}",
                new { action = "Index", id = "", controller = "MyRoute" },
                new { controller = "MyRoute" }
            );
        }

will do the job. Note the use of the constraint that will only accept requests to the areas’ controller that should be used as a default.

This is a working solution, but I think it is quite error-prone and not very elegant. In the meantime, I started to use MvcContrib, because it’s portable areas are really smart, make deployment easier and foster reusability of the code, and the views.

Post to Twitter Post to Delicious Post to Digg Post to Facebook

Tags: , ,

A Simple Standalone Server for db4o as Windows Service

by Christoph Menge in Software, db4o

Download: db4oSimpleServer

Lately, I have fiddled around with Versant’s excellent dual-license object database db4o a little and I think object databases are very neat. In fact, I read a few pages in PoEAA [Patterns of Enterprise Application Architecture] today, and here and there Fowler writes how cool object databases are. The Fowler knows!

Well, anyhow when you download db4o (sign up and go for the latest release) there is no standalone server application included. That makes perfect sense, because the server needs access to your domain model / data model dll. In an object database, there are no tables, but there are objects, so you have to compile them into the application that runs the actual db4o server. There is server code which handles practically everything that is tricky, but it’s in a dll so you’ll have to write a few lines to build an application out of it. I figured this would be a great time to write my first windows service and so I mashed up some code I found on the net.

I initially thought the VS 2008 designer would set up pretty much everything, but it turned out that things are a little more complicated.

The Installer

Since a service will run in a different manner, potentially using a different user account, etc. it needs to be installed first. There is a tool called installutil.exe, but using it is a little awkward. Also, you’d have to deploy it along your application. Matt Davis posted a cool guide on stackoverflow and some additional information here on how to write a windows service that is esentially able to install itself!

Unexpected Features

One of the most peculiar features is that -should you prefer to have your own event log instead of writing to the ApplicationLog- you have to search through a tree of installers, find the EventLogInstaller and modify it instead of simply adding a new one because the ServiceInstaller will come along with the EventLogInstaller as a child:

EventLogInstaller installer = FindInstaller(this.Installers);
if (installer != null)
    installer.Log = ServiceConfiguration.LogName;

We also need to take care of spawning the actual worker thread, but fortunately there is a comprehensive guide on MSDN explaining this.

Using the Sample

First things first: Of course, this is not nearly production-safe code! Also, I did not include the db4o dll’s because I’m not sure about the licence terms.

The installer in 'action'. Note the Administrator prompt

  1. Download the sample, extract
  2. Download db4o, install
  3. Copy Db4objects.Db4o.dll and Db4objects.Db4o.CS.dll from the db4o install directory to the samples’ lib directory
  4. Compile
  5. Open an Administrator command prompt and find the sample’s bin folder. There is no feedback in case anything goes wrong (at best, the program crashes)
  6. Call the install routine by executing emphess.db4oServer.exe -install. This will install the db4o server and the associated event log. The server will not be started automatically.
  7. Head over to services.msc or your EventLog, where you should find a new entry!
  8. Edit the configuration file, ready to run!
  9. Don’t use this for anything production…

The App.Config

The configuration file looks like this

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="UseLocalFile" value="false" />
    <!-- <add key="DatabaseLocalFileName" value="irrelevant"/> -->
    <add key="DatabaseHost" value="localhost" />
    <add key="DatabasePort" value="52354" />
    <add key="DatabaseServerFileName" value="C:/Development/db4oServer/db/server.db4o" />
    <add key="DatabaseUser" value="test" />
    <add key="DatabasePassword" value="test" />
  </appSettings>
</configuration>

The UseLocalFile setting is meant for clients so you can easily switch between file-based and network-based configurations. Since db4oServer.Shared contains a small class that holds this configuration data, you can easily reuse the code in a different app, e.g. in the client.

Todo-List

There are some things I’d like to add over the next weeks, so I put the list here to give me some ‘pseudo-extrinsic motivation’… (yes, I like complicated words)

  • Simple sample client
  • Out-of-band data for backup, defragmentation commands, etc.
  • Tests / Benchmarks

I hope you have any use for this.

Post to Twitter Post to Delicious Post to Digg Post to Facebook

Tags: , , , ,

A Nice Product Crippled

by Christoph Menge in Software

A screenshot of Yahoo's signup page

Not so delicious

Well… with a few years delay, I just wanted to sign up to del.icio.us, which is now simply called delicious.com. When Yahoo bought it, they obviously decided they didn’t want to have any more users, as you can see on the right.

I want to store some bookmarks online. period. Do they really think I’ll signup for a yahoo mail account (given that I already have a dozen accounts anyway?) Why does one need two secret questions? Secret questions are stupid in my eyes anyhow, but in times of facebook, knowing one’s mothers maiden name does not mean anything… Anyway, can somebody recommend a (good) online bookmarking service? yeah, I know, shouldn’t be too hard to find, hm? I better get some sleep… :)

Post to Twitter Post to Delicious Post to Digg Post to Facebook

Updating WordPress Plugins

by Christoph Menge in Software

Automatic updates are great, especially for web applications. Apart from saving you some time, I believe they help to enhance security since updating is really easy if you just need to push a button. The ‘traditional’ workflow involves backing up, finding and downloading the most recent (compatible) version from an external site, unzipping, uploading via FTP, calling some update script, perhaps setting a maintenance mode, etc.

However, when using shared hosting there are some pitfalls. I just trapped into the “Unable to locate WordPress Content directory (wp-content).” error. Here’s what I did:
My web hoster allows many FTP logins, so I created one especially for WordPress and set its ftp root to the WordPress root
It seems that the error message is a little misleading, because it turned out that access to the temp-directory was the actual problem. In this post, user lightwolf posted a workaround:

putenv('TMPDIR=' . ini_get('upload_tmp_dir'));

Added to wp-config.php.

This fixed my problem.

Post to Twitter Post to Delicious Post to Digg Post to Facebook

Tags: ,

A very simple profiler

by Christoph Menge in Software

When I just posted the source code of the Dependency Property Serialization sample, I realized it included a small piece of source code that might come in handy from time to time:

A very basic, but rather exact profiler. It uses QueryPerformanceCounter. Perhaps I shouldn’t even call this profiler, because a profiler typically keeps track of the parts of code it measures. So let’s call it a precise stopwatch. Well, anyways, here’s the code:

Continue Reading

Post to Twitter Post to Delicious Post to Digg Post to Facebook

Tags: ,

DependencyProperty Serialization Part II

by Christoph Menge in .NET

First off, my apologies for not posting the second part earlier. I have had a lot to do in the past Months…

Part II of the article will show how a SerializationSurrogate works and explore wheter we can use it for generic de-serialization of DependencyProperties. Also, in this article I provide full source code for both parts of the article.

UPDATE: A much better version of this article can now be found at the CodeProject: DependencyProperty Serialization for Business Objects

Alternatively, directly get the source code here: http://www.emphess.net/wp-content/uploads/2009/03/bpboserialization1.zip

Continue Reading

Post to Twitter Post to Delicious Post to Digg Post to Facebook

Tags: , ,

DependencyProperty Serialization

by Christoph Menge in Software

Ok, I did it: I tried to do it the easy way — serialization! And I quickly ran into the following issue: Serializing DependencyObjects. There are a lot of sources saying “you can’t serialize dependency objects”, which is true, but you can get pretty close rather quickly and it might not be the worst decision.

In this article, we’ll explore if and how reflection magic can assist us in doing it anyway, how we can locate DependencyProperties via reflection and what pitfalls remain. Moreover, I’ll give an outlook why the most desireable solution (that almost suggests itself) fails.

Continue Reading

Post to Twitter Post to Delicious Post to Digg Post to Facebook

Tags: , ,

Composite WPF, Aggressive GC and dead events

by Christoph Menge in Software

Well, this is the first post after a very long pause, I am sorry but I had very little time…

Anyway, I stumbled over some strange bug this morning that turned out to be related to the GC. But let’s just step back a minute:

The application I currently work on is based on CompositeWPF . I added a very simple module to the application, a simple passive debugging module. I called it “InspectorModule”. The InspectorPresenter now listens for global modifications to the business object which is kept by the BusinessEntityManager:

public InspectorPresenter(IInspectorView _view,
            IEventAggregator _eventAggregator,
            IBusinessEntityManager _businessEntityManager)
{
    mView = _view;
    mEventAggregator = _eventAggregator;
    mBusinessEntityManager = _businessEntityManager;

    mEventAggregator.GetEvent<BusinessInstanceInvalidatedEvent>().Subscribe(ReloadInspector);
}

Now, upon each modification of the business object, ReloadInspector shall be called. So, starting the application and triggering the event — nothing happened. The ReloadInspector method is not being called. Why?

After some debugging, it suddenly worked – the only problem being: I didn’t actually change anything!

To make a long story, I realized the CompositeWPF DelegateReferenceclass keeps a WeakReference to the object (the InspectorPresenter, in this case) when registering it via the EventAggregator as above.

Unfortunately, due to the simplicity of the InspectorPresenter, no instantiated object actually keeps a reference to the Presenter (apart from the WeakReference of the event which, by definition, does not prevent it from being finalized).

That’s why the object is being finalized and the event never fires. Actually, on the first call of the associated Publish() method, the Composite WPF-Internal EventBase.PruneAndReturnStrategies() will remove it from the list after it’s associated Action is supposedly null.

Debugging this was quite cumbersome, and the solution of striking simplicity: I simply continued to develop the “Inspector”, a little more than I originally planned, so the user can now select an object in the inspector. Now, I have a simple event like this:

public InspectorPresenter(IInspectorView _view,
            IEventAggregator _eventAggregator,
            IBusinessEntityManager _businessEntityManager)
{
    // ...
    mView.SelectedNodeChanged += new OnSelectedNodeChanged(mView_SelectedNodeChanged);
    // ...
}

This keeps a (strong) reference to the Presenter, and the GC does not kill my presenter anymore! The reason it worked when debugging, I guess, is that the debugger will keep the GC from finalizing objects under certain circumstances.

Post to Twitter Post to Delicious Post to Digg Post to Facebook

Tags: , , ,

WPF Direct3D Interop

by Christoph Menge in Software

There are some very interesting posts I missed the last days. Since .NET 3.5 SP1 perfectly integrates WPF with DirectX, a number of cool effects can finally be applied:
http://www.codeproject.com/KB/WPF/WPFPixelShader.aspx

http://blogs.msdn.com/greg_schechter/archive/2008/05/12/introduction-to-writing-effects.aspx

http://weblogs.asp.net/scottgu/archive/2008/05/12/visual-studio-2008-and-net-framework-3-5-service-pack-1-beta.aspx

Some of those effects were on Dax Pandhi’s wish list for WPF (http://blog.nukeation.com/post/My-wishlist-for-the-next-release-of-WPF.aspx) for quite some time.

More importantly, the integration of WPF and Direct3D might eliminate the need for a custom overlay control for the MenuKiller Control, which is a major performance hog (I currently use an overlay similar to this: http://blogs.msdn.com/pantal/archive/2007/07/31/managed-directx-interop-with-wpf-part-2.aspx).

Post to Twitter Post to Delicious Post to Digg Post to Facebook

Tags: , , ,

A MenuKiller Control – Draft

by Christoph Menge in Software

This is just a draft on my upcoming article. I have asked a number of WPF Gurus to help me out on this…

Introduction

In an effort to take user experience to the next level, designers have come up with ideas on how to solve old problems in a new way. In the last few months, the term “Differentiated User Experience” or simply “Differentiated UX” has come up to describe these efforts.

One of the beforementioned UI designers is Dax Pandhi, who published an article “Rethinking the Button” on his blog which introduced a new control he calls MenuKiller. This control is certainly more than a replacement for the Menu and ContextMenu, as it can be used in ways that are quite different. However, it turns out that changing the way things are typically done is indeed not so easy.

A Screenshot of the MenuKiller

This article gives an overview of my implementation of the MenuKiller and presents a small sample application. It’s written in C# using .NET 3.5 and Visual Studio 2008. What is presented here is not a full-fledged control, since there are quite a number of open todos. Yet, I hope that this motivates the use of different controls and serves as a good starting point for other WPF control development.

Continue Reading

Post to Twitter Post to Delicious Post to Digg Post to Facebook

Tags: , , , ,