FabianTerhorst / coreclr-module

Old alt:V CoreClr (.NET Core Common Language Runtime) community made module. New: https://github.com/altmp/coreclr-module
MIT License
72 stars 67 forks source link

[Suggestion] Use instances instead of static classes & methods #260

Open emre1702 opened 3 years ago

emre1702 commented 3 years ago

All api classes like AltV.Net.Alt, AltChat etc. are static. This totally takes away the possibility to write extensions for those without getting much benefit.

Extensions for theses classes make the code much cleaner by allowing consistent code. E.g. Alt.CreateGang would be consistent and clean.

This is specially important for libraries and other resources. E.g. for my custom commands resource I'd like to extend AltChat, for a possible gang library I'd like to extend Alt and AltAsync. That would make the libraries and resources much easier to use.

Edit: For my library I'll extend IServer so the user can use Alt.Core. Not a nice solution but currenty the best.

emre1702 commented 3 years ago

This would also open up the possibility to mock the alt:V API. Here I've tried that and it works pretty nice: https://dotnetfiddle.net/zNUAgW Didn't use setter to lower the chance to replace the api by accident.

So the advantages would be:

  1. Possibility to add extensions (like e.g. LINQ, EF Core and soo many more do)
  2. Possibility to mock the whole API.

Only disadvantage I can think about currently:

  1. Time needed to call will be doubled. But that makes no difference as 1 Million calls in a propably not optimized code are done in 10-11ms: https://dotnetfiddle.net/vldR32 But this can also be lowered by converting the property to a field by removing the "{ get; }", then I have 7 ms instead of 10-11 ms, which is ~30% slower than the static calls.