0

This is more of an open ended question regarding design best practice.

Say I have two classes; one is a Vehicle, and the other is an InformationSource (for maintenance/repair information). An information source will be used for many different vehicles, and a vehicle may have multiple different information sources (for different parts of the vehicle).

Vehicle and InformationSource are two different tables in the database, with a seperate link table to map them together (VehicleInfoSourceLink).

I currently keep a cache/collection of Vehicle objects for a specific customer (All vehicle records with customerID = X). And I also keep a cache/collection of InformationSource objects for a specific customer.

However when it comes time to find the available InformationSource(s) for a vehicle - I am not sure what the best way to do this is.

Option 1) Create a new object in my C# code that represents the link table e.g. VehicleInfoSourceLink and keep a cache of this available along with the Vehicle and InformationSource caches. This way when I have a specific vehicle, I can look at the link objects and find all the InformationSource Id's for a specific Vehicle, and then select those Ids from the InformationSource cache.

Option 2) Create an SQL function called "GetInformationSourcesForVehicle" which takes in the id of the vehicle and returns the Id's of all InformationSources that are mapped to it. I would call this function from my C# code, and keep a list of IDs, and then select the InformationSource objects with those IDs.

Really keen to hear peoples opinions of which is the better option, or maybe other solutions as well. Open to any feedback including how to ask questions like this better (as I want to use forums like this more in future to get good design advice).

1

You usually do not want to have a class representing the relationship between the InformationSource and Car classes. In relational databases you do have these tables because there is no other way how to represent the relationship. In object oriented languages you can do that directly.

I do not exactly know what you mean by available information source and when an information source becomes available for a vehicle based on your business rules, but even then, keeping information sources for a specific customer, unless this directly correlates to the availability of one, is probably not needed.

You may have classes like this:

class InformationSource
{
    private List<Car> _carsToBeSourceOf;

    public InformationSource(List<Car>? carsToBeSourceOf)
    {
        _carsToBeSourceOf = carsToBeSourceOf == null ?
            new List<Car>() : carsToBeSourceOf;
    }

    public function AddCarToHaveThisAsInformationSource(Car car)
    {
        _carsToBeSourceOf.Add(car);
    }
};

class Car
{
    protected List<InformationSource> _informationSources;

    public Car(List<InformationSource>? informationSources)
    {
        _informationSources = informationSources == null ?
            new List<InformationSource>() : informationSources;
    }

    public addInformationSource(InformationSource informationSource)
    {
        _informationSources.Add(informationSource);
        informationSource.AddCarToHaveThisAsInformationSource(this);
    }
};

A Customer will then have a list of cars as a property, you can add a getter to the Car class to access the information sources and through this getter you can access all the information sources the Customer has.

But then again, you may already see the problem, that in orded to load a Car class, there could be a situation that would require to load all the cars from the database, because of the relationships.

This would happen in a situation when one concrete car with unique ID (lets assume ID 1 for this example) would have more information sources and some of these information sources would be tied to cars with IDs other than 1.

You may be thinking about some lazy loading, which could work, but then you would probably be mixing business logic with persistence (the Car and InformationSource classes are likely to include the loading part), which is not good.

If the situation is the case, then yes, another class like CarsInformationSources is probably a good idea, then a Car would contain a list of the CarsInformationSources and so would the InformationSource class.

But then again, if it was the case where an information source tied to a car would be only tied to this one car, you would not really need the map in the first place, so having the CarsInformationSources class acting as a mediator is most likely the best idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.