Reputation: 559
Should you ever separate a class into different parts, assuming there will be common values inside of it or are there some reasons against it?
For example:
public class Car
{
public string ModelName { get; }
public string Class { get; }
public string Engine { get; }
public Color CarColor { get; }
public uint Mileage { get; }
public uint IssueYear { get; }
}
could be separated into:
public class CarModel
{
public string Name { get; }
public string Class { get; }
public string Engine { get; }
}
public class CarInstance
{
private CarModel _car_model;
public string ModelName { get => _car_model.Name; }
public string Class { get => _car_model.Class; }
public string Engine { get => _car_model.Engine; }
public Color CarColor { get; }
public uint Mileage { get; }
public uint IssueYear { get; }
}
Upvotes: 1
Views: 182
Reputation: 6430
I think I understand now what you are asking for. Well, first of all this is very broad questions and highly opinionated. But I will try to answer as briefly as possible. Since, I follow DDD (Domain Driven Design), the approach largely depends on the Bounding Context
.
You can read about bounding context here https://martinfowler.com/bliki/BoundedContext.html
if your bounding context deals with only Cars
and only what Make, Model or Year they are made of you can go with a structure like this -
public class CarInstance
{
public string Name { get; }
public string Class { get; }
public string Engine { get; }
}
Since you will only ever use these values in conjunction of a car; not independently. In this bounding context, the details of those items are insignificant. An example of such system is - if you are building an asset management system for a family and only ever concerned about the value of the car.
But, consider a different bounding context. Say, an insurance company, who issue insurance based on car model, make and year. For this bounding context it if very very important for you to know the model, make and year. Because based on those your insurance premium changes. Now, you need to dig deeper into the context and an example model might look like this -
public class CarMake
{
public string Name { get; }
public string PriceMultiplier{ get; }
public string PenaltyOffset{ get; }
}
public class CarModel
{
public string Name { get; }
public string PriceMultiplier{ get; }
public string PenaltyOffset{ get; }
public CarMake Make { get; } //since model is dependent on Make. For example BMW models are not same as Toyota models.
}
public class CarYear
{
public string Name { get; }
public string PriceMultiplier{ get; }
}
public class CarInstance
{
public string RegistrationNumber { get; }
public CarModel Model { get; }
public CarYear Year{ get; }
}
As you see if vastly depends on what you are building.
Suggestion -
Try to understand your bounding context well. You will find out yourself what you need.
Upvotes: 2
Reputation: 2417
I get what you're looking for now. This is similar to how a Product
and ProductType
relationship work on an ecommerce platform. In my experience, although you could technically achieve the same thing with inheritance like isidat's answer shows, it's not an accurate representation of how the data would exist in say, a relational database.
However, the example you gave is overcomplicated. If you want your CarModel information to be readonly, then simply set the property for it in CarInstance
as readonly
like so:
public class CarModel
{
public string Name { get; }
public string Class { get; }
public string Engine { get; }
}
public class CarInstance
{
public readonly CarModel CarModel;
public uint Mileage { get; }
public uint IssueYear { get; }
}
The CarModel would have to be provided during construction, but that's not a problem because you shouldn't be constructing a CarInstance
without knowing which CarModel
it's going to be.
I would like to point out however that in reality a car tracking system would have it even more granular than you have here. Engine
would be a class, Class
would be a class, even PaintJob
would be a class. Basically any element that could be reused between instances of a car would be their own classes, and their own relational tables in a database.
Upvotes: 0
Reputation: 946
In your example separation will be a good choice. You can do it by using inheritance.
public class CarModel
{
public string Name { get; }
public string Class { get; }
public string Engine { get; }
}
public class CarInstance : CarModel
{
public uint Mileage { get; }
public uint IssueYear { get; }
}
Upvotes: 3