Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible in C# to create a very simple value type for IBAN, BIC?

Tags:

c#

Is it possible in C# to create a very simple value type for IBAN, BIC (maybe with struct)? That automatically works for features like Json Serialization and EF Core saving?

I would like to use the primitive type in data objects that are converted to Json and also in entities that are stored via EF Core

public class AccountTransaction
{
    public Iban RecipientIban { get; set; }
    public Bic RecipientBic { get; set; }
}

For instance for EF Core I dont wanna specify some value converter everytime I used those types. Or I dont wanna specify some custom JsonSerializer everytime those types are used in an object. They more or less should work like usual string.

Also the goal would be to achieve some automatic validations on assignment. e.g:

Iban ibanVariable = "ILLEGAL IBAN";

That such calls are leading to errors automatically. (Also on reading from database / deserializing Jsons, etc.)

Is there some simple way I could achieve this? Is there some FromString() thingy or assignment operator at struct that I could overwrite (like operator-overloading)?

like image 943
grafbumsdi Avatar asked Mar 04 '23 06:03

grafbumsdi


1 Answers

You can achieve this by using conversion operators. These basically allow you to define a conversion from one Type to another and back. These conversions can either be explicit, so you'll need to explicitly cast them every time, or they can be implicit, so it just does it. Here's an example:

public class Iban
{
    private readonly string _ibanString;

    public Iban(string iban)
    {
        _ibanString = iban;
    }

    public static implicit operator Iban(string iban) => new Iban(iban);
   
    public static implicit operator string(Iban iban) => iban._ibanString;
}

You can now do this:

Iban iban = "SOME IBAN";

A quick breakdown of the implicit/ explicit operator syntax and a link to the MSDN documentation:

Basically, a operator must always be public static followed by either the implicit or explicit keyword, then follows the type which will be returned (much like for a method), and then finally you can specify exactly one argument to use to convert from. After that you can either do like I did and use the expression body syntax from a method or use an actual body with { ... return foo; } and all.

And here is the promised MSDN link: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators

Note 1: I just re read your title and you were asking for a value type, it's just as easy. All you need to do is replace public class Iban with public struct Iban.

Note 2: When you have implicitly defined operators on a class you can use explicit casting, but when you only have explicitly defined operators on a class you cannot implicitly cast them. So with implicit operators this is perfectly legal:

var iban = (Iban) "SOME IBAN";

But when you have explicit operators the following becomes illegal and you'll have to explicitly cast it:

// Throws a compiler error
Iban iban = "SOME IBAN"
like image 67
MindSwipe Avatar answered Apr 30 '23 00:04

MindSwipe