Class Map

You can create a mapping class to support POCOs. I'll use the classes from the previous page. You can opt out of a property mapping for a subclass by using the Ignore() method. Mapping can be performed either in the constructor or within the PerformMapping method. However, PerformMapping must always be implemented.

The framework will call PerformMapping for you. It gives you the option to pass in the NamingContext, ObjectCategory, and ObjectClasses at a later time if you don't want to map that information up front.

Mapping ObjectClasses and / or ObjectCategory gives you the option to include them in all queries or ignore them unless otherwise specified by IgnoreOC or IncludeOC.
Basic Mapping
public class Role
{
    public string DistinguishedName { get; set; }
    public string CommonName { get; set; }
    public Collection<string> Members { get; set; }
}

public class RoleMap : ClassMap<Role>
{
    public override IClassMap PerformMapping(...)
    {
        NamingContext("OU=Roles,DC=example,DC=com");
        ObjectCategory("group", true);
        ObjectClasses(new[] {"top", "group"}, false);

        DistinguishedName(x => x.DistinguishedName);
        Map(x => x.CommonName)
            .ReadOnly()
            .Named("cn");
        Map(x => x.Members);

        return this;
    }
}
Subclass Mapping
public class Server
{
    public string DistinguishedName { get; set; }
    public string Cn { get; set; }
    public Guid Guid { get; set; }
    public string OperatingSystem { get; set; }
}

public class EmailServer : Server
{
    public string Platform { get; set; }
}

public class WebServer : Server
{
    public string Platform { get; set; }
    public bool? IsLoadBalanced { get; set; }
    public string LoadBalancerName { get; set; }
}

public class ServerMap : ClassMap<Server>
{
    public override IClassMap PerformMapping(...)
    {
        NamingContext("OU=Servers,DC=example,DC=com");
        ObjectClass("server");

        DistinguishedName(x => x.DistinguishedName);
        Map(x => x.Cn)
            .ReadOnly();
        Map(x => x.Guid)
            .Named("objectguid")
            .StoreGenerated();
        Map(x => x.OperatingSystem);

        return this;
    }
}

public class EmailServerMap : SubClassMap<EmailServer, Server>
{
    public EmailServerMap() : base(new ServerMap())
    {
    }

    public override IClassMap PerformMapping(...)
    {
        NamingContext("OU=Email Servers,OU=Servers,DC=example,DC=com");
        ObjectClass("Server");

        Map(x => x.Platform);

        return this;
    }
}

public class WebServerMap : SubClassMap<WebServer, Server>
{
    public WebServerMap() : base(new ServerMap())
    {
    }

    public override IClassMap PerformMapping(...)
    {
        NamingContext("OU=Web Servers,OU=Servers,DC=example,DC=com");
        ObjectClass("Server");

        Map(x => x.Platform);
        Map(x => x.IsLoadBalanced);
        Map(x => x.OperatingSystem);

        return this;
    }
}

Last edited Jun 12, 2012 at 3:33 AM by MadHatter22, version 13