Lazy loading - what's the best approach?

前端 未结 7 874
慢半拍i
慢半拍i 2020-12-31 07:22

I have seen numerous examples of lazy loading - what\'s your choice?

Given a model class for example:

public class Person
{
    private IList

        
相关标签:
7条回答
  • 2020-12-31 07:38

    You can use the Lazy<T> class I talked about here : What is the proper way to inject a data access dependency for lazy loading?

    There is also link to a more detailed blog post there...

    0 讨论(0)
  • 2020-12-31 07:39

    You can use the Virtual Proxy pattern, along with the Observer pattern. This would give you lazy loading without the Person class having explicit knowledge about how Children are loaded.

    0 讨论(0)
  • 2020-12-31 07:42

    I just asked a related question here, but it was heavier on the Immutability & Thread Safety tack. Lots of good answers and comments. You may find it useful.

    0 讨论(0)
  • 2020-12-31 07:51

    Here is an example implementing lazy loading using the Proxy pattern

    The Person class that would live with the rest of your models. Children is marked as virtual so it can be overridden inside the PersonProxy class.

    public class Person {
        public int Id;
        public virtual IList<Child> Children { get; set; }
    }
    

    The PersonRepository class that would live with the rest of your repositories. I included the method to get the children in this class but you could have it in a ChildRepository class if you wanted.

    public class PersonRepository {
        public Person FindById(int id) {
            // Notice we are creating PersonProxy and not Person
            Person person = new PersonProxy();
    
            // Set person properties based on data from the database
    
            return person;
        }
    
        public IList<Child> GetChildrenForPerson(int personId) {
            // Return your list of children from the database
        }
    }
    

    The PersonProxy class that lives with your repositories. This inherits from Person and will do the lazy loading. You could also use a boolean to check if it has already been loaded instead of checking to see if Children == null.

    public class PersonProxy : Person {
        private PersonRepository _personRepository = new PersonRepository();
    
        public override IList<Child> Children {
            get {
                if (base.Children == null)
                    base.Children = _personRepository.GetChildrenForPerson(this.Id);
    
                return base.Children;
            }
            set { base.Children = value; }
        }
    }
    

    You could use it like so

    Person person = new PersonRepository().FindById(1);
    Console.WriteLine(person.Children.Count);
    

    Of course you could have PersonProxy take in an interface to the PersonRepository and access it all through a service if you don't want to call the PersonRepository directly.

    0 讨论(0)
  • 2020-12-31 07:54

    The best lazy loading is avoiding it ;) Thread safety is an immediate problem you'll have to handle. I have no count of how often I have seen production systems with 8 cpu cores run lazy loading 8 times for every single lazy loading pattern in use. At least on server startups all server cores have a tendency to end up in the same places.

    Let a DI framework construct it for you instead, if you can. And if you cannot, I still prefer explicit construction. So all sorts of AOP magic simply do not cut it with me, go for explicit construction outside the class. Don't put it inside the person class, just make a service that constructs the objects in the proper manner.

    Introducing "magic" layers that more or less transparently do these things seem like a nice idea, but I have yet to come across implementations that do not have unforseen and problematic consequences.

    0 讨论(0)
  • 2020-12-31 07:54

    I talked about a solution I use to accomplish lazy loading here

    0 讨论(0)
提交回复
热议问题