C
C#•5w 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•5w ago
You can just use init properties public string Manufacturer { get; init; }
tama
tama•5w 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•5w 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
tama•5w 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•5w 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
tama•5w 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•5w ago
Yep Best idea I have, is to write a separate little project, and show the build error
tama
tama•5w 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
More Posts