0

I have a class where some properties are going back to the database, ONLY when they are called. So, let consider the following example:

class User
{
   public int UserID { get; internal set; }
   public string UserName { get; set; }
   public Role[] Roles
   {
      get
      {
         if (this.UserID > 0)
            return RoleRepository.GetByUserID(this.UserID).ToEntityArray();
         else
            return RoleRepository.GetByUserName(this.UserName).ToEntityArray();
      }
   }
}

There is also a Role class, but that's probably not important. The 'Roles' property in the 'User' class is kind of like lazy loading. It is only retrieving the info when that property is accessed. The question is how can I use JQuery to ensure that the lazy loading occurs or is that even possible? Any articles on this concept would also be helpful.

Thanks

2
  • Which technology are you using to interact with your C# from jQuery? Commented Nov 12, 2010 at 14:13
  • Do you mean like windows forms or ASP.NET? Or do u mean the back-end? For the front end, its ASP.net mvc and backend its sql server '05. Commented Nov 12, 2010 at 15:10

2 Answers 2

2

I don't see what jQuery has to do with this at all.

Touching that Property will trigger the lazy-load.

If you use JQuery to call some HttpHandler/WebMethod/ActionMethod/etc, as soon as you try to iterate through or access that Property it is going to load that data

Also, rather than having that biz logic in your domain object you could try a delegate approach to keep "how" data is bound to that Property in its proper place

class User
{
   public int UserID { get; internal set; }
   public string UserName { get; set; }

   public delegate Role[] GetRolesDelegate(int userId, string username);

   public GetRolesDelegate GetRoles { get; set; }

   Role[] _roles;
   public Role[] Roles
   {
      get
      {
          if (_roles == null)
          {
              _roles = GetRoles == null 
                  ? new Role { } 
                  : GetRoles(UserID, UserName).ToEntityArray();
          }
          return _roles;
      }
   }
}

Then you could bind that like so:

User user = new User();

user.GetRoles = RoleRepository.GetRoles;
Sign up to request clarification or add additional context in comments.

Comments

0

Like hunter I'm not sure what jQuery has to do with this. To make the Roles property lazy load add a private backing property that stores the value the first time it is accessed from the repository. Then, for subsequent accesses, you can just return that private backing variable, rather than going through the RoleRepository. Something like:

class User
{
   public int UserID { get; internal set; }
   public string UserName { get; set; }
   private Role[] _roles;

   public Role[] Roles
   {
      get
      {
         if (_roles == null)
         {    
             if (this.UserID > 0)
                _roles = RoleRepository.GetByUserID(this.UserID).ToEntityArray();
             else
                _roles = RoleRepository.GetByUserName(this.UserName).ToEntityArray();
         }

         return _roles;
      }
   }
}

2 Comments

You mean if (_roles == null) ? If not, it's not lazy-loading, it's never-loading :)
could call that bone-idle-loading ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.