1

I'm writing a WCF service to return information about a person from Active Directory. As well as returning the person's details I also want it to return their manager's username and full name.

I began coding this as:...

[DataContract]
public class ADPerson
{
    Guid objectGuid;
    Guid managerObjectGuid;
    string username;
    string displayname;

    [DataMember]
    public string Username 
    { 
        get { return this.username; }
        set { this.username = value; }
    }
    [DataMember]
    public string DisplayName
    { 
        get { return this.displayName; }
        set { this.displayName = value; }
    }
    [DataMember]
    public ADPerson Manager
    { 
        get { return new ADPerson(this.managerObjectGuid); }
        set { this.managerObjectGuid = value.objectGuid; }
    }

    /* ... */

}

...but then realised that this has no stop condition; i.e. it will traverse all the way up the object graph until it reaches a user for whom no manager is defined (i.e. the CEO).

Is there a good way to put this stop condition in place whilst still being able to reuse the ADPerson class, or do I need to provide an alternative way of getting the manager's details (e.g. putting those details I want to be visible into their own fields and removing DataMember from the Manager property, or creating an ADManager class which presents a subset of the ADPerson fields which doesn't the manager's manager?

This is another case where solving the problem is simple, but knowing what the best solution to the problem is is vexing me.

Thanks in advance,

JB

2
  • 1
    Including all managers up to the top is not the worst include pattern. It should result in acceptable numbers. Oct 8, 2012 at 13:00
  • True, the depth is likely only 20 or so people in a worst case scenario, but I like to keep things relatively tight (i.e. having 80% of what's returned as redundant information isn't ideal - I'd rather just have the requested employee and their manager if possible).
    – JohnLBevan
    Oct 8, 2012 at 13:13

2 Answers 2

2

Using inheritance in XML is bound to get messy, given the circular references ("Employee A has Manager B, to print Manager B you'll have to print his emplyees, one of which is Employee A...").

This code demonstrates that, using the DataContractSerializer, this won't work:

[ServiceContract]
public class NestedDataService
{
    [OperationContract]
    public Person FindPerson()
    {
        var person = new Person { Name = "E.M. Ployee" };
        var manager = new Person { Name = "M.A. Nager" };

        manager.Employees = new List<Person>();
        manager.Employees.Add(person);

        person.Manager = manager;

        return person;
    }
}

[DataContract]
public class Person
{
    [DataMember]
    public String Name { get; set; }

    [DataMember]
    public Person Manager { get; set; }

    [DataMember]
    public List<Person> Employees { get; set; }
}

I would use some (the?) G/UUID from the AD and re-link them toghether at the client side using that ID. Then Employee A won't have a public ADPerson Manager property anymore (or at least not as far as the serializer is concerned), but a public UUID ManagerID which can be implemented like this:

public UUID ManagerID 
{ 
    get 
        { 
            return Manager != null ? Manager.UUID : null;
        }
}

This limits you in returning one person, because you'll have to send a list of ADPersons if there are people related to the requested person, in order to be able to look them up.

2
  • @HenkHolterman I've run into this issue a few times. See the edited question. The code shown will (at least at default settings) break the XmlSerializer and DataContractSerializer while looping infinitely.
    – CodeCaster
    Oct 8, 2012 at 13:09
  • Makes sense, but that would mean multiple WCF calls - i.e. one to get the individual, a second to get their manager. Since network's the slowest piece, I'm hoping to keep the number of calls down. Alternatively, if returning a list of objects from a single call (i.e. rather then serialising as a hierarchy I return all people sequentially, then tie them up in client code), I'd need to put in alternate logic to select just the records I'm interested in, which seems to add to the complexity?
    – JohnLBevan
    Oct 8, 2012 at 13:11
1

In my opinion, based on the information given, the best solution is to make the properties you want serialized visible on the class at hand and remove the DataMember attribute from the manager, like this:

[DataContract] 
public class ADPerson 
{ 
    Guid objectGuid; 
    Guid managerObjectGuid; 
    string username; 
    string displayname; 

    [DataMember] 
    public string Username  
    {  
        get { return this.username; } 
        set { this.username = value; } 
    } 
    [DataMember] 
    public string DisplayName 
    {  
        get { return this.displayName; } 
        set { this.displayName = value; } 
    } 
    public ADPerson Manager 
    {  
        get { return new ADPerson(this.managerObjectGuid); } 
        set { this.managerObjectGuid = value.objectGuid; } 
    } 
    [DataMember] 
    public string ManagerUsername
    {  
        get { return this.Manager.Username; } 
    } 
    [DataMember] 
    public string ManagerName
    {  
        get { return this.Manager.DisplayName; } 
    } 
} 

And they don't even have to be public if you don't want them to.

2
  • Thanks Mike - that fits with my initial assumptions / makes sense.
    – JohnLBevan
    Oct 8, 2012 at 13:24
  • @JohnLBevan, I'm glad I could be of assistance! Oct 10, 2012 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.