Sunday, December 4, 2011

LINQ To LDAP: Configuration

There have been more than a few questions about the use of static properties within LdapConfiguration. This was intended for simplicity, but it's always nice to have an option for more complex applications.

With the recent changes you now can control the lifetime of your configuration as well as have multiple configurations within a project. Configurations are self contained with their own DirectoryMapper and ConnectionFactory. You can still choose to have a global configuration by calling UseStaticStorage on LdapConfiguration.
var config = new LdapConfiguration()
    .AddMapping(new AttributeClassMap<User>())
    .LogTo(new SimpleTextLogger(Console.Out))


//will look for the static Configuration property on LdapConfiguration
using (var context = new DirectoryContext())

//the next two statements construct a DirectoryContext in the same manner
using (var context = config.CreateContext())

using (var context = new DirectoryContext(config))

//will create a LdapConfiguration local to this instance.  
//As a result, no mappings can be cached beyond the lifetime 
//for this DirectoryContext.
using (var context = new DirectoryContext(new LdapConnection("localhost"), true))