Base solution for your next web application
Open Closed

AggregateRoot class - Usage based on example #7524


User avatar
0
leonkosak created

Let's say that we have the following co-related entities.

DeviceType Device DeviceStatusHistory

Each DeviceType has 0 or more devices (defined with serial numbers). Each device has 0 or more history (status history).

public class DeviceType : AggregatedRoot<int> {
    ...
    public ICollection<Device> { get; set; }
}

public class Device : AggregatedRoot<long> {
    ...
    public ICollection<DeviceSatusHistory> { get; set; }
}

public class DeviceSatusHistory : Entity<long> {
    ...
}

The main point is that DeviceType is "absolute root", but particular device is also "strong independent entity".

Is semantically correct that Device is also AggregatedRoot or it should be Entity<long>?

Thank you for suggestions.


2 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @leonkosak

    This depends on the domain very much but Device seems like an AggregateRoot to me. How many devices a device type can have ?

  • User Avatar
    0
    leonkosak created

    DeviceTypes in our case is defined from us and users cannot add/change/delete these entities in DeviceTypes SQL table (let's say max 100 device types). The number of DeviceTypes is negligible compared to how many instances one DeviceType could have (easily 100k+).

    The next specific thing for DeviceTypes is also number of Devices (based on specific DeviceType). For instance, DeviceType1 has basically always much fewer devices because of the "nature" of this DeviceType. For instance, one tenant could not have a single device of DeviceType1, but have many of DeviceType3.

    public class DeviceType : AggregatedRoot<int> {
        ...
        public ICollection<object> SomeDataList { get; set; }  //tipically 3-7 for all devicy types, not more
        public ICollection<Device> Devices { get; set; }
    }
    
    public class Device : AggregatedRoot<long> {
        ...
        public ICollection<DeviceSatusHistory> DeviceSatusHistory { get; set; }
    }
    
    public class DeviceSatusHistory : Entity<long> {
        ...
    }
    

    (DT = DeviceType)

    Tenant      No. of devices of DT1   No. of devices of DT2   No. of devices of DT3
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    T1              36724                                 1254                                0
    T2              146734                               800                                  22
    T3              33535                                 135                                  55
    

    Each Device has its own history. Some devices (based on specific DT are sending data, for instance, every 15 minutes, other devices (based on other DT) are sending for instance once per day. So the number of records in ICollection<DeviceSatusHistory> heavily depends on the DeviceType of a specific device. Of course, history table would be shrunk (old records) periodically, but the number of items in ICollection<DeviceSatusHistory> is expected to be huge anyway (the device has to have some history for the purposes of analysis and predictions).