Calebs.Extensions
1.3.0-ci-45
See the version list below for details.
dotnet add package Calebs.Extensions --version 1.3.0-ci-45
NuGet\Install-Package Calebs.Extensions -Version 1.3.0-ci-45
<PackageReference Include="Calebs.Extensions" Version="1.3.0-ci-45" />
paket add Calebs.Extensions --version 1.3.0-ci-45
#r "nuget: Calebs.Extensions, 1.3.0-ci-45"
// Install Calebs.Extensions as a Cake Addin #addin nuget:?package=Calebs.Extensions&version=1.3.0-ci-45&prerelease // Install Calebs.Extensions as a Cake Tool #tool nuget:?package=Calebs.Extensions&version=1.3.0-ci-45&prerelease
Calebs.Extensions
Useful extension methods and attributes for working with enums, strings and lists. The majority of these extensions were born out of working with various models while building micro services.
Installing Calebs.Extensions
You should install Extensions with NuGet:
Install-Package Calebs.Extensions
Or via the .NET Core command line interface:
dotnet add package Calebs.Extensions
Either command, from Package Manager Console or .NET Core CLI, will download and install Calebs.Extensions and all required dependencies.
.NET 7
These extensions target .NET 6 and .NET 7. With .NET 7 the Calebs.Extensions.Validators
include EnumStringValidator<T>
. The ability for Attributes to support <T> was added with .NET 7.
Helpers - not really extensions
EnumStringValidator
Used for string properties in models that are supposed to conform to an enum value. The philosophy of my micro-services have been to be liberal in what you accept and conservative in what you send.
Let's propose a scenario:
- you are recieving a message (model) that represents an account
with a field accountType
. Now, in this scenarios accountType
could be Standard, Silver or Gold
values. The easy way to restrict this is with en enum. The problem is - that if the incoming message doesn't exactly have one of those values (say "AccountStatus":"Gold-Status"
is passed in instead of "AccountStatus":"Gold"
), and if you are leveraging Microsoft Web API
with model binding - then it is likely that model binding will fail and you will return a 400 - Bad Request
by default. This is the correct response, but you might want to log what was actually sent, or add additional context like an error message stating what field or fields were incorrect and what values are supported for that field. This makes for a much more developer friendly API.
So instead of having your model directly bind to an enum - and throw a Bad Request exception - you can accept a string
in that field, and use the Calebs.Extensions.Validators.EnumStringValidatorAttribute
to perform model validation and then decide how to handle the errors.
SystemIO
IFileIO
A collection of thin shims for common File IO opperations. Helpful when you want to mock out the File IO opperations for testing.
The interface IFileIO
- every method is so slim that they each have a default implementaion. The default implementation FileIO
jump implements the Interface, but doesn't need to implement any of the methods.
To use this helper - register IFileIO
is your DI
with FileIO
as the implmentation. For unit tests use something like nSubstitute
to mock out and intercept interactions through IFileIO
methods.
- GetFiles(path, filter) // returns a string []
- DirectoryExists(path)
- GetDirectoryName(path)
- ReadAllText (path)
- FileExists(path)
- WriteAllLines(path, lines)
- DeleteFile(path)
Extension Methods
EnumExtensions
- ToList<D> -
- ToList(Type)
- Description (enum)
- Description<ToDesc>(Enum)
- Parse<T>
- Parse <T, D>
StringExtensions
- IsNotNullOrEmpty
- IsNullOrEmpty
- Compare
- string?.ValueOrEmpty()
ListExtensions
- ToDelimitedList
- ToUpper
- AddRange -
IList<T>.AddRange(IList<T>)
JsonExtensions
For both of these extension methods I'm using the Newtonsoft.Json
library. I'm planning on migrating to System.Text.Json
as soon as it is viable. Right now, Newtonsoft is easeir to serialize enums to thier ToString() value rather than index, and to deserialize the same. For example, by default - an enum is serialized to the index of the value. So an enum with (High, Med, Low) values would otherwise be serialiezed to a 2, instead of to "Med". Serializing to "Med" is my prefered behavior. I will continue to evailuate System.Text.Json
against these unit tests and most likely migrate at some point.
- ToJson<T>
- FromJson
Versioning
This package follow semantic versioning as much as possible.
Contributions
Please submit PR's to the develop
branch.
Merges to deveoper
automtically run all unit tests and publish a nuget package with the postfix -ci-build_number
Merges to main
publish to nuget as a major release.
Change Log
- 1.1.0 - added IList.AddRange extension method
- 1.2.0 - never published - only preview
- 1.3.0 - added IFileIO - an interface + implementation for making common filesystem opperations easier to test
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Newtonsoft.Json (>= 12.0.3)
-
net7.0
- Newtonsoft.Json (>= 12.0.3)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Calebs.Extensions:
Package | Downloads |
---|---|
Calebs.KeyValueRepo
a library abstraction for KeyValue data stores. |
|
Calebs.KeyValueRepo.SQLite
a SQLite implementation of the KeyValueRepo abstraction for KeyValue data stores. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.7.0 | 77 | 11/11/2024 |
1.7.0-ci-68 | 63 | 11/10/2024 |
1.6.0 | 159 | 1/21/2024 |
1.6.0-ci-67 | 88 | 1/20/2024 |
1.5.0 | 356 | 1/2/2024 |
1.5.0-ci-66 | 89 | 1/2/2024 |
1.5.0-ci-65 | 110 | 1/2/2024 |
1.5.0-ci-64 | 109 | 1/2/2024 |
1.5.0-ci-62 | 310 | 11/29/2023 |
1.4.0 | 177 | 9/8/2023 |
1.4.0-ci-61 | 139 | 9/8/2023 |
1.3.1 | 165 | 9/4/2023 |
1.3.1-ci-60 | 127 | 9/4/2023 |
1.3.1-ci-59 | 125 | 9/4/2023 |
1.3.1-ci-58 | 123 | 9/4/2023 |
1.3.1-ci-57 | 132 | 9/4/2023 |
1.3.1-ci-54 | 143 | 9/4/2023 |
1.3.1-ci-49 | 144 | 9/3/2023 |
1.3.0 | 156 | 9/3/2023 |
1.3.0-ci-48 | 131 | 9/3/2023 |
1.3.0-ci-47 | 128 | 9/3/2023 |
1.3.0-ci-46 | 127 | 9/3/2023 |
1.3.0-ci-45 | 121 | 9/2/2023 |
1.3.0-ci-44 | 135 | 9/2/2023 |
1.3.0-ci-43 | 129 | 9/2/2023 |
1.3.0-ci-42 | 139 | 9/1/2023 |
1.2.0-ci-41 | 132 | 9/1/2023 |
1.2.0-ci-39 | 435 | 5/30/2023 |
1.1.0-ci-38 | 131 | 5/30/2023 |
1.1.0-ci-37 | 136 | 5/28/2023 |
1.0.1-ci-36 | 152 | 4/23/2023 |
1.0.1-ci-35 | 141 | 4/23/2023 |
1.0.1-ci-33 | 149 | 4/4/2023 |
1.0.0 | 393 | 4/1/2023 |
1.0.0-ci-34 | 151 | 4/23/2023 |
1.0.0-ci-32 | 150 | 4/4/2023 |
1.0.0-ci-31 | 153 | 4/3/2023 |
1.0.0-ci-30 | 173 | 4/3/2023 |
1.0.0-ci-29 | 151 | 4/3/2023 |
1.0.0-ci-28 | 145 | 4/3/2023 |
1.0.0-ci-27 | 156 | 4/3/2023 |
1.0.0-ci-26 | 150 | 4/1/2023 |
1.0.0-ci-25 | 161 | 4/1/2023 |
1.0.0-ci-24 | 155 | 4/1/2023 |
1.0.0-ci-23 | 157 | 4/1/2023 |
1.0.0-ci-22 | 160 | 4/1/2023 |
1.0.0-ci-21 | 167 | 4/1/2023 |
1.0.0-ci-16 | 144 | 3/31/2023 |
1.0.0-beta-ci-17 | 151 | 3/31/2023 |
0.1.0-ci-15 | 162 | 3/31/2023 |