开发者

How to write to a column not in the domain entity?

开发者 https://www.devze.com 2023-01-14 13:44 出处:网络
In our SOA application we have several customers\' data in the same database and in the same tables. To control access, we have a column that describes this relationship. This column isn\'t represent

In our SOA application we have several customers' data in the same database and in the same tables.

To control access, we have a column that describes this relationship. This column isn't represented in the domain model, however. I know about query only properties - so, for querying - it is working perfectly.开发者_StackOverflow社区

However, how do I write to this column when saving new rows/entities?

Code:

public class Customer
{
    public virtual long Id{get;private set;}
    public virtual string AccountNumber{get;set;}
}

Mapping:

<class Name="Customer">
  <id name="Id">
    <generator class="native"/>
  </id>
  <property name="AccountNumber"/>
  <property name="Partition" access="noop" not-null="true"/>
</class>


I'm not sure to understand from where the value that should be inserted will come from if not set when creating entities (assuming they would provide the appropriate attribute of course) but...

One option would be to use an IInterceptor to alter the SQL during OnPrepareStatement and add whatever you want in case of INSERT.

using System;
using System.Collections;
using NHibernate.SqlCommand;
using NHibernate.Type;

namespace NHibernate
{
    [Serializable]
    public class MyInterceptor : IInterceptor
    {
        ...

        public SqlString OnPrepareStatement(SqlString sql)
        {
            ///Do what is required here to alter the SQL string
            return sql;
        }
    }
}

And apply your interceptor either at the session level or globally.

Reference

  • NHibernate Reference Guide
    • 9.10. Interceptors


Is there a problem including the Partition property as part of the domain?

Personally I would add it as part of the domain model and save yourself a whole lot of pain. The very limited view that we have of the problem is that it appears that you would benifit from the Partion property being a part of the domain either as part of the customer object or refactored to a superclass of customer.

Having said that if it must stay the way it is, look at implementing either IPreInsertEventListner / IPreUpdateEventListener or the IPostInsertEventListener / IPostUpdateEventListener You could check for the Customer class and execute a SQL query that updates the Partition column in the database. It appears that most people use these event handlers for audit logging, but they should still be applicable for this situation.

Ayende's Blog - NHibernate IPreUpdateEventListener & IPreInsertEventListener

0

精彩评论

暂无评论...
验证码 换一张
取 消