Thank you, gentlemen. I now understand where my reasoning was incorrect. 

As you can tell, I am a complete NHibernate newb. Could you recommend any 
read material on NHibernate's proxy generation?

On Thursday, November 29, 2012 5:11:29 AM UTC-5, Oskar Berggren wrote:
>
> To elaborate, not initializing on method calls would only be safe if 
> all mapped items are properties with automatic backing fields. In 
> other circumstances there could be direct access to private fields 
> involved, and NH cannot detect when those are used. 
>
> /Oskar 
>
>
> 2012/11/29 cremor <[email protected] <javascript:>>: 
> > How should NHibernate know that you only access the Id in 
> > Equals/GetHashCode? It won't initialize the proxy if you don't override 
> > Equals/GetHashCode at all, but as soon as you override it you might 
> access 
> > every property in there, so it needs to initialize. 
> > 
> > 
> > On Wednesday, November 28, 2012 7:28:53 AM UTC+1, SirSirAaron wrote: 
> >> 
> >> Why does a dictionary lookup operation initialize a proxy if its 
> >> respective class's equals and gethashcode method only use its Id 
> property? 
> >> 
> >> namespace LookUpTest 
> >> { 
> >>     public abstract class Entity 
> >>     { 
> >>         public virtual int Id { get; set; } 
> >> 
> >>         public override int GetHashCode() 
> >>         { 
> >>             return Id.GetHashCode(); 
> >>         } 
> >> 
> >>         public override bool Equals(object obj) 
> >>         { 
> >>             var entity = obj as Entity; 
> >>             return entity != null && Id == entity.Id; 
> >>         } 
> >>     } 
> >> 
> >>     public class Project : Entity 
> >>     { 
> >>         public class ProjectMap : ClassMap<Project> 
> >>         { 
> >>             public ProjectMap() 
> >>             { 
> >>                 Id(x => x.Id).GeneratedBy.Assigned(); 
> >>             } 
> >>         } 
> >>     } 
> >> 
> >> 
> >>     public class User : Entity 
> >>     { 
> >>         public virtual Project Project { get; set; } 
> >>         public class UserMap : ClassMap<User> 
> >>         { 
> >>             public UserMap() 
> >>             { 
> >>                 Id(x => x.Id).GeneratedBy.Assigned(); 
> >>                 References(x => x.Project); 
> >>             } 
> >>         } 
> >>     } 
> >> 
> >>     [TestFixture] 
> >>     public class LookupTests 
> >>     { 
> >>         [Test] 
> >>         public void ShouldNotInitializeProxy() 
> >>         { 
> >>             Configuration config = null; 
> >>             ISessionFactory sessionFactory = 
> >> 
> Fluently.Configure().Database(SQLiteConfiguration.Standard.InMemory).Mappings(m
>  
>
> >> => { 
> >> 
> m.FluentMappings.Add<Project.ProjectMap>();m.FluentMappings.Add<User.UserMap>();}).ExposeConfiguration(cfg
>  
>
> >> => config = cfg).BuildConfiguration().BuildSessionFactory(); 
> >> 
> >>             Dictionary<Project, String> stringByProject = new 
> >> Dictionary<Project, string>(); 
> >>             User userRetrievedFromDB = null; 
> >>             using (ISession session = sessionFactory.OpenSession()) 
> >>             { 
> >>                 //create tables 
> >>                 new SchemaExport(config).Execute(true, true, false, 
> >> session.Connection, null); 
> >> 
> >>                 Project newProject = new Project {Id = 2}; 
> >>                 session.Save(newProject); 
> >> 
> >>                 User newUser = new User { Id = 1, Project = newProject 
> }; 
> >>                 session.Save(newUser); 
> >> 
> >>                 //force the save of the new project and new user to the 
> DB 
> >>                 session.Flush(); 
> >> 
> >>                 //add entry to dictionary 
> >>                 stringByProject[newUser.Project] = "test!"; 
> >> 
> >>                 //evict object from session to force retrieval from DB 
> >>                 session.Evict(newUser); 
> >>                 session.Evict(newProject); 
> >> 
> >>                 //get user1 from DB 
> >>                 userRetrievedFromDB = session.Get<User>(1); 
> >>             } 
> >>             //Able to use Id property without causing lazy 
> initialization 
> >>             Assert.That(userRetrievedFromDB.Project.Id == 2); 
> >>             //Should be able to do a dictionary lookup without 
> >> initializing the proxy granted gethashcode and equals only use the Id 
> >> property 
> >>             Assert.That(stringByProject[userRetrievedFromDB.Project] == 
> >> "test!"); //Could not initialize proxy exception! Why? 
> >>         } 
> >>     } 
> >> } 
> >> 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "nhusers" group. 
> > To view this discussion on the web visit 
> > https://groups.google.com/d/msg/nhusers/-/7ce4T4cBUgcJ. 
> > 
> > To post to this group, send email to [email protected]<javascript:>. 
>
> > To unsubscribe from this group, send email to 
> > [email protected] <javascript:>. 
> > For more options, visit this group at 
> > http://groups.google.com/group/nhusers?hl=en. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/nhusers/-/qsOj0AbZNGIJ.
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.

Reply via email to