Skip to content

IncludeThen feature

Jon P Smith edited this page Jan 27, 2020 · 3 revisions

IncludeThen feature

There is a four page article about this feature available via this link.

Version 3.1.0 of EfCore.GenericServices added an attribute to make writing DDD-styled updates of relationships easier. This attribute allows you to ask the UpdateAndSave/Async method to add Include and ThenInclude methods to the load on the entity.
Here is an example:

[IncludeThen(nameof(Book.Reviews))]
public class AddReviewWithIncludeDto : ILinkToEntity<Book>
{
    public int BookId { get; set; }
    public string VoterName { get; set; }
    public int NumStars { get; set; }
    public string Comment { get; set; }
}

This has the effect of loading the Book class with its Reviews collection.
This makes the writing of the DDD access methods that work on relationships much easier, in that you can have all of the relationships loaded by EfCore.GenericServices. The code below is a access method in the 'Book' class.

public void AddReviewWithInclude(int numStars, string comment, string voterName)
{
    if (_reviews == null)
        // This is a test to make sure I added the IncludeThen attribute on the DTO
        throw new InvalidOperationException("The Reviews collection must be loaded before calling this method");
    _reviews.Add(new Review(numStars, comment, voterName));
}

This is shorter/simpler than the original version because the Reviews collection has been loaded before the method is called.

More information on the what the IncludeThen attribute parameters are, and how they are combined can be found in this section of the "GenericServices and DTOs" page.