How to create a NHibernate proxy object with some initialized fields (except Id)?

I want to create a proxy object, similar to ISession.Load being returned, but with some initialized fields. For other properties, upon access, the proxy will retrieve the entire object from the database. Consider the following example:

public class User
{
    protected User() {

    }

    public User(int id, string username, string email) {
        // ... 
    }

    // initialize the following fields from other datasources
    public virtual int Id { get; set; }
    public virtual string UserName { get; set; }
    public virtual string Email { get; set; }

    // the rest of fields when accessed will trigger a select by id in the database
    public virtual string Field1 { get; set; }
    public virtual string Field2 { get; set; }
    public virtual DateTime Field3 { get; set; }
    public virtual ISet<Comment> Comments { get; set; }
}

Id, UserName, Email are well known in my case, so I could create a proxy object containing these fields, and for the rest, the default proxy server behavior. In addition to throwing an exception if this identifier is not found in the database, I could throw an exception if the previously initialized fields do not match or overwrite them silently. I am using NHibernate.ByteCode.Castle for proxy factories.

: , , (, lucene) . , , , -, . , . SELECT N + 1 . , , :

        // create User object proxy with some fields initialized
        var user = Session.Load<User>(5, new { UserName = "admin", Email = "admin@company.com" });
        Console.WriteLine(user.Id); // doesn't hit the database
        Console.WriteLine(user.UserName); // doesn't hit the database
        Console.WriteLine(user.FullName); // doesn't hit the database
        if (somecondition) {
            Console.WriteLine(user.Field1); // fetches all other fields 
        }
+3
3

. - , (Criteria, Hql LINQto NH) . .

0

- , ; ( ) , . 'lazy=true'.

, , - , - , ( - Field1..Field20).
:
1. component,
2. DTO .

0

lazy = "true" ( Not.LazyLoad() Fluent NHib) Field1, Field2, Field3, Comments , N + 1.

: lazy = "false" UserName, Email

-1

All Articles