Josh
Josh

Reputation: 16567

Fluent NHibernate - mapping portions of a single table into multiple classes with a few shared fields

I have a situation where a User table has lots of information about a user (first name, last name, email, etc) in addition to credential data (username, password, prior passwords, etc). Normally I would separate Authentication from Personalization, but this table is long established and I can't do that.

It bothers me to have 1 user object with all this data in it that is being passed around my application. I instead want to split this out into two objects: User and UserCredentials. User can be freely passed around my application without leaking any of the passwords, while UserCredentials will be used only in my services backend for validating and authenticate a user.

That seems simple enough. However, some fields need to be shared across both these objects. Will this cause issues with nhibernate when one of my objects is updated? For instance if a username change takes place and both have the username in the object, will both objects be updated?

Upvotes: 2

Views: 475

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52745

If User and UserCredentials represent two views of the same entity, do not create two classes for them. It'll only lead to pain.

Alternatives:

  • Use a component to expose the "public" part of the entity and pass that
  • Use a DTO to contain exactly the fields you need and pass that

Upvotes: 3

Related Questions