Set Data Annotation MaxLength w/ Variable? - c#

I want to set the [MaxLength()] data annotation in my EF Core 7 entity with a variable. Here's my code.
public static class Constants
{
public static byte IdentifierMaxLength { get { return 46; } }
public static byte NameMaxLength { get { return 64; } }
public static ushort DescriptionMaxLength { get { return 1024; } }
public static ushort UsageMaxLength { get { return 1024; } }
}
public class TagEntity
{
private byte _maxIdentifierLength;
public Tag()
{
_maxIdentifierLength = Constants.IdentifierMaxLength;
}
[Required]
[MaxLength(_maxIdentifierLength)]
public string Identifier { get; set; }
}
// Or better yet:
[MaxLength(Constants.IdentifierMaxLength)]
I get this error instead:
An object reference is required for the non-static field, method, or property 'Tag._maxIdentifierLength'
Is this possible? If not, is there another way to go about this so I don't have to change these lengths in multiple places if they ever need to change?

Values provided to Attributes must be known at compile time. Constants are a good way of providing that guarantee.
Change your Constants class to have actual constants. In addition, you'll need/want to change the type to int to match the parameter for the attribute.
public static class Constants
{
public const int IdentifierMaxLength = 46;
}
Now you can do
[MaxLength(Constants.IdentifierMaxLength)]

Related

In C#, How can I create or overload an assignment operator to possibly assign two values at once?

This is probably a stupid question, but just in case....
We have a 3rd party package with weird models like:
public partial class CountingDevice
{
public int countingDeviceNo { get; set; }
public string countingDeviceName { get; set; }
public string obis { get; set; }
public int integralPart { get; set; }
public bool integralPartFieldSpecified;
public int fractionalPart { get; set; }
public bool fractionalPartFieldSpecified;
public double value { get; set; }
public bool valueFieldSpecified;
public bool offPeakFlag { get; set; }
public bool offPeakFlagFieldSpecified;
public ExpectedMeterReading expectedMeterReading { get; set; }
// snipped for brevity
}
You'll notice that sometimes there are pairs of fields like integralPart and integralPartFieldSpecified.
Here is the problem: If I simply assign some value to integralPart but do not set integralPartFieldSpecified = true, the value of integralPart will be completely ignored causing the solution to fail.
So when mapping our own models to this madness, I need to litter the code with constructs like:
if (IntegralPart != null)
{
countingDevice.integralPartSpecified = true;
countingDevice.integralPart = (int)IntegralPart!;
}
Both in the interest of reducing lines of code and not stumbling over a minefield, I would like to do any one of the following:
A. Overload the = operator so it will automatically check for a property which is a boolean and has "Specified" concatenated to the current property's name. If such a property exists, it will be assigned true when the value is assigned; if not, then assignment will operate as normal. Ideally, it should be "smart" enough to assign "...Specified" to false if the value assigned is null/default/empty.
B. Create some customer operator which will do the same as A.
C. Create some method which I could invoke in a concise and preferably typesafe way to do the same.
Is this possible?
If so, how?
To make it clear: I need to build quite a few wrappers.
I don't want to repeat this logic for every field and worry about missing some fields which it applies to.
I want a generic way of assigning both fields at once if the "Specified" field exists and being able to do assignments in exactly the same way if it does not exist.
not stumbling over a minefield
Encapsulate the minefield.
If you don't control this 3rd party DTO then don't use it throughout your domain. Encapsulate or wrap the integration of this 3rd party tool within a black box that you control. Then throughout your domain use your models.
Within the integration component for this 3rd party system, simply map to/from your Domain Models and this 3rd party DTO. So this one extra line of code which sets a second field on the DTO only exists in that one place.
Another (expensive) solution would be to write a method that takes in an object, a property name, and the new property value. You can then use reflection to both set the property value for the specified property, as well as search for the bool field that you want to set (if it exists).
Note that you need to pass the correct type for the property. There's no compile-time checking that you're passing a double instead of a string for the value property, for example.
Below I've created an extension method on the object type to simplify calling the method in our main code (the method becomes a member of the object itself):
public static class Extensions
{
// Requires: using System.Reflection;
public static bool SetPropertyAndSpecified(this object obj,
string propertyName, object propertyValue)
{
// Argument validation left to user
// Check if 'obj' has specified 'propertyName'
// and set 'propertyValue' if it does
PropertyInfo prop = obj.GetType().GetProperty(propertyName,
BindingFlags.Public | BindingFlags.Instance);
if (prop != null && prop.CanWrite)
{
prop.SetValue(obj, propertyValue, null);
// Check for related "FieldSpecified" field
// and set it to 'true' if it exists
obj.GetType().GetField($"{propertyName}FieldSpecified",
BindingFlags.Public | BindingFlags.Instance)?.SetValue(obj, true);
return true;
}
return false;
}
}
After you add this class to your project, you can do something like:
static void Main(string[] args)
{
var counter = new CountingDevice();
// Note that 'valueFieldSpecified' and `integralPartFieldSpecified'
// are set to 'false' on 'counter'
// Call our method to set some properties
counter.SetPropertyAndSpecified(nameof(counter.integralPart), 42);
counter.SetPropertyAndSpecified(nameof(counter.value), 69d);
// Now 'valueFieldSpecified' and 'integralPartFieldSpecified'
// are set to 'true' on 'counter'
}
You cannot overload the = operator in C#.
You can just use custom properties and set the "FieldSpecified" fields in the setters e.g.
private int _integralPart;
public int integralPart
{
get { return _integralPart; }
set
{
_integralPart = value;
integralPartFieldSpecified = true;
}
}
public bool integralPartFieldSpecified;
Update
If you want a generic solution you can use a generic class for properties that you want to achieve the specified behaviour with e.g.
public class ValueWithSpecifiedCheck<T>
{
private T _fieldValue;
public T FieldValue
{
get
{
return _fieldValue;
}
set
{
_fieldValue = value;
FieldSpecified = true;
}
}
public bool FieldSpecified { get; set; }
}
public class Data
{
public ValueWithSpecifiedCheck<int> IntegralPart { get; set; }
}
Then the class/property would be used as following:
public static void Main()
{
var data = new Data();
data.IntegralPart = new ValueWithSpecifiedCheck<int>();
data.IntegralPart.FieldValue = 7;
Console.WriteLine(data.IntegralPart.FieldSpecified);// Prints true
}
If you implement a generic solution and add implicit conversion operators, it's quite convenient to use.
Here's a sample Optional<T> struct (I made it a readonly struct to ensure immutable mechanics):
public readonly struct Optional<T> where T : struct
{
public Optional(T value)
{
_value = value;
}
public static implicit operator T(Optional<T> opt) => opt.Value;
public static implicit operator Optional<T>(T opt) => new(opt);
public T Value => _value!.Value;
public bool Specified => _value is not null;
public override string ToString() => _value is null ? "<NONE>" : _value.ToString()!;
readonly T? _value;
}
You could use that to implement your CountingDevice class like so:
public partial class CountingDevice
{
public int countingDeviceNo { get; set; }
public string countingDeviceName { get; set; }
public string obis { get; set; }
public Optional<int> integralPart { get; set; }
public Optional<int> fractionalPart { get; set; }
public Optional<double> value { get; set; }
public Optional<bool> offPeakFlag { get; set; }
// snipped for brevity
}
Usage is quite natural because of the implicit conversions:
public static void Main()
{
var dev = new CountingDevice
{
integralPart = 10, // Can initialise with the underlying type.
value = 123.456
};
Console.WriteLine(dev.fractionalPart.Specified); // False
Console.WriteLine(dev.integralPart.Specified); // True
Console.WriteLine(dev.value); // 123.456
Console.WriteLine(dev.value.ToString()); // 123.456
Console.WriteLine(dev.fractionalPart.ToString()); // "<NONE>"
dev.fractionalPart = 42; // Can set the value using int.
Console.WriteLine(dev.fractionalPart.Specified); // True
Console.WriteLine(dev.fractionalPart); // 42
var optCopy = dev.offPeakFlag;
Console.WriteLine(optCopy.Specified); // False
dev.offPeakFlag = true;
Console.WriteLine(dev.offPeakFlag.Specified); // True
Console.WriteLine(optCopy.Specified); // Still False - not affected by the original.
Console.WriteLine(optCopy); // Throws an exception because its not specified.
}
You might also want to use optional reference types, but to do that you will need to declare a generic with the class constraint:
public readonly struct OptionalRef<T> where T : class
{
public OptionalRef(T value)
{
_value = value;
}
public static implicit operator T(OptionalRef<T> opt) => opt.Value;
public static implicit operator OptionalRef<T>(T opt) => new(opt);
public T Value => _value ?? throw new InvalidOperationException("Accessing an unspecified value.");
public bool Specified => _value is not null;
public override string ToString() => _value is null ? "<NONE>" : _value.ToString()!;
readonly T? _value;
}
Personally, I think that's a bit overkill. I'd just use nullable value types, int?, double? etc, but it depends on the expected usage.
C# doesn't allow overloading the = operator (unlike eg C++). However, your suggestion C should work. It's a bit of a hassle, too, since you'll have to write a bunch of methods, but you could write an extension method such as
public static class Extensions
{
public static void UpdateIntegralPart(this CountingDevice dev, double value)
{
dev.integralPart = value;
dev.integralPartSpecified = true;
}
}
Then you can call
countingDevice.UpdateIntegralPart(1234);

Why keep two variables for one content in C# UWP?

In C#, I notice that many coders do something like the following:
class X
{
private int test_;
public int test
{
get { return test_; }
set { if (test_ != value) test_ = value; }
}
}
My question is why keep a private, and a public variable for the same content?
Why did we not do this instead?
class X
{
public int test
{
get; set;
}
}
I mean, we are changing the private variable within anyway. What is the point of not using a single public variable instead?
The code:
class X
{
public int test
{
get; set;
}
}
...is a direct equivalent for this:
class X
{
private int test_;
public int test
{
get { return test_; }
set { test_ = value; }
}
}
The first example is an auto-implemented property. The C# compiler automatically produces the second example when you compile.
Now, the code you presented first though had this line:
set { if (test_ != value) test_ = value; }
You'll notice that it's doing something different to the auto-property equivalent code. And that's where the difference lies in these two approaches.
When you use a backing field for your properties you can introduce specific rules that you need your code to follow.
For example, you might want to set the audio volume on a music app, so your code might be like this:
public class Music
{
private int _volume = 7;
public int Volume
{
get { return _volume; }
set
{
if (value >= 0 && value <= 10)
{
_volume = value;
}
}
}
}
It is common to have a private field variable when your property contains logic like this.
These are not two variables. One is a field and other one is a property. A property is optional getter and setter methods in disguise.
Your proposal of the solution is in fact part of the language, called auto-implemented properties:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

Why isn't an implicit conversion from int to intBox working in Json.decode or default modelbinding?

EDIT: removed first part of the question, as it was misleading and not completely pertinent.
For various reasons, I've come to the point where I could really use the ability to box some of my Post body parameters in their model.
I started this just accepting the default model binder would just work™, which it does, but not for ints(?!). Example:
public class IntBox
{
public int Value;
public IntBox(int value)
{
Value = value;
}
public static implicit operator IntBox(int value)
{
return new IntBox(value);
}
}
public class StringBox
{
public string Value;
public StringBox(string value)
{
Value = value;
}
public static implicit operator StringBox(string value)
{
return new StringBox(value);
}
}
public class BoolBox
{
public bool Value;
public BoolBox(bool value)
{
Value = value;
}
public static implicit operator BoolBox(bool value)
{
return new BoolBox(value);
}
}
public class NeedQuery : StateFullQuery
{
public StringBox[] TestStrings { get; set; } //"TestStrings":["a","b"]
public IntBox[] TestInts { get; set; } //"TestInts":[1,2,3,4]
public BoolBox[] TestBools { get; set; } //"TestBools":[true,false]
}
//Inside Controller:
public string Post([FromBody]NeedQuery query)
{
//At this point query.TestStrings contains two StringBoxes with the expected values. As does query.TestBools
// However, query.TestInts is empty.
}
What am I missing?! What's so special about Ints that they can't be implicitly cast by the Modelbinder? How do I get around this?
Would really appreciate help with this one; without a large rewrite I'm blocked until I can figure it out.
NOTE: When I say I need to box the int values, that's a simplification of the problem for the sake of the post, it's a bit more complicated than simply boxing the values.
Figured it out.
This doesn't work:
public class IntBox
{
public int Value;
public IntBox(int value)
{
Value = value;
}
public static implicit operator IntBox(int value)
{
return new IntBox(value);
}
}
This does work:
public class IntBox
{
public int Value;
public IntBox(int value)
{
Value = value;
}
public static implicit operator IntBox(Int64 value)
{
return new IntBox(value);
}
}
So obviously there's some silliness with 64/32 bit conversion there. I will probably have to implement my implicit conversion for both 32 and 64 bit ints, but at least I can stop pulling my hair out. And maybe this will help someone else in the future.
The reason this happens is (in short) that implicit casts are inferred at compile time, while the model binder uses reflection at runtime to set the value, at which point there are two different - incompatible - types.
The simplest solution to deal with situations like this is usually to have (for example) an int[] property on your model, bind to that and have it simply map to an IntBox[] property manually (e.g. via custom accessors).
I won't post a "full" solution since - as you've said - your example is simplified; however, hopefully this is enough information for you to understand and solve the problem.

Looking for a short & simple example of getters/setters in C#

I am having trouble understanding the concept of getters and setters in the C# language. In languages like Objective-C, they seem an integral part of the system, but not so much in C# (as far as I can tell). I have read books and articles already, so my question is, to those of you who understand getters & setters in C#, what example would you personally use if you were teaching the concept to a complete beginner (this would include as few lines of code as possible)?
I think a bit of code will help illustrate what setters and getters are:
public class Foo
{
private string bar;
public string GetBar()
{
return bar;
}
public void SetBar(string value)
{
bar = value;
}
}
In this example we have a private member of the class that is called bar. The GetBar() and SetBar(string value) methods do exactly what they are named - one retrieves the bar member, and the other sets its value.
In C# 1.1 and later, you have properties. The basic functionality is also the same:
public class Foo
{
private string bar;
public string Bar
{
get { return bar; }
set { bar = value; }
}
}
The private member bar is not accessible outside the class, but the public Bar is, and it has two accessors: get, which returns the private member just as the GetBar() example above, and also a set, which corresponds to the SetBar(string value) method in the aforementioned example.
Starting with C# 3.0 and above, the compiler was optimized to the point that such properties do not need to be explicitly given a private member as their source. The compiler automatically generates a private member of that type and uses it as a source of a property.
public class Foo
{
public string Bar { get; set; }
}
What the code shows is an automatic property that has a private member generated by the compiler. You don't see the private member, but it is there. This also introduced a couple of other issues - mainly with access control. In C# 1.1 and 2.0, you could omit the get or set portion of a property entirely:
public class Foo
{
private string bar;
public string Bar
{
get { return bar; }
}
}
Giving you the chance to restrict how other objects interact with the Bar property of the Foo class. But from C# 3.0 to before 6.0, if you chose to use automatic properties, you would have to specify the access to the property as follows to emulate that behavior:
public class Foo
{
public string Bar { get; private set; }
}
The set accessor would still exist, but only the class itself could use it to set Bar to some value, and anyone could still get the value.
Thankfully, starting in C# 6.0, properties can be made read- or write-only again by simply omitting the property's get or set respectively (not to be confused with the readonly keyword):
public class Foo
{
// Read-only property
public string Bar { get; }
// Write-only property (less common)
public string Baz { set; }
}
In C#, Properties represent your Getters and Setters.
Here's an example:
public class PropertyExample
{
private int myIntField = 0;
public int MyInt
{
// This is your getter.
// it uses the accessibility of the property (public)
get
{
return myIntField;
}
// this is your setter
// Note: you can specify different accessibility
// for your getter and setter.
protected set
{
// You can put logic into your getters and setters
// since they actually map to functions behind the scenes
if (DoSomeValidation(value))
{
// The input of the setter is always called "value"
// and is of the same type as your property definition
myIntField = value;
}
}
}
}
You would access this property just like a field. For example:
PropertyExample example = new PropertyExample();
example.MyInt = 4; // sets myIntField to 4
Console.WriteLine( example.MyInt ); // prints 4
A few other things to note:
You don't have to specifiy both a getter and a setter, you can omit either one.
Properties are just "syntactic sugar" for your traditional getter and setter. The compiler will actually build get_ and set_ functions behind the scenes (in the compiled IL) and map all references to your property to those functions.
My explanation would be following. (It's not so short, but it's quite simple.)
Imagine a class with a variable:
class Something
{
int weight;
// and other methods, of course, not shown here
}
Well, there is a small problem with this class: no one can see the weight. We could make weight public, but then everyone would be able to change the weight at any moment (which is perhaps not what we want). So, well, we can do a function:
class Something
{
int weight;
public int GetWeight() { return weight; }
// and other methods
}
This is already better, but now everyone instead of plain something.Weight has to type something.GetWeight(), which is, well, ugly.
With properties, we can do the same, but the code stays clean:
class Something
{
public int weight { get; private set; }
// and other methods
}
int w = something.weight // works!
something.weight = x; // doesn't even compile
Nice, so with the properties we have finer control over the variable access.
Another problem: okay, we want the outer code to be able to set weight, but we'd like to control its value, and not allow the weights lower than 100. Moreover, there are is some other inner variable density, which depends on weight, so we'd want to recalculate the density as soon as the weight changes.
This is traditionally achieved in the following way:
class Something
{
int weight;
public int SetWeight(int w)
{
if (w < 100)
throw new ArgumentException("weight too small");
weight = w;
RecalculateDensity();
}
// and other methods
}
something.SetWeight(anotherSomething.GetWeight() + 1);
But again, we don't want expose to our clients that setting the weight is a complicated operation, it's semantically nothing but assigning a new weight. So the code with a setter looks the same way, but nicer:
class Something
{
private int _w;
public int Weight
{
get { return _w; }
set
{
if (value < 100)
throw new ArgumentException("weight too small");
_w = value;
RecalculateDensity();
}
}
// and other methods
}
something.Weight = otherSomething.Weight + 1; // much cleaner, right?
So, no doubt, properties are "just" a syntactic sugar. But it makes the client's code be better. Interestingly, the need for property-like things arises very often, you can check how often you find the functions like GetXXX() and SetXXX() in the other languages.
Most languages do it this way, and you can do it in C# too.
public void setRAM(int RAM)
{
this.RAM = RAM;
}
public int getRAM()
{
return this.RAM;
}
But C# also gives a more elegant solution to this:
public class Computer
{
int ram;
public int RAM
{
get
{
return ram;
}
set
{
ram = value; // value is a reserved word and it is a variable that holds the input that is given to ram ( like in the example below )
}
}
}
And later access it with:
Computer comp = new Computer();
comp.RAM = 1024;
int var = comp.RAM;
For newer versions of C# it's even better:
public class Computer
{
public int RAM { get; set; }
}
and later:
Computer comp = new Computer();
comp.RAM = 1024;
int var = comp.RAM;
C# introduces properties which do most of the heavy lifting for you...
ie
public string Name { get; set; }
is a C# shortcut to writing...
private string _name;
public string getName { return _name; }
public void setName(string value) { _name = value; }
Basically getters and setters are just means of helping encapsulation. When you make a class you have several class variables that perhaps you want to expose to other classes to allow them to get a glimpse of some of the data you store. While just making the variables public to begin with may seem like an acceptable alternative, in the long run you will regret letting other classes manipulate your classes member variables directly. If you force them to do it through a setter, you can add logic to ensure no strange values ever occur, and you can always change that logic in the future without effecting things already manipulating this class.
ie
private string _name;
public string getName { return _name; }
public void setName(string value)
{
//Don't want things setting my Name to null
if (value == null)
{
throw new InvalidInputException();
}
_name = value;
}
well here is common usage of getter setter in actual use case,
public class OrderItem
{
public int Id {get;set;}
public int quantity {get;set;}
public int Price {get;set;}
public int TotalAmount {get {return this.quantity *this.Price;}set;}
}
This would be a get/set in C# using the smallest amount of code possible. You get auto-implemented properties in C# 3.0+.
public class Contact
{
public string Name { get; set; }
}
As far as I understand getters and setters are to improve encapsulation.
There is nothing complex about them in C#.
You define a property of on object like this:
int m_colorValue = 0;
public int Color
{
set { m_colorValue = value; }
get { return m_colorValue; }
}
This is the most simple use. It basically sets an internal variable or retrieves its value.
You use a Property like this:
someObject.Color = 222; // sets a color 222
int color = someObject.Color // gets the color of the object
You could eventually do some processing on the value in the setters or getters like this:
public int Color
{
set { m_colorValue = value + 5; }
get { return m_colorValue - 30; }
}
if you skip set or get, your property will be read or write only. That's how I understand the stuff.
Simple example
public class Simple
{
public int Propery { get; set; }
}
Getters and Setters in C# are something that simplifies the code.
private string name = "spots";
public string Name
{
get { return name; }
set { name = value; }
}
And calling it (assume we have a person obj with a name property):
Console.WriteLine(Person.Name); //prints "spots"
Person.Name = "stops";
Console.Writeline(Person.Name); //prints "stops"
This simplifies your code. Where in Java you might have to have two methods, one to Get() and one to Set() the property, in C# it is all done in one spot. I usually do this at the start of my classes:
public string foobar {get; set;}
This creates a getter and setter for my foobar property. Calling it is the same way as shown before. Somethings to note are that you don't have to include both get and set. If you don't want the property being modified, don't include set!
Internally, getters and setters are just methods. When C# compiles, it generates methods for your getters and setters like this, for example:
public int get_MyProperty() { ... }
public void set_MyProperty(int value) { ... }
C# allows you to declare these methods using a short-hand syntax. The line below will be compiled into the methods above when you build your application.
public int MyProperty { get; set; }
or
private int myProperty;
public int MyProperty
{
get { return myProperty; }
set { myProperty = value; } // value is an implicit parameter containing the value being assigned to the property.
}
This is a basic example of an object "Article" with getters and setters:
public class Article
{
public String title;
public String link;
public String description;
public string getTitle()
{
return title;
}
public void setTitle(string value)
{
title = value;
}
public string getLink()
{
return link;
}
public void setLink(string value)
{
link = value;
}
public string getDescription()
{
return description;
}
public void setDescription(string value)
{
description = value;
}
}
In case someone is looking for a short version of getter only (I was):
public class Foo
{
private string bar;
public string Bar => bar;
}

How should I encapsulate this multi-dimensional enum?

In my application I've got some information that can be one of a small set of values - so I'd like to use an enum to hold it, ensuring valid values through type-safety at compile time:
public enum Something { A1, A2, A3, B1, B2, C1 };
These enums represent multi-dimensional data (they have a letter and a number in the example above), so I'd like to be able to get the value associated with them, e.g.
Something example = Something.A1;
// Now I want to be able to query the values for example:
example.Letter; // I want to get "A"
example.Number; // "1"I want to get 1
I've two possible solutions, neither of them feel very 'clean', so I was interested in which people prefer, and why, or whether anyone has any better ideas.
Option 1:
Create a struct which wraps the enum, and provides properties on the wrapped data, e.g.
public struct SomethingWrapper
{
public Something Value { get; private set; }
public SomethingWrapper(Something val)
{
Value = val;
}
public string Letter
{
get
{
// switch on Value...
}
}
public int Number
{
get
{
// switch on Value...
}
}
}
Option 2:
Leave the enum as it is and create a static Helper class which provides static functions that get the values:
public static class SomethingHelper
{
public static string Letter(Something val)
{
// switch on val parameter
}
public static int Number(Something val)
{
// switch on val parameter
}
}
Which should I choose, and why? Or is there a better solution I've not thought of?
Third option: like the second option, but with extension methods:
public static class SomethingHelper
{
public static string Letter(this Something val)
{
// switch on val parameter
}
public static int Number(this Something val)
{
// switch on val parameter
}
}
Then you can do:
Something x = ...;
string letter = x.Letter();
It's unfortunate that there aren't extension properties, but such is life.
Alternatively, create your own pseudo enum: something like this:
public sealed class Something
{
public static Something A1 = new Something("A", 1);
public static Something A2 = ...;
private Something(string letter, int number)
{
Letter = letter;
Number = number;
}
public string Letter { get; private set; }
public int Number { get; private set; }
}
Why not just use two enums, and maybe define a struct that holds one of each?

Categories