• 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()
    {

    parent.AddChildKernel(_container);
    

    }

    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:

    [Flags]
    public enum PropertyOptions
    {</p>
    
    
    
    
    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.

    build
    _ReSharper*
    *.user
    *.resharper
    *.suo
    bin
    obj

    These sites have been helpful in getting started.

    http://lwn.net/Articles/245678/
    http://www.kernel.org/pub/software/scm/git/docs/tutorial.html

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

  • Tortoise SVN global ignores

    I know tons of people have already posted their global ignores list, but here’s mine:

    */bin */obj *.user *.suo *.webinfo bin obj *.dll *.pdb *.exe  _ReSharper* */_ReSharper* *.resharper *.DLL

    http://tortoisesvn.tigris.org/ – Tortoise Explorer Client for SVN

  • NS3Library now has support for SQLiteS3Service

    More refactoring and support added for SQLite. There are now two IS3Service implementations, one using Amazons S3 and one using SQLite locally. This allows for easier testing (and free) testings and just a config change to your IoC config for deployment.

    The IS3Service Interface has been simplied and cleaned up as well. It’s not perfect, but it is open source, so feel free to make changes.

    http://sqlite.phxsoftware.com/ – You can get the SQLite for .net from there.