Dennis van der Stelt

The only way to win is to learn faster than anyone else

Community

Email Notifications

News

  • Addicted to Refactor! Pro

I read...

I Use...

Tags

Recent Posts

Archives

Blog Subscription Form

  • Email Notifications
    Go

October 2009 - Posts

Zune player in taskbar

 zunetaskbar This is pretty sweet, you can have the Zune player in your taskbar.

On previous version of Windows, I had my media player up there. But now the Zune player has this ability in Windows 7 as well.

If you have the Zune 4.0 software installed you can right-click the Windows 7 taskbar, select toolbars and then select the Zune toolbar. Now whenever you minimize the Zune player software, the icon will disappear and the player will be in the taskbar. The larger image on the left appears when you hover over the minimized player.

On the Windows weblog there’s more information available about the combination of Windows 7 and the Zune software. There are other options available that might interest you.

Zune software updates for Zune 4.0 optimized for Windows 7

Create a custom caching manager for Enterprise Library 4

I wanted to create a custom caching manager for Enterprise Library 4 that did… nothing. I Googled and Binged but could not find a way to temporarily turn off caching completely. And I want that so I can do some performance tests with the websites we have on the server. I don’t want to know how fast my app is, I want to load test the server and see how it performance, with and without caching. So that’s why I decided to create a Caching Manager that did nothing.

The documentation did not tell me much though, and there are some really weird gotchas in there. Here’s the part about the custom caching manager.

caching_documentation

Inherit from ICacheManager and add ‘the following configuration element’. And they’re serious about that part!!! I’ll show you why. Here’s what I did. First, I created a new Class Library and added a class named PointlessCacheManager that inherits from ICacheManager and put the attribute on top of it. I added another one for those who need it, but it’s commented out. This extra class also does absolutely nothing! :) First the custom cache manager.

//[ConfigurationElementType(typeof(PointlessCacheManagerData))]
[ConfigurationElementType(typeof(CustomCacheManagerData))]
public class PointlessCacheManager : ICacheManager
{

    public PointlessCacheManager()
    {
    }

    public PointlessCacheManager(NameValueCollection collection)
    {
    }


    public void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
    {
    }

    public void Add(string key, object value)
    {
    }

    public bool Contains(string key)
    {
        return false;
    }

    public int Count
    {
        get { return 0; }
    }

    public void Flush()
    {

    }

    public object GetData(string key)
    {
        return null;
    }

    public void Remove(string key)
    {
        return;
    }

    public object this[string key]
    {
        get { return null; }
    }
}

If you use the CustomCacheManagerData you need the constructor with the NameValueCollection as parameter. The PointlessCacheManagerData uses the default constructor, or you can modify it to whatever you like.

Are we done yet? Maybe almost!!!
Again, if you want to have a custom cache manager that does nothing, you’re done with adding classes and you can skip the next part and continue reading at the configuration part. If you need more, like your own configuration, read on…

Now here’s the second class, with an even lesser implementation.

[Assembler(typeof(PointlessCacheManagerAssembler))]
public class PointlessCacheManagerData : CustomCacheManagerData
{
}

But this PointlessCacheManagerData class also references another class named PointlessCacheManagerAssembler. These two classes are used for reading configuration and initiating the cache manager. Here’s the last class.

public class PointlessCacheManagerAssembler : IAssembler<ICacheManager, CacheManagerDataBase>
{
    public ICacheManager Assemble(Microsoft.Practices.ObjectBuilder2.IBuilderContext context, CacheManagerDataBase objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
    {
        PointlessCacheManager createdObject = new PointlessCacheManager();
        return createdObject;
    }
}

Configuration
app_config_entlib Now comes the fun part. You want to configure the new cache manager in your application. Most of it is pretty easy though. Open the Enterprise Library Configuration application, or open up the application configuration by right-clicking it in Visual Studio, as you can see on the right.

When the configuration is opened (either in the seperate EntLib Config app or in Visual Studio), right-click the configuration node and select “New” and “Caching Application Block”. A new node should be added and under it you should see a node called “Cache Managers” with the default cache manager under it. Right-click the “Cache Managers” node (or folder, if you will) and select “New” and “Custom Cache Manager”. This is where you’ll configure your PointlessCacheManager.

However, and this is a big however. Two even. But only if you used another class than the default CustomCacheManagerData in the attribute on top of your PointlessCacheManager. Because the documentation told you to put “[ConfigurationElementType(typeof(CustomCacheManagerData))]” on top of your custom cache manager. And as I said before, they mean what they say. Although we don’t want to use that class when we implement our own configuration, we need to put the CustomCacheManagerData on top anyway. If you did that and compiled the class, we can configure our custom cache manager.

app_config_entlib_2 You can first give this one a name. I named mine “Pointless Cache Manager”. Note the spaces, the name is nowhere related to class names. Now select the “Type” key so that you can add a value. A small button should come available to select the type in an assembly. I pointed it out in the image on the left. Locate the assembly with the PointlessCacheManager in it on your harddrive. You should get the following image. If not, read on as well.

app_config_assembly

REMEMBER : When you made a change, mistake or anything like this, you need to restart the host of the configuration, to be able to see the changes. In other words, when you loaded the Enterprise Library Configuration application, restart it. When you’re changing the configuration in Visual Studio, restart it. Because it loads the assembly and never refreshes it, so changes aren’t visible until you restart the host application.

Make sure you’ve got the commented out line in the PointlessCacheManager uncommented so that it exactly (!) states what the documentation states. Once you’ve configured it correctly in your application’s configuration (or web.config) you can change it back to where it states that you’re using the PointlessCacheManagerData instead of the CustomCacheManagerData, recompile and work with it.

Now you can switch back and forth between the normal en the pointless cache manager.

SDC 2009 slidedecks and demos

DSC_0130 For me this was my first SDC and I really had a lot of fun. Great discussions with speakers and attendees. And as Dennis Doomen mentioned, the evening was pretty fun as well. I did two sessions, on about Test Driven Development, a basic introduction with some best practices. And also a presentation on Microsoft’s solution to distributed caching, “Velocity”.

Introduction into the TDD Mantra (slides and demo)
So you've heard about Test Driven Development but you're not sure what it is? Not sure if it is what you're doing? And if's "Test Driven", why do people say tests are less important? Why do tests first have to fail? In this session you'll learn what Test Driven Development is really about. About the simplest thing first and growing your application larger and larger. Until TDD made your application into what it should be, instead of what you thought it was going to be.

Distributed caching with Velocity (slides and demos)
It's been some time since Velocity was introduced, and it hasn't gotten the attention it deserved. Velocity is a distributed caching framework which means it can cache your application's data and distribute this over multiple machines for performance, backup, etc. If you want to know how to use Velocity in your project and/or your website, come join me for this session.

If you have any questions about any of these subjects, don’t hesitate to contact me.

Syntaxhighlighter

I’ve tried a lot of tools and addons to use syntax highlighting on my weblog and BloggingAbout.NET in general. I’ve fallen in love with two of them.

The first I use to use for my weblog. But this causes a lot of markup within the code and it can never be changed later on. I still use CopySourceAsHtml for posting in PowerPoint, Word and other tools though. Simply because it’s really, really good and easy to use from within Visual Studio.

SyntaxHighlighter is a set of Javascript that gives your code its color after it’s being displayed, ie. when the page has loaded. The code itself is rendered within <pre> tags and has no specific markup in HTML for the coloring. This makes the highlighting a bit slower, but with the additional benefits that there’s no extensive markup in the code, that the coloring libraries can be updated. Additionally a popup window is displayed for easily copying the code and some other useful buttons.

For installation I’ll redirect you to the official site.

SyntaxHighlighter plugin for Windows Live Writer : PreCode
What makes my life especially easy as a blogger, is this plugin called "PreCode". The plugin fits nicely into Windows Live Writer and with a popup window, a paste and a fix indentation you have some new code layed out on your weblog. Could not be any easier.

This is the end result:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Hello world");
            }
        }
    }
}