C
C#•6mo ago
tama

Entity DB object with immutable variables

Hello there. I am kind of new to C# and I am doing a project about some database management system. I have created an object for the database and my first migration went well. Now, part of the challenge with this project is that some class variables for the database object needs to be readonly after being set in the initializer. I am a little unsure if this is something you can achieve with some keywords or if I need to create my own logic for ensuring the fields are never changed. As an example: I have a class Car with three variables: Manufacturer, Model and Owner. Now, Manufacturer and Owner will always stay the same for the object in the database but Owner can of course change. To achieve this, I have so far written:
C#
public class Car
{
private string _manufacturer;
private string _modelName;

public string Manufacturer => _manufacturer;
public string ModelName => _modelName;

[Required]
public string Owner { get; set; }

public Car(string manufacturer, string modelName, string owner)
{
_manufacturer = manufacturer;
_modelName = modelName
Owner = owner;
}
}
C#
public class Car
{
private string _manufacturer;
private string _modelName;

public string Manufacturer => _manufacturer;
public string ModelName => _modelName;

[Required]
public string Owner { get; set; }

public Car(string manufacturer, string modelName, string owner)
{
_manufacturer = manufacturer;
_modelName = modelName
Owner = owner;
}
}
Is this sufficient?
8 Replies
Angius
Angius•6mo ago
You can just use init properties public string Manufacturer { get; init; }
tama
tamaOP•6mo ago
Let me just try, maybe you are right. 🙂 Alright, it seems to work, thanks a lot. 🙂 Just one last thing: Is it possible to showcase that it is not possible to write to the variable after calling the initializer? I'm thinking an xunit test?
Angius
Angius•6mo ago
Yeah, a unit test sounds good That said... I'd quwstion unit testing the framework Like, you don't unit test whether Math.Max() actually returns the maximum value, of if Console.WriteLine() actually works Or if a private method really is private
tama
tamaOP•6mo ago
Yeah, I am aware it is not the best practice, however, part of the criteria for the project is that the variable cannot be changed after initialization, so it could be nice to be able to point to a unittest and say: "Look, it's not being changed!"
Angius
Angius•6mo ago
If that's the case, then sure, write a test Assuming you can Since trying to assign to an init property is a compile-time error, not a runtime exception IIRC
tama
tamaOP•6mo ago
I just wonder how... I tried doing something like this but it would not build because it's an obvious error:
C#
Assert.Throws<InvalidOperationException>(() => car.ModelName = "test");
C#
Assert.Throws<InvalidOperationException>(() => car.ModelName = "test");
Angius
Angius•6mo ago
Yep Best idea I have, is to write a separate little project, and show the build error
tama
tamaOP•6mo ago
Yeah, it's probably not worth the time at that point, since there are still a bunch of features I need to add. I appreciate the help though. 🙂
Want results from more Discord servers?
Add your server