by NicoJuicy · 9y ago · 0 comments
Eg. An API to a IoT cloud that lets user define their own type. The payload that i fetch is <T> so users can define their own type.
Another one is an api where i take a generic approach, so my only code that i write ( for every "class" / " model" ) is:
//every endpoint does a complete crud + jsonpatch + batch, also, all methods can be overridden to customize code. This is the code i need to write every time :p https://pastebin.com/fp8MtpBu
Note to myselve: extend the implementation to support expressions ( odata ) in the url ( similar to sql on top of an API through changing url parameters) and return the dto as response. :)
public Result WriteValue<T>(T value) where T : INumeric/Numeric { }
https://stackoverflow.com/questions/32664/is-there-a-constra...:(
So instead the interface has an identical method for ints, doubles, floats, decimals, strings...
http://referencesource.microsoft.com/#mscorlib/system/double...
public interface Num<A>
{
A Add(A x, A y);
A Subtract(A x, A y);
A Multiply(A x, A y);
A Divide(A x, A y);
A FromInteger(int value);
}
public struct NumInt : Num<int>
{
public int Add(int x, int y) => x + y;
public int Subtract(int x, int y) => x - y;
public int Multiply(int x, int y) => x * y;
public int Divide(int x, int y) => x / y;
public int FromInteger(int value) => value;
}
public struct NumDouble : Num<double>
{
public double Add(double x, double y) => x + y;
public double Subtract(double x, double y) => x - y;
public double Multiply(double x, double y) => x * y;
public double Divide(double x, double y) => x / y;
public double FromInteger(int value) => (double)value;
}
public struct NumBigInt : Num<BigInteger>
{
public BigInteger Add(BigInteger x, BigInteger y) => x + y;
public BigInteger Subtract(BigInteger x, BigInteger y) => x - y;
public BigInteger Multiply(BigInteger x, BigInteger y) => x * y;
public BigInteger Divide(BigInteger x, BigInteger y) => x / y;
public BigInteger FromInteger(int value) => (BigInteger)value;
}
public static class TestGenericNums
{
public static void Test()
{
var a = DoubleIt<NumInt, int>(10); // 20
var b = SquareIt<NumInt, int>(10); // 100
var c = NegateIt<NumInt, int>(10); // -10
var t = DoubleIt<NumDouble, double>(10); // 20
var u = SquareIt<NumDouble, double>(10); // 100
var v = NegateIt<NumDouble, double>(10); // -10
var x = DoubleIt<NumBigInt, BigInteger>(10); // 20
var y = SquareIt<NumBigInt, BigInteger>(10); // 100
var z = NegateIt<NumBigInt, BigInteger>(10); // -10
}
public static A DoubleIt<NumA, A>(A value) where NumA : struct, Num<A> =>
default(NumA).Add(value, value);
public static A SquareIt<NumA, A>(A value) where NumA : struct, Num<A> =>
default(NumA).Multiply(value, value);
public static A NegateIt<NumA, A>(A value) where NumA : struct, Num<A> =>
default(NumA).Subtract(default(NumA).FromInteger(0), value);
}It's usually a good idea to be provide both an generic and a non-generic interface to an object.
A good technique I've found is to have the base object be non-generic and then have a generic version that overrides the properties (with new) that you want to use generic types on. (using as against an underlying field for conversion)
But it's not as bad as it sounds and the only way (AFAIK) to be able to work with both polymorphic objects and have the convenience (and type checking) of a generic counterpart. Limiting yourself to just a generic object/interface unfortunately breaks a lot of design patterns. It's also safe afaik since the only thing the generic version does is some casting and it's generic parameter enforces that casting.
but if you have a better way I'm genuinely interested to hear it
> Limiting yourself to just a generic object/interface unfortunately breaks a lot of design patterns.
Which ones and why?
> It's also safe afaik since the only thing the generic version does is some casting and it's generic parameter enforces that casting.
If you don't have a known constraint that is enforced by the type system then it can never be 'safe'.
is the horrible bit. You should almost NEVER override with "new".
Please don't. I've ended up maintaining far too many abominations where developers got excited about generics and linq rather than solving real problems. You just end up with apps that should be simple and straightforward being a nightmare to maintain.
Generics, linq and inheritance all have their uses but if they aren't used sparingly they create more spaghetti.
Edit - at the very least, please be careful and use generics as sparingly as possible.
Its all about stepping back and going: "am I trying too hard?". Like that last 10% of making the code base 100% elegant is what ruins it.