views:

47

answers:

2

I am trying to build a domain model that will allow me to manage Contracts.

The Contract class is my aggregate root, and it has a single property right now, which is Reviewers.

Reviewers, in the context of the Contract, each have a property to it's parent Contract, and a First Name, Last Name, and Login. The reason they have these properties is so I can have the user select which Reviewers they want on a Contract.

The database that I'm tying my domain model to already exists, and it's a legacy system that I'm trying to extend.

It has a Contract Table, and a Reviewer Table.

The thing I haven't mentioned up until this point, is that Reviewers are actually Users in the system. So there's actually a third table involved, which is Users.

I have been able to map my Contract Table easily with FNH.

It looks something like this:

public class ContractMapping: ClassMap<Contract>
{
    public ContractMapping()
    {
        Id(c => c.Id);
        HasMany(c => c.AdditionalReviewers);
    }
}

But I'm not sure how to model my Reviewers, because they are in fact Users as well. So my object model looks like this:

public class Reviewer: User
{
    public virtual Guid Id { get; set; }

    public virtual Contract Contract { get; set; }
}

public class User
{
    public virtual Guid Id { get; set; }

    public virtual string Login { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }        
}

I've been able to map my User class properly, and it looks something like this:

public class UserMapping: ClassMap<User>
{
    public UserMapping()
    {
        Id(u => u.Id);
        Map(u => u.Login);
        Map(u => u.FirstName);
        Map(u => u.LastName);
    }
}

and I believe I want to map my Reviewer class like this:

public class ReviewerMapping: SubclassMap<Reviewer>
{
    public ReviewerMapping()
    {
        Table("Reviewer");

        //Id(r => r.Id).Column("ReviewerId"); <- won't compile
        References(r => r.Contract).Column("ContractId");
    }
}

So the problem I'm having is this:

The relationship between the User table and the Reviewer table is one to many. Meaning, for a given User there may be many Reviewer records. Why? Because a User has to be a Reviewer for a specific Contract. This causes an issue with the mapping, though, because the primary key for my Reviewer and the primary key for my User are completely different values, by necessity.

Also, because of the way I'm using Reviewer, when I create a new Reviewer, what I'm really trying to do is to associate a User with a Contract. I am not trying to create an entirely new User in the database.

What is the correct way for me to map Reviewer, knowing that in my domain model it is a subclass of User?

+2  A: 

Sounds like a the Reviewer is not really modelling a person, but modelling a role or assignment the User takes on. I'd say your domain model is flawed in this aspect. Tweak Reviewer to be an association class between a User and a Contract.

Rich
@Rich I had it like that originally, but it seemed odd to me to have a class called Reviewer, and then have it contain a property that was a User, it just seemed wrong from a DDD perspective. From a DDD perspective, I need my Contract to be able to add Reviewers and remove them. Those Reviewers that the Contract can add are Users in the system, though.
Joseph
+1  A: 

I don't think Reviewer should inherit from User in the scenario you've described. I would have the Reviewer class hold a User object instead (composition over inheritance).

If it helps you conceptualize it better, rename Reviewer to Review. That way you can stop thinking about it as a User since it really isn't (multiple Reviewers in your current domain can be the same User, which doesn't make much sense).

Kevin Pang
@Kevin I see what you're saying, but the domain context boundaries that I've set for this problem are set only with a single Contract. And in that context, there can't be multiple Reviewers for the same User. That only exists outside my current domain context (because I'm dealing with a legacy system I guess). Maybe there's something about domain contexts that I'm not grasping from a DDD perspective.
Joseph
@Joseph Question: why not just have each Contract have a list of Users then instead of this extra Reviewer class that doesn't hold anything? If it does hold information, like the review data itself, then I still think renaming the class as Review and having it have a User as one of its properties is the proper way to model this domain.
Kevin Pang
@Kevin it will have extra data as soon as I get to my next task, which is to keep track of what User in the system actually assigned the Reviewer to the Contract. I did spend some more time thinking about this, and I think that your idea is right, but for my domain I think it is more correct to change the User class to a UserAccount class, and leave the Reviewer class alone. By changing it to UserAccount I think that it clarifies what is actually happening.
Joseph