vkhorikov / DddAndEFCore

Source code for the DDD and EF Core Pluralsight course
https://enterprisecraftsmanship.com/ps-ef-core
MIT License
248 stars 89 forks source link

[Question] Backing fields (Module 5) #2

Open davidhenley opened 4 years ago

davidhenley commented 4 years ago

Why did you use metadata instead of the following from EF Core docs?

modelBuilder.Entity<Student>()
        .Property(b => b.Enrollments)
        .HasField("_enrollments")
        .UsePropertyAccessMode(PropertyAccessMode.PreferFieldDuringConstruction);

And why didn't you use => _enrollments.ToReadOnlyList() or AsReadOnly() or the following from Microsoft docs?

    private readonly List<Enrollment> _enrollments;
    public IReadOnlyCollection<Enrollment> Enrollments => _enrollments;

I'm trying to follow along but there are so many ways of doing things it seems.

vkhorikov commented 4 years ago

1) Didn't know about this setting, thanks for pointing this out.

2) .ToString() makes a copy of the collection such that the original collection can't possibly be modified. It's not a big deal, though, and Enrollments => _enrollments is fine too. But I recommend using IReadOnlyList, not IReadOnlyCollection.

davidhenley commented 4 years ago

Thanks! Just trying to understand it all. Thank you for all your work in this course