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) {
}
public virtual int Id { get; set; }
public virtual string UserName { get; set; }
public virtual string Email { get; set; }
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 .
, , :
var user = Session.Load<User>(5, new { UserName = "admin", Email = "admin@company.com" });
Console.WriteLine(user.Id);
Console.WriteLine(user.UserName);
Console.WriteLine(user.FullName);
if (somecondition) {
Console.WriteLine(user.Field1);
}