I'm working with a database that has a lot of "versioned" tables like this:
Code:
CREATE TABLE Person (
Person_ID INT
)
CREATE TABLE PersonInfo (
Person_ID INT,
Version INT,
FirstName VARCHAR(50),
LastName VARCHAR(50)
)
The idea is that we never actually update a person; instead, any time a person's information changes, we insert a new row into the PersonInfo table with an incremented Version. We also have views (e.g., vwPerson_LatestVersion) which return the most recent version for each person.
I don't want this versioning business to exist in my object model because it hardly ever matters. My solution has been to map classes to the *_LatestVersion views and provide custom sql-insert and sql-update code to perform the acrobatics to insert and update people. For example:
Code:
<sql-update>
DECLARE
@Version AS INT = ?,
@FirstName AS INT = ?,
@LastName AS INT = ?,
@Person_ID AS INT = ?
INSERT INTO PersonInfo ( Person_ID, Version, FirstName, LastName )
VALUES
(
@Person_ID,
( SELECT MAX(Version) + 1 FROM PersonInfo WHERE Person_ID = @Person_ID ),
@FirstName,
@LastName
)
</sql-update>
Everything is working perfectly, but it feels dirty.
The things I'm interested in are:
1) Is there a way to use named parameters rather than relying on the order in which NHibernate decides to pass them?
2) Is there a way to do this programatically, perhaps by implementing my own IEntityPersister? (That interface is ginormous!)
3) Is there an option that I'm just missing?
Any feedback would be very appreciated!
Thanks
- chad