Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] What is the indented usage of populating entity properties? #14488

Closed
KieranDevvs opened this issue Jan 22, 2019 · 2 comments
Closed
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@KieranDevvs
Copy link

KieranDevvs commented Jan 22, 2019

Im currently using EF Core on a large project. My main problem with using it is, I'm unaware of what the intended process is for obtaining data. Is it best practice to let each method that uses the entities and their nested types, assume that the nested objects are populated from outside of the method? Or is it intended that each method should be responsible for obtaining the data that it needs?

Visual example for collecting the data from the start of a unit:

public void main(string[] args) 
{
    var buildingWithIncludedData = _context.Building
             .Include(a => a.b)
             .Include(b => b.c)
             .Include(d => d.e)
             ...
             .Single();

    ProcessBuilding(buildingWithIncludedData);
}

public void ProcessBuilding(Building building) {
    var specifiedPeople = building.People.Where(x => condition);
    building.SomeProperty = "something";

    DoSomethingWithPeople(specifiedPeople);
}

public void DoSomethingWithPeople(IEnumerable<Person> people) {
    foreach(var person in people) {
        person.DoMethod();
    }
}

Visual example for for obtaining the relative data within each method:

public void main(string[] args) 
{
    var buildingWithIncludedData = _context.Building
             .Single();

    ProcessBuilding(buildingWithIncludedData);
}

public void ProcessBuilding(Building building) {
    var specifiedPeople = _context.People.Where(x => x.BuildingId == building.Id);
    building.SomeProperty = "something";

    DoSomethingWithPeople(specifiedPeople);
}

public void DoSomethingWithPeople(IEnumerable<Person> people) {
    foreach(var person in people) {
        person.DoMethod();
    }
}

My assumption is that its wiser to have the data present before beginning any logic as one large query is more optimal compared to the alternative several smaller queries (which is more taxing on the database).

@tuespetre
Copy link
Contributor

It's really going to be a case-by-case thing. The ORM is a nice layer to work with but in the end it's just a tool to alleviate the burden of managing a load of bespoke SQL statements and data access boilerplate code. Just find something that works, and if you have a true issue with performance or something, you'll have to work out those details for that particular case. It's the wild west and there are no rules. 🤠

@ajcvickers
Copy link
Member

@KieranDevvs I think what @tuespetre said is basically the same thing I would say. That is, it depends. For example, you might want to read some of the comments on the discussion about lazy-loading.

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. customer-reported labels Jan 22, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants