Open Closed

Correct service code for using Many to many entities created with the builder #5579


User avatar
0
richard.harrison@brightserve.com created

The requirement is that I have an entity called RateCard that is used to define pricing for a range of services. To do this the rate card has a collection of Rates (RateCardRate). Each of these has a Value for the base rate together with a set of entities (called ValueModifiers) that provide a different rate when a set of criteria are fulfilled.

I used the builder and the only way that I could find to have a entity that contains a collection of entities is with the many to many navigation property. This provides a design that is reasonably elegant; although for my purposes the linking table isn't strictly necessary because there will not be any shared data (i.e each ValueModifier is unique). However I am happy to go this way.

I can populate the data quite nicely; creating a RateCard and then creating ValueModifiers that I insert and add to the rate card.

What I am struggling with is what is the right way to navigate these properties.

What I want to do is to load a rate card which has a collection of RateCardRates and each RateCardRate has a collection of ValueModifiers that I can iterate.

What I have to do to make this work is the following (the whole test is included for clarity) - but these seems a little inelegant to have to iterate and then call the GetWithNavigationProperties; normally I'd like to be able to do all of this within a LINQ .Select statement

    public async Task ImportAsync()
    {
        var rateCardMatch = await _rateCardRepository.FindAsync(c => c.RateCardName == "Wessex Fleet");
        var rateCard = await _rateCardRepository.GetAsync(rateCardMatch.Id);
        rateCard.ShouldNotBeNull();
        var rates = await _rateCardRateRepository.GetListAsync(c => c.RateCardId == rateCard.Id);
        rates.ShouldNotBeEmpty();
        foreach (var rate in rates)
        {
            rate.ShouldNotBeNull();
            rate.ValueModifiers.ShouldNotBeNull();

            var rateDetails = await _rateCardRateRepository.GetWithNavigationPropertiesAsync(rate.Id);
            rateDetails.ShouldNotBeNull();
            rateDetails.ValueModifiers.ShouldNotBeNull();
            rateDetails.ValueModifiers.ShouldNotBeEmpty();
            foreach (var modifier in rateDetails.ValueModifiers)
            {
                var modifierDetails = await _valueModifierRepository.GetWithNavigationPropertiesAsync(modifier.Id);
                modifierDetails.ShouldNotBeNull();

                modifierDetails.Criteria.ShouldNotBeNull();
                modifierDetails.Criteria.ShouldNotBeEmpty();
            }
        }
    }

My questions are

  • am I doing the right thing
  • is there a better way of doing this
  • how can I do this using LINQ

1 Answer(s)
  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Hi,

    You can go through these documentation https://docs.abp.io/en/abp/latest/Entity-Framework-Core#loading-related-entities https://docs.abp.io/en/abp/latest/Entity-Framework-Core#repository-withdetails https://docs.abp.io/en/abp/latest/Entity-Framework-Core#repository-get-find-methods https://docs.abp.io/en/abp/latest/Entity-Framework-Core#explicit-lazy-loading https://docs.abp.io/en/abp/latest/Entity-Framework-Core#ensurepropertyloadedasync-ensurecollectionloadedasync https://docs.abp.io/en/abp/latest/Entity-Framework-Core#lazy-loading-with-proxies

Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19