Data Types

These data types have mapping support
  • string, string[], and Collection<string>
  • bool (nullable)
  • numeric (int, long, decimal, double, etc.) (nullable)
  • Guid (nullable)
  • DateTime, DateTime[], and Collection<DateTime> (defaults to ldap format yyyyMMddHHmmss.0Z, but supports file time and customizable formats) (nullable)
  • Enums (nullable)
  • byte[], byte[][], and Collection<byte[]>
  • SecurityIdentifier (objectsid in Active Directory)
  • X509Certificate, X509Certificate[], and Collection<X509Certificate>
  • X509Certificate2, X509Certificate2[], and Collection<X509Certificate2>
  • Custom data types (see last section)
DateTime Mapping
DateTimes can have different formats between LDAP servers. Most store it as either a generalized time with the format yyyyMMddHHmmss.0Z or in file time format. DateTimes default to yyyyMMddHHmmss.0Z, but you can map as a file time or a custom format.

Optionally you can configure value forwarding when mapping via ClassMap. This is useful for values that are valid within the directory, but cannot be converted to a DateTime.
Class Map
public class Role
{
    public DateTime? WhenCreated { get; set; }
    public DateTime? LastUpdated { get; set; }
}

public class RoleMap : ClassMap<Role>
{
    public RoleMap()
    {
        Map(x => x.WhenCreated).DateTimeFormat(null)
             .DirectoryValue("9223372036854775807").Returns(null)
             .InstanceValue(DateTime.MinValue).Sends("9223372036854775807")
             .InstanceValueNullOrDefault().Sends("9223372036854775807"); 
         //maps as file time
        Map(x => x.LastUpdated).DateTimeFormat("yyyyMMddHHmmssZ"); //maps in a custom format.
    }
}
Attribute Map
public class Role
{
    [DirectoryAttribute(DateTimeFormat = null)] //maps as file time
    public DateTime? WhenCreated { get; set; }

    [DirectoryAttribute(DateTimeFormat = "yyyyMMddHHmmssZ")] //maps in a custom format.
    public DateTime? LastUpdated { get; set; }
}

Enum Mapping

Mapped enums default to strings, but you can also map them as integers.
Class Map
public enum RoleType
{
     Distribution = 1,
     Group = 2,
     Other = 3
}

public class Role
{
    public RoleType? RoleType { get; set; }
}

public class RoleMap : ClassMap<Role>
{
    public RoleMap()
    {
        Map(x => x.RoleType).EnumStoredAsInt();
    }
}
Attribute Map
public class Role
{
    [DirectoryAttribute(EnumStoredAsInt= true)]
    public RoleType? RoleType { get; set; }
}

Custom Property Mapping

Sometimes you need to transform directory data types into a custom .Net type (i.e. component classes). Below is just an example since you want to leave objectguid maintenance up to the directory.
Class Map
How much you implement is based on how you plan to use the property, At the minimum you need to map ConverFromDirectoryUsing. If you plan to do updates then you have to map ConvertToDirectoryUsing. And if you plan to query using the property then you have to implement ConvertToFilterUsing.

MapCustom(s => s.ObjectGuid).Named("objectGuid")
    .ConvertFromDirectoryUsing(directoryAttribute =>
    {
        var bytes = directoryAttribute.GetValues(typeof(byte[]))[0] as byte[];
        return new Guid(bytes);
    })
    .ConvertToFilterUsing(guid => guid.ToStringOctet())
    .ConvertToDirectoryUsing(guid => guid.ToByteArray())
    .CompareChangesUsing((guid1, guid2) => guid1.Equals(guid2));
Attribute Map
public class Role
{
    [DirectoryAttribute]
    private byte[] ObjectGuid { get; set; }

    public Guid Guid
    {
         get { return new Guid(ObjectGuid); }
         set { ObjectGuid = value.ToByteArray(); }
    }
}

Last edited Jun 12, 2012 at 2:35 AM by MadHatter22, version 14