Pattern for retrieving complex object graphs with Repository Pattern with Entity Framework

前端 未结 2 1064
醉话见心
醉话见心 2021-02-01 09:25

We have an ASP.NET MVC site that uses Entity Framework abstractions with Repository and UnitOfWork patterns. What I\'m wondering is how others have implemented navigation of co

2条回答
  •  庸人自扰
    2021-02-01 10:25

    It depends on how much of the information you're using at any one time.

    For example, if you just want to get the country name for a person (person.Pathway.Country.Name) what is the point in hydrating all of the other objects from the database?

    When I only need a small part of the data I tend to just pull out what I'm going to use. In other words I will project into an anonymous type (or a specially made concrete type if I must have one).

    It's not a good idea to pull out an entire object and everything related to that object every time you want to access some properties. What if you're doing this once every postback, or even multiple times? By doing this you might be making life easier in the short term at the cost of you're making your application less scalable long term.

    As I stated at the start though, there isn't a one size fits all rule for this, but I'd say it's rare that you need to hydrate that much information.

提交回复
热议问题