Navigation Property and repository usage

252 Views Asked by At

I am a little confused on the following. Should i be using navigation properties to get where i want to get to, or should i asked the repository? for example: I have the following class

public class Vehicle
{
   public IList<Equipment> Equipment { get; set; }
}

public class Equipment
{
 //.. Properties..
}

AS far as DDD is concerned, what would be the recommended way of going about getting equipment associated with a partiacular vehicle?

Option1: Ask repository for vehicle and grab equipment from navigation property

public class VehicleService {

    public IEnumerable<Equipment> GetVehicleEquipment(int vehicleId)
    {
        var vehicle = _repository.Get(vehicleId);
        return vehicle.Equipment;
    }
}

Option2: Make a specialized repository method to be able to ask for equipment for a specific vehicle public class VehicleService {

         public IEnumerable<Equipment> GetVehicleEquipment(int vehicleId)
        {
            return _repository.GetEquipment(vehicleId);
        }
}
1

There are 1 best solutions below

0
On

Choose option1 if Vehicle is an aggregate root and Equiment is a local entity or a value object since only aggregates have their repositories.

Option 1 and Option2 are both fine if Equiment is also an aggregate root and you don't mind your aggregate root referencing other aggregate roots. But Option2 seems to be redundant in this case.

Choose Option2 if you follow "do not reference other aggregate roots" since you reference equipement's id instead and the navigation is not doable.

public class Vehicle
{
   public IList<Guid> EquipmentIds { get; set; }
}