Facebook.NET Configuration Section

I’m checking out the Facebook.NET libraries, and find the documentation to be very lacking. I could be looking in the wrong places. Anyway, to setup the configuration section, you need to reference the FacebookNET.Web.dll. Then in your config file add a section entry to the configSections tag like:

<section name="facebook" type="Facebook.Web.Configuration.FacebookSection, FacebookNET.Web" allowDefinition="MachineToApplication"/>

Then add the Facebook section with your info to the configuration tag:

    <application name="Facebook.NET App" apiKey="[App Key]" secret="[Secret]"/>

That’s it. Now you can access that info from code like:

var fbSection = (FacebookSection)ConfigurationManager.GetSection("facebook");
var secret = fbSection.Applications["Facebook.NET App"].Secret;

Castle microkernel Shows It’s strength

I’m a big fan of the CastleProject.org stack, but the MicroKernel really shows it’s strength when you need to do a couple of custom things, and find out that the customization is really easy, and intuitive.

Setting up a facility to add a custom bunch of objects. I think there are already facilities to do this, but it’s really simple for standard cases and allows you to easily get things setup. You basically just need to override the Init method.

protected override void Init()

List<Assembly> assemblies = ReadConfigForAssemblies(FacilityConfig);
<span class="kwrd">foreach</span> (Assembly assembly <span class="kwrd">in</span> assemblies)


The configuration is already read in for you, so parsing that is easy.

public List<Assembly> ReadConfigForAssemblies(IConfiguration facilityConfig)
    List<Assembly> assemblies = new List<Assembly>();
        foreach (IConfiguration configuration in facilityConfig.Children)
            if (configuration.Name.Equals("assembly", StringComparison.InvariantCultureIgnoreCase))
    catch (Exception e)
        throw new ConfigurationErrorsException(
            "Assembly could not be loaded, check you have the correct name in the configuration.", e);
    return assemblies;

Lastly, you can search through the configured assemblies and register any types that match what your looking for.

private void LoadCommands(Assembly assembly)
    foreach (Type type in assembly.GetTypes())
        if (typeof (IMyInterface).IsAssignableFrom(type) && !type.IsAbstract && type.IsPublic)
            Kernel.AddComponent(type.FullName, type);

This is getting to be a long post, but I'm sure you're wondering about testing. It's easy too! You have to mock out the configuration stuff, but that's really it. I just put into a setup method and once done you have your container setup and ready for testing.

public void Setup()
    _container = new WindsorContainer();
    _facility = new CommandFacility();

    _mocks = new MockRepository();
    IConfiguration config = mocks.DynamicMock<IConfiguration>();
    IConfiguration configChild1 = mocks.DynamicMock<IConfiguration>();
    IConfiguration configChild2 = mocks.DynamicMock<IConfiguration>();

    using (_mocks.Record())
        Expect.Call(config.Children).Return(new ConfigurationCollection(new IConfiguration[] { configChild1 }));

    using (_mocks.Playback())
        _facility.Init(_container.Kernel, config);

There's more documentation on the Castle site. Next time custom dependency resolvers and sub kernels...

Castle MicroKernel Shows It’s Strength P2

Continuing from my previous post… Using Custom Dependency Resolvers and Sub Kernels. I think this is more of an edge case, but the fact that I can be done really easily is where the Castle strength is.

First, I see two nice things about Sub Kernels, they can be used by themselves or added as a child to another Kernel, meaning for testing and other uses, the Kernel can be completely hidden. Next, when added as a child, dependencies that live in the parent container can be used by types in the child container, automatically. I don’t know if there are any guide lines as to when to use a Sub Kernel, but I would consider it when you have a custom facility or a factory class which stores a lot of types or if you need to customize the Kernel in some way. Using a container is as easy as creating the class and allowing a Kernel to be passed in a Constructor when needed.

private readonly IKernel _container = new DefaultKernel();</p>

public MyFactory() {

_container.Resolver.AddSubResolver(<span class="kwrd">new</span> CaseInsensitiveDependencyResolver());


public MyFactory(IKernel parent) : this()



Secondly, Customizing the Kernel with a CustomDependencyResolver, is easy and very useful when you need to do that. In this case I have a simple Resolver which looks at the additional parameter dictionary (passed to the Resolve Methods) and looks for a match ignoring case. I just needed to implement the ISubDependencyResolver interface which only has two methods, and register it on the container using Resolver.AddSubResolver (You can also overwrite the main Resolver when the Kernel is created). SubResolvers should return null if they didn't resolve the dependency, the main Resolver will throw an error if it's a required dependency.

internal class CaseInsensitiveDependencyResolver : ISubDependencyResolver
    public object Resolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model,
                          DependencyModel dependency)
        foreach (DictionaryEntry entry in context.AdditionalParameters)
            if (dependency.DependencyKey.Equals(entry.Key as string, StringComparison.InvariantCultureIgnoreCase))
                return entry.Value;
        return null;

    public bool CanResolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model,
                           DependencyModel dependency)
        if (context == null || context.AdditionalParameters == null)
            return false;

        foreach (DictionaryEntry entry in context.AdditionalParameters)
            if (dependency.DependencyKey.Equals(entry.Key as string, StringComparison.InvariantCultureIgnoreCase))
                return true;
        return false;

Again, as always, there’s more info on the CastleProject.org site.

Using flags in c#

Some notes about using enums as flags in C#. Use the FlagsAttribute on you enum. Have a None option set to 0. Have the first option start at 1, and each next value should be 2 times the previous one (I wonder what that pattern is called). Something like:

public enum PropertyOptions

None = 0,
IgnoreProperty = 1,
DisplayName = 2


Checking if a flag is set is done using the following syntax:

bool isIgnoreSet = (_options&PropertyOptions.IgnoreProperty) == PropertyOptions.IgnoreProperty;

Guidelines: http://msdn2.microsoft.com/en-us/library/ms229062.aspx

Using The Enum: http://dotnet.org.za/kevint/pages/Flags.aspx

Attribute Values on interfaces are not inherited

I guess it makes sense, that an interface is implemented, not inherited, but I think it’s confusing that interfaces are ignored when searching for custom attributes, using the GetCustomAttributes methods.

Attributes shouldn’t be thought of as part of an interface, strictly meta data in that case? No, I think, interfaces should have been searched, that member is part of an interface, and implementations should have access to that members metadata. If nothing else, custom attributes defined with inheritance should be required to be implements by the implementers.

Blogging With Windows Live Writer

I setup windows live writer today. I really like posting using this over the web interface. Not sure how to tag my posts yet… Oh, found it. At the bottom of the writer interface there is a categories list you can select your tags. You can also set the publish date there.

Live Writer: http://gallery.live.com/default.aspx?pl=8
Code Plug-in: http://gallery.live.com/liveItemDetail.aspx?li=1f57bd9b-a692-4593-9e9e-e2962d9c0eee&bt=9&pl=8

Setting up and starting with GIT (ignoring files)

I’m setting up GIT. So far I’m liking it.

To exclude files and directories I added the following to the .git/info/exclude file.


These sites have been helpful in getting started.


Git for windows: http://code.google.com/p/msysgit/downloads/list

Brutalist Framework