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/-/VJFNSYNXru0J.
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