Skip to content
Chris Dibbern edited this page Nov 20, 2018 · 6 revisions

There are several ways to create directory entry models, but first, you should decide if you need to. If you are content referencing your properties by key, then you can use the default Linq2Ldap.Core.Models.Entry model.

Expression<Func<Entry, bool>> expr
  = e => e["mail"].EndsWith("gmail.com") && e["samaccountname"] == myName;

This is a simple way to get started.

Custom models

You can also subclass Entry in order to add custom-typed, convenience properties that insulate your code base from any changes. They can be used directly in an Expression<Func<TEntry, bool>>.

All properties must be marked with [LdapField(string key)]. Any that aren't marked will be ignored. The key will be used to find the value in the Attributes bag, and the property's value will be set at load-time. Single-valued property types will use the first entry in the value list, if available, while many-valued types will use all of them.

using Linq2Ldap.Attributes;
using Linq2Ldap.Models;

namespace MyApp {
    public class MyModel: Entry {
        [LdapField("mail")]
        public LdapString Mail { get; set; }

        [LdapField("alt-mails")]
        public LdapStringList AltMails { get; set; }
    }
}

With respect to the second property, AltMails, using the type LdapStringList will let you run LINQ Expressions over multiple values in the same way LDAP does:

(MyModel m) => m.AltMails == "one-of-them@example.com"; // returns true, if any AltMails matches.

As you can see, the many-valued property types overload == to implicitly search the list like the equivalent RFC 1960 filter: (alt-mails=one-of-them@example.com). When compiled to a filter, that's exactly what the output would be.

Advanced

If the default Entry class doesn't suit your needs, you could also provide your own, custom implementation of its interface, IEntry. Likewise, if the available field types don't suit, you can subclass BaseLdapType<,> and BaseLdapManyType<,>. Take a look at the implementation of LdapInt, LdapString, and LdapStringList for guidance. Tests in Linq2Ldap.Core.Tests/PublicAPI guard this extensibility.

Clone this wiki locally