A lightweight collection of useful functions to reduce boilerplate code.
$ dotnet add package CommonNetFuncs.CoreThis lightweight project contains helper methods for several common functions required by applications.
Helper methods for dealing with asynchronous processes.
Asynchronously update properties of a class using ObjectUpdate
//Fill the Name and Address property using async tasks
Person person = new();
//AsyncIntString helper class is used for int and string types since they can't otherwise be gotten asynchronously like this
AsyncIntString personPhotoLocation = new();
person.Id = 1;
List<Task> tasks =
[
person.ObjectUpdate(nameof(Person.Name), GetPersonNameByIdAsync(person.Id)), //Fills person.Name with results of GetPersonNameByIdAsync(person.Id)
person.ObjectUpdate(nameof(Person.Address), GetPersonAddressByIdAsync(person.Id)), //Fills person.Address with results of GetPersonAddressByIdAsync(person.Id)
personPhotoLocation.ObjectUpdate(nameof(AsyncIntString.AsyncString), GetPersonPhotoLocationById(person.Id)) //Fills personPhotoLocation.AsyncString with the results of GetPersonPhotoLocationById(person.Id)
]
await Task.WhenAll(tasks);
Object fill can be used to asynchronously fill classes and lists with.
Person person = new();
ConcurrentBag<Person> people = [];
List<Task> tasks =
[
person.ObjectUpdate(GetPersonById(1)), //person is filled by results of GetPersonById(1) which returns type Person
//people is filled by the results of all three calls to GetPeopleByState additively (all results will be present in people)
people.ObjectUpdate(GetPeopleByState("Ohio")),
people.ObjectUpdate(GetPeopleByState("California")),
people.ObjectUpdate(GetPeopleByState("Texas"))
]
await Task.WhenAll(tasks);Helper methods that work with collections such as IEnumerable, List, IDictionary, ConcurrentBag, and DataTable
Used to address issue CA1860 where it suggests using .Count for performance in an easier to type extension method
bool x = collection?.Any() == true;
//Or
collection?.Count > 0;
//Becomes
bool x = collection.AnyFast();
Used to directly TryAdd a KeyValuePair object(s) to a dictionary
KeyValuePair<string, string> JsonContentHeader = new("Content-Type", "application/json");
//Single addition
ConcurrentDictionary<string, string>? httpHeaders = [];
httpHeaders.AddDictionaryItem(JsonContentHeader);
//Add multiples
List<KeyValuePair<string, string>> keyValuePairs = [new("Accept-Encoding", "br"), new("Accept-Encoding", "gzip")];
httpHeaders.AddDictionaryItems(keyValuePairs);
[Description]
undefined
[Description]
undefined
[Description]
undefined
[Description]
undefined
[Description]
undefined
[Description]
undefined
[Description]
undefined
[Description]
undefined
[Description]
undefined
[Description]
undefined
[Description]
undefined
[Description]
undefined
[Description]
undefined
[Description here]
//Code here
[Description here]
//Code here
[Description here]
//Code here
[Description here]
//Code here
[Description here]
//Code here
[Description here]
//Code here
[Description here]
//Code here
[Description here]
//Code here
[Description here]
//Code here
[Description here]
//Code here
[Description here]
//Code here
[Description here]
//Code here
[Description here]
//Code here