Kros.Utils
4.3.0
dotnet add package Kros.Utils --version 4.3.0
NuGet\Install-Package Kros.Utils -Version 4.3.0
<PackageReference Include="Kros.Utils" Version="4.3.0" />
<PackageVersion Include="Kros.Utils" Version="4.3.0" />
<PackageReference Include="Kros.Utils" />
paket add Kros.Utils --version 4.3.0
#r "nuget: Kros.Utils, 4.3.0"
#:package Kros.Utils@4.3.0
#addin nuget:?package=Kros.Utils&version=4.3.0
#tool nuget:?package=Kros.Utils&version=4.3.0
Kros.Utils 
Kros.Utils is universal library of various tools to simplify the work of the programmer. Library is:
- Independent of third-party libraries. You only need .NET Framework.
- Platform-independent. That means it's applicable to desktop applications and server services (e.g. It's independent of System.Windows.Forms).
Library is compiled for .NET Standard 2.0. .NET Framework 4.8 is supported.
Table of contents
Documentation
For configuration, general information and examples see the documentation.
Download
Kros.Utils is available as Nuget Kros.Utils.
Contributing Guide
To contribute with new topics/information or make changes, see contributing for instructions and guidelines.
Arguments Check Functions
The Check class provides simple tools to check arguments of the functions. Standard usage:
private string _value1;
private int _value2;
public void MethodWithParameters(string arg1, int arg2)
{
if (string.IsNullOrEmpty(arg1))
{
throw new ArgumentNullException(nameof(arg1));
}
if (arg2 <= 0)
{
throw new ArgumentException("Value of parameter arg2 must be greater than 0.", nameof(arg2));
}
_value1 = arg1;
_value2 = arg2;
// ...
}
With the Check class, it's much more simplier. The individual checks return the input value, so it's possible to check the argument on one line, even to assign:
private string _value1;
private int _value2;
public void MethodWithParameters(string arg1, int arg2)
{
_value1 = Check.NotNullOrEmpty(arg1, nameof(arg1));
_value2 = Check.GreaterThan(arg2, 0, nameof(arg2));
// ...
}
The Check offers string check, type check, value check (equal, smaller, larger, ...), check if the value is in the list, check GUID values.
Standard Extensions
General extensions for:
- Strings - String Extensions
- Dates - DateTime Extensions
URI
"https://gmail.google.com/inbox".GetDomain()
.Should()
.Be("google.com")
File/Folder Path Helpers
The PathHelper class provides functions to work with files/folder paths.
- PathHelper.BuildPath serve to join multiple string into one path, as well as standard function Path.Combine but with some changed details.
- PathHelper.ReplaceInvalidPathChars in the input string will replace all characters that are not applicable to the file path.
The PathFormatter class includes functions for formatting paths to output files so that the result path is valid. The class checks the maximum allowed length of the path so that the result path does not exceed it.
The class is not static. If necessary, you can inherit it and modify its behavior. For simple use, the Default instance is created.
PathFormatter.Default.FormatPath("C:\data\export", "exportFile.txt")returns pathC:\data\export\exportFile.txt. If theresulting path is too long, the file name is automatically truncated (the suffix is preserved) so the path is valid.PathFormatter.Default.FormatNewPath("C:\data\export", "exportFile.txt")returns pathC:\data\export\exportFile.txt. However, if the resulting file already exists, it automatically adds a counter to the name so that the return path is to a non-existent file:C:\data\export\exportFile (1).txt. If the resulting path was too long, the file name is automatically truncated so that the path is valid. The suffix and counter are preserved.
The PathFormatter class can also be used to create a list of paths with specified parameters. More information is provided in the documentation of each of its functions.
Database Scheme
It is simple to get a database schema. The database schema includes TableSchema tables, their ColumnSchema columns, IndexSchema indexes, and ForeignKeySchema foreign keys (foreign keys are only supported for SQL Server).
SqlConnection cn = new SqlConnection("...");
DatabaseSchema schema = DatabaseSchemaLoader.Default.LoadSchema(cn);
Since getting a schema is a time-consuming operation, it is a good idea to use the DatabaseSchemaCache cache for the schemas you are reading. It holds the schema once it is retrieved and is returned from the memory upon the next schema request. The class can be used either by creating its instance or by using the static DatabaseSchemaCache.Default property for ease of use.
SqlConnection cn = new SqlConnection("...");
// Use to create your own instance.
var cache = new DatabaseSchemaCache();
DatabaseSchema schema = cache.GetSchema(cn);
// Using a static property.
schema = DatabaseSchemaCache.Default.GetSchema(cn);
Bulk Operations - Bulk Insert and Bulk Update
Inserting (INSERT) and updating (UPDATE) large amounts of data in a database are time-consuming. Therefore, support for rapid
mass insertion, Bulk Insert and a fast bulk update, Bulk Update. The IBulkInsert and IBulkUpdate interfaces are used.
They are implemented for SQL Server in the SqlServerBulkInsert and SqlServerBulkUpdate classes. As a data source, it serves
any IDataReader or DataTable.
Because IDataReader is an intricate interface, you just need to implement the simplier interface IBulkActionDataReader.
If the source is a list (IEnumerable), it is sufficient to use the EnumerableDataReader<T>
class for its bulk insertion.
Bulk Insert
private class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
public void InsertManyItems()
{
IEnumerable<Item> data = GetData();
using (var reader = new EnumerableDataReader<Item>(data, new string[] { "Id", "Name" }))
{
using (var bulkInsert = new SqlServerBulkInsert("connection string"))
{
bulkInsert.Insert(reader);
}
}
}
private class BulkUpdateItem
{
public int Id { get; set; }
public string Name { get; set; }
}
Bulk Update
public void UpdateManyItems()
{
IEnumerable<BulkUpdateItem> data = GetItems();
using (var reader = new EnumerableDataReader<BulkUpdateItem>(data, new string[] { "Id", "Name" }))
{
using (var bulkUpdate = new SqlServerBulkUpdate("connection string"))
{
bulkUpdate.DestinationTableName = "TableName";
bulkUpdate.PrimaryKeyColumn = "Id";
bulkUpdate.Update(reader);
}
}
}
IdGenerator
Generating unique (incrementally) values in databases (most common Id) is not easy. In the namespace Kros.Data there is the
IIdGenerator interface that describes exactly such a unique value generator. They are currently supported for SqlServer
and MsAccess. We do not create their instances directly but through the IIdGeneratorFactory factory class. We can get
a factory with GetFactory(DbConnection) in the IdGeneratorFactories class.
public class PeopleService
{
private IIdGeneratorFactory _idGeneratorFactory;
public PeopleService(IIdGeneratorFactory idGeneratorFactory)
{
_idGeneratorFactory = Check.NotNull(idGeneratorFactory, nameof(idGeneratorFactory));
}
public void GenerateData()
{
using (var idGenerator = _idGeneratorFactory.GetGenerator("people", 1000))
{
for (int i = 0; i < 1800; i++)
{
var person = new Person()
{
Id = idGenerator.GetNext()
};
};
}
}
}
An IdStore table is required in the database. For SQL Server, the stored procedure spGetNewId is required.
In order to create everything necessary for the ID generator to work it is possible to use the methods of the individual generators
SqlServerIdGenerator.InitDatabaseForIdGenerator for SQL Server and MsAccessIdGenerator.InitDatabaseForIdGenerator for MS Access.
If your existing implementations do not suit you (for example, you have another id store table) you can create a custom implementation of the IIdGenerator and IIdGeneratorFactory interfaces that you register using the Register method in the IdGeneratorFactories class.
IDiContainer Interface Describing Dependency Injection Container
The IDiContainer interface is not directly implemented in Kros.Utils because Kros.Utils does not have any external
dependencies. However, the interface is implemented using the Unity container in the KrosUnityContainer class in the
Kros.Utils.UnityContainer library.
Caching
The very simple Cache<TKey, TValue> is implemented. The class holds values to the specified keys. When the value is retrieved the returned value is already in the cache.
Unit Testing Helpers
Standard unit tests should be database-independent. But sometimes it is necessary to test the actual database because the test items are directly related to it. To test the actual database you can use the SqlServerTestHelper class. It creates a database for testing purposes on the server and runs tests over it. When tests are finished the database is deleted.
// In the connection string there is no specified database
// because it is automatically created with random name.
// At the end of the job, the database is automatically deleted.
private const string BaseConnectionString = "Data Source=SQLSERVER;Integrated Security=True;";
private const string CreateTestTableScript =
@"CREATE TABLE [dbo].[TestTable] (
[Id] [int] NOT NULL,
[Name] [nvarchar](255) NULL,
CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED ([Id] ASC) ON [PRIMARY]
) ON [PRIMARY];";
[Fact]
public void DoSomeTestWithDatabase()
{
using (var serverHelper = new SqlServerTestHelper(BaseConnectionString, "TestDatabase", CreateTestTableScript))
{
// Do tests with connection serverHelper.Connection.
}
}
When testing MS Access database you can use MsAccessTestHelper from Kros.Utils.MsAccess library.
The superstructure over the SqlServerTestHelper class is the basic class for creating additional test classes
SqlServerDatabaseTestBase. In a child just rewrite the BaseConnectionString property to connect to the database, and then
just write the tests. The class in the constructor creates a temporary empty database in which it is possible to "play" and this
database is deleted during Dispose(). Internally SqlServerTestHelper is used which is available in the ServerHelper
property. There is also a link to the database itself.
If you need to initialize the test database (create some tables, fill dates, etc.) just rewrite the DatabaseInitScripts property and return the scripts that initialize the database.
There is no need to solve database initialization for individual tests because xUnit creates a new instance of the test class
for each test. So each test has its own initialized test database.
The class is for SQL Server. There is no similar class for MS Access.
public class SomeDatabaseTests
: Kros.UnitTests.SqlServerDatabaseTestBase
{
protected override string BaseConnectionString => "Data Source=TESTSQLSERVER;Integrated Security=True";
[Fact]
public void Test1()
{
using (var cmd = ServerHelper.Connection.CreateCommand())
{
// Use cmd to execute queries.
}
}
[Fact]
public void Test2()
{
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Azure.Identity (>= 1.21.0)
- Microsoft.Data.SqlClient (>= 7.0.0)
- Microsoft.Net.Http.Headers (>= 2.3.9)
-
net10.0
- Azure.Identity (>= 1.21.0)
- Microsoft.Data.SqlClient (>= 7.0.0)
- Microsoft.Net.Http.Headers (>= 10.0.6)
NuGet packages (10)
Showing the top 5 NuGet packages that depend on Kros.Utils:
| Package | Downloads |
|---|---|
|
MMLib.SwaggerForOcelot
Swagger generator for Ocelot downstream services. |
|
|
Kros.AspNetCore
General utilities and helpers for building ASP.NET Core WEB API |
|
|
Kros.KORM
KORM is fast, easy to use, micro ORM tool (Kros Object Relation Mapper). |
|
|
Kros.ApplicationInsights.Extensions
Extensions to facilitate work with Application Insights. |
|
|
Kros.MassTransit.AzureServiceBus
Simpler configuration of MassTransit library. |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Kros.Utils:
| Repository | Stars |
|---|---|
|
Burgyn/MMLib.SwaggerForOcelot
This repo contains swagger extension for ocelot.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 4.3.0 | 134 | 5/6/2026 |
| 4.2.0 | 181 | 4/21/2026 |
| 4.1.0 | 1,064 | 2/12/2026 |
| 4.0.0 | 4,721 | 12/18/2025 |
| 3.1.0 | 30,958 | 7/4/2025 |
| 3.0.2 | 215,095 | 1/2/2025 |
| 3.0.1 | 9,695 | 9/5/2024 |
| 3.0.0 | 22,839 | 12/15/2023 |
| 2.1.0 | 1,232,143 | 3/27/2023 |
| 2.0.0 | 28,451 | 12/12/2022 |
| 1.19.0 | 828,995 | 11/8/2021 |
| 1.18.0 | 26,915 | 4/16/2021 |
| 1.17.0 | 1,544 | 4/14/2021 |
| 1.16.2 | 14,597 | 11/1/2020 |
| 1.15.0 | 2,145 | 9/18/2020 |
| 1.14.0 | 7,636 | 5/25/2020 |
| 1.13.0 | 14,408 | 3/23/2020 |
| 1.12.0 | 1,899 | 2/25/2020 |