Hi, if you want reference equality for the instances returned by NHibernate an the static readonly instances in your class, you could create a static dictionary of the int Id to your PricingStrategy instances inside the PricingStrategy class. On each constructor call you directly add the instance to the dictionary. You then provide a static Get method for accessing those instances by Id.
Finally you don't map your class as an entity with foreign key but as an Integer with an IUserType that uses the static Get method in the NullSafeGet. In the NullSafeSet you just set the PricingStrategy's Id in the IDbCommand. Also see some NHibernate Tests for examples for writing IUserTypes. You even don't need the Equals and GetHashCode methods because you get reference equality without any additional effort. With this you can use the strategy pattern with your PricingStrategy class (you could even do subclassing and implement whatever you want as long as you keep your integer Ids stable). CSharper On 25 Apr., 22:41, Chris B <[email protected]> wrote: > Hi, > > I am trying to map an enumeration class (something similar to Jimmy > Bogard's idea > here:http://lostechies.com/jimmybogard/2008/08/27/strategies-and-discrimin.... > > One difference is that in my scenario, my PricingStrategy class is > implemented like this (notice I do not have separate subclasses for > each PricingStrategy): > > public class PricingStrategy : Enumeration<PricingStrategy> > { > public static readonly PricingStrategy FullPrice = new > PricingStrategy(1, "Full price"); > public static readonly PricingStrategy LowStock = new > PricingStrategy(2, "Low stock"); > > public int Id { get; protected set; } > public string Name { get; protected set; } > > protected PricingStrategy(int id, string name) > { > // omitted > } > > // overrides of Equals, GetHashCode, ==, and != omitted > > } > > With my current mappings, I get a NonUniqueObjectException. I am > fairly certain that this is because: > > var fromSession = > session.Get<PricingStratey>(PricingStrategy.FullStock.Id); > Console.WriteLine(object.ReferenceEquals(fromSession, > PricingStrategy.FullStock); // prints false > > I end up with two different objects that are really the same loaded > because sometimes the session loads these objects, and sometimes I > want to explicitly reference on of them via the public static > properties (in my core domain classes, I don't have access to the > session). > > So far my best shot has been to create an IUserType, but I am > struggling with correctly implementing NullSafeGet(). I haven't even > tried NullSafeSet yet (not sure if I will need to since these types > are immutable). > > In case it helps, I am using NHibernate 3.2 and mapping by code. > > My questions are: > 1. Is this possible? > 2. Is there a better way to do this than with an IUserType? > 3. If IUserType is the best way to do this, I need some pointers w/ > NullSafeGet (and probably a few other methods as well). > > Thanks, > Chris B -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
