http://fabiomaulo.blogspot.com/2009/03/ensuring-updates-on-flush.html
2009/5/27 David Perfors <[email protected]> > > It was very easy to implement the PreLoad- and PostLoadEventListeners. > It now works as I wanted. > > On May 27, 10:52 am, David Perfors <[email protected]> wrote: > > Using the state pattern won't fix the problem. And I find it quite > > difficult to persist a state pattern. > > > > I couldn't find the example on nhforge, but I think I found another > > possible solution... > > I found an article that describes Audit Log using NHibernate Events. > > Now I thought, are there Loading events? and looking in the source I > > found the IPreLoad and IPostLoad, so I can implement those interfaces > > and set an initialize property that I will check in the CheckState > > method. > > Are there any things I should think about? > > > > > If your real world case is more complex than your example (?), I would > consider impl state pattern rather than your enum. > > > > > << giving the object an initialization state>> > > > IIRC - I've seen some example at nhforge how to use a parameterized > ctor, but if you want to keep your design, it's seems a lot easier mapping > the fields instead. > > > > > -----Original Message----- > > > From: [email protected] [mailto:[email protected]] On > Behalf Of David Perfors > > > Sent: den 26 maj 2009 16:28 > > > To: nhusers > > > Subject: [nhusers] Loading objects with readonly state > > > > > Hi, > > > > > In my application I want to do the following: > > > public enum State > > > { > > > Writable, > > > Readonly > > > } > > > > > public class DomainObject > > > { > > > private string _name; > > > public DomainObject() { } > > > public int Id { get; private set; } > > > public State State { get; set; } > > > public string Name > > > { > > > get { return _name; } > > > set > > > { > > > CheckState(); > > > _name = value; > > > } > > > } > > > private void CheckState() > > > { > > > if (State != Status.Bewerken) > > > throw new NotSupportedException("You can't do this in > > > readonly mode"); > > > } > > > } > > > > > So in almost every setter that I want to check I will do this check. > > > I can save this object with the following mapping file: > > > > > <hibernate-mapping assembly="Domain" > > > default-cascade="save-update" > > > default-lazy="false" > > > namespace="Domain" > > > xmlns="urn:nhibernate-mapping-2.2"> > > > <class name="DomainObject"> > > > <id name="Id" type="int"> > > > <generator class="sequence" /> > > > </id> > > > <property name="State" /> > > > <property name="Name" /> > > > </class> > > > </hibernate-mapping> > > > > > But when I have saved the DomainObject with a Readonly state I can't > > > load it correctly because the state will prevent this. > > > Of course I can solve this by mapping it to the field instead of the > > > property, but I was wondering whether it is possible to do it another > > > way, for example giving the object an initialization state which will > > > prevent the CheckState method from throwing the exception... > > > > > Any ideas? > > > > > David > > > > > > > -- Fabio Maulo --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
