Search code examples
c#nhibernatefluent-nhibernate

Nhibernate/C# - Set Nhibernate to decode all text inputs before SQL?


The site I am working on is mostly secure data entry and I am using the following workflow:

  1. User Enters Text
  2. JS encodeURI(text)
  3. Ajax to C#
  4. C# calls nHibernate method
  5. nHiberate executes SQL
  6. returns to C#
  7. returns to JS
  8. JS decodeURI

I am wondering if it is possible to set nHibernate to decode all text fields before sending them to SQL without having to manually decode every single text field. I would like to do this because the reports I generate in SQL contain the encoding and is unreadable.

Again, I am looking for some sort of global setting for nHiberate to decode all text fields before sending them to SQL.

Or perhaps a way to decode before generating the report?

Does anyone have any ideas?


Solution

  • You could create a custom type that encode/decode when you set/get the property. You'll need to derive from IUserType and register your type with NHibernate. Here's an example en encrypted string

    https://gist.github.com/jasondentler/1170933#file-encryptedstring-cs

    Alternatively you could write EventListeners and do your encode/decode there.

    Here's the documentation on EventListeners

    http://nhibernate.info/doc/nhibernate-reference/events.html

    If I were you I'd go down the Custom type route. It's less code to maintain and is more of what you are looking for.