Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 524071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:31:46+00:00 2026-05-13T08:31:46+00:00

I’m testing out NHibernate to be the solution to my company’s ORM needs. To

  • 0

I’m testing out NHibernate to be the solution to my company’s ORM needs. To do this, I’ve produced a small test model based on a school, providing some useful edge-cases for NHibernate to handle.

I’m having problems finding out how to map a custom structure as a component of an entity without using theIUserType interface. I should stress that it is an important requirement that the domain classes are in a separate assembly from our NHibernate code, and that the domain assembly does not have a reference to NHibernate.

The custom structure is Time, used to represent a time of day in hours and minutes. It’s a very simple immutable structure and only provided to illustrate the problem of a custom structure. The constructor takes a single argument which is hours and minutes, as an integer in the form hhmm.

public struct Time
{
    public Time(int hoursAndMinutes)
    {
        // Initialize Structure //
    }

    public int Hours { get; private set; }

    public int Minutes { get; private set; }

    public int HoursAndMinutes { get; private set; }
}

This structure is used as a component of the Lesson class to store the time of day the lesson starts:

public class Lesson
{
    public int ID { get; private set; }
    public Teacher Teacher { get; internal set; }
    public DayOfWeek Day { get; set; }
    public Time StartTime { get; set; } // <-- Custom Type
    public int Periods { get; set; }
}

This class maps directly to this table:

CREATE TABLE Lessons
(
    ID INT,
    Subject NVARCHAR(128)
    TeacherID INT,
    Day VARCHAR(9),
    StartTime INT, // <-- Maps to custom type.
    Periods INT
)

I am looking for a way to map this structure as a component of the Lesson class, so that NHibernate will read a property value on the structure (like any other component) to get a value for the column, but will initialize a new instance of the structure by passing the column value to the constructor when reading the value from the column into the entity.

If you have any suggestions, that’d be super. If you want to tell me this can’t be accomplished without using IUserType, that’s a fine answer too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:31:46+00:00Added an answer on May 13, 2026 at 8:31 am

    To my knowledge, there are three plans of attack.

    • You can map the component directly into properties of the custom type. In this example, have NHibernate set the HoursAndMinutes property, change the code in that property’s setter to update the Hours and Minutes properties appropriately, and have the constructor just call this.HoursAndMinutes = hoursAndMinutes; so the same code to update the Hours and Minutes properties gets executed regardless of whether the constructor is used or the setter on the HoursAndMinutes property is used. Would you write it that way if you weren’t using an ORM and knew it was going to diddle with that property? Probably not. But it’s not the end of the world and a comment would explain everything.

    • You write an IUserType or ICompositeUserType implementation. Really, they exist precisely for this scenario and give you the flexibility to instantiate the structure however you please in the NullSafeGet() implementation and extract the data however you please in the NullSafeSet() implementation. Put it in another assembly, say MyModel.NHibernateCrap.dll, if you like. There is no need for your model/domain to be aware that the IUserType implementation or NHibernate exists–that is all for the mapping file to specify.

    • You use the code-based workaround described by Miki Watts in his answer. That is, your component in the NHibernate mapping maps to fields or private properties in your model type that do some magic hand-waving to convert them into public properties that the application uses, and vice versa. (It’s similar to the first option I provide; the only difference is that in his scenario the field is a workaround that lets the legacy database leak into the class implementation, but not to other parts of the application or model. For small, isolated cases, or if the legacy database is just a Fact of Life, then I think this is perfectly reasonable.)

    To directly and realistically answer your question, NHibernate is not going to call a constructor that has parameters, period–that is simply how it works, it news up an object and then starts setting and reflecting on it–unless you start doing strange things with proxies or tell it to use your IUserType implementation. There is no mechanism for saying something like <constructor><arg>HoursAndMinutes</arg></constructor> in a mapping file or anything like that. Stop worrying and love the bomb.

    Since IUserType is the mechanism provided by NHibernate for doing such things, I don’t really understand why one wouldn’t want to use it.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.