- AppSettingNotFoundException
- AppSettings
- AsyncHelper
- CleanerCodeExtensions
- DBHelper
- EnumerationExtensions
- LinqExtensions
- ObjectShredder
- RawQuery
- ConnectionString
- Execute(cString,sql,param,transaction,commandTimeout,commandType)
- ExecuteAsync(cString,sql,param,transaction,commandTimeout,commandType)
- ExecuteScalarAsync``1(cString,sql,param,transaction,commandTimeout,commandType)
- ExecuteScalar``1(cString,sql,param,transaction,commandTimeout,commandType)
- QueryAsync``1(cString,sql,param,transaction,commandTimeout,commandType)
- Query``1(cString,sql,param,transaction,buffered,commandTimeout,commandType)
- ReflectionCopy
- SerializationExtension
- StringExtensions
- SpecialChars
- ConvertHexToBin()
- EmptyIfNull(s)
- EqualsCI(a,b)
- FixSpaces(input)
- GetNumbers()
- HasQuotes()
- IsAnagram()
- Left(s,length)
- LimitDomain(input)
- MD5()
- RemoveDiacritics()
- RemoveDiacriticsAndNormalize()
- RemoveHtmlTags(text)
- RemoveSpacesAndNewLines(input)
- RemoveStrings(text,tags)
- Reverse()
- Right(value,length)
- SHA1()
- SplitAtFirstSpace(text)
- StringToIntArray()
- StripHtml(input)
- ToDate()
- TrimEndSpecialChars()
- TrimSpecialChars()
- TrimStartSpecialChars()
- TruncateAtWord(text,length)
- TryParseInt32()
- ValueObject
DotNet.CommonHelpers.Exceptions
Represents errors that occur during application execution.
Initializes a new instance of the AppSettingNotFoundException
This constructor has no parameters.
Initializes a new instance of the AppSettingNotFoundException class with a specified error message
Name | Type | Description |
---|---|---|
message | System.String | The message that describes the error. |
Initializes a new instance of the AppSettingNotFoundException class with a specified error message and a reference to the inner exception that is the cause of this exception.
Name | Type | Description |
---|---|---|
message | System.String | The message that describes the error. |
innerException | System.Exception | The exception that is the cause of the current exception, or a null reference if no inner exception is specified. |
Initializes a new instance of the AppSettingNotFoundException class with serialized data.
Name | Type | Description |
---|---|---|
info | System.Runtime.Serialization.SerializationInfo | The System.Runtime.Serialization.SerializationInfo that holds the serialized object data about the exception being thrown. |
context | System.Runtime.Serialization.StreamingContext | The System.Runtime.Serialization.StreamingContext that contains contextual information about the source or destination. |
DotNet.CommonHelpers
Easier reading of App.config
parameters.
Name | Type | Description |
---|---|---|
key | System.String | Appsettings key |
Name | Description |
---|---|
T | Type of the parameter |
Name | Description |
---|---|
DotNet.CommonHelpers.Exceptions.AppSettingNotFoundException | Thrown when specified key is not found in the appsettings |
var timeret = AppSettings.Get<int>("Timeout");
DotNet.CommonHelpers.Extensions
Helper for running async functions synchronously
Runs an async method synchronously
Name | Type | Description |
---|---|---|
func | System.Func{System.Threading.Tasks.Task} |
Runs an async function synchronously
Name | Type | Description |
---|---|---|
func | System.Func{System.Threading.Tasks.Task{``0}} |
Name | Description |
---|---|
TResult |
DotNet.CommonHelpers.Extensions
Extensions that make the code easier to read and mentain
Checks if the parameter meets the condition. It throws an exception if it does not
Name | Type | Description |
---|---|---|
param | ``0 | parameter to be checked |
condition | System.Func{``0,System.Boolean} | Condition to be met |
Name | Description |
---|---|
T |
int amount = 30; string productName = ""; amount.Require(a=>a>0); productName.Require(p=>!string.IsNullOrWhiteSpace(p));
DotNet.CommonHelpers.Extensions
Makes querying the database easier/more elegant
Global connection string. It has to be set if you wish to execute queries without specifying the connectionString
Global query CommandTimeout in seconds
Executes the query. If connectionString and timeout are null it takes the default settings. If ConnectionString is null you have to set it before the 1st call with DBHelper.ConnectionString=...
Results of the query in a datatable
Name | Type | Description |
---|---|---|
sqlQuery | System.String | SQL query to be executed |
connectionString | System.String | SQL Database connection string |
timeout | System.Nullable{System.Int32} | Command timeout in seconds |
var data = "SELECT * FROM [dbo].[Table]".QueryDT(); // gets results of the query as a DataTable
DotNet.CommonHelpers.Extensions
Syntactic suggar for better looking loops
Name | Type | Description |
---|---|---|
range | System.Range |
foreach(var i in 4..10)
Console.WriteLine(i);
foreach(var i in 42..13)
Console.WriteLine(i);
DotNet.CommonHelpers.Extensions
Linq extensions
Clones a list of clonable items
This method has no parameters.
Index of max value
This method has no parameters.
Write items to console using converter function func
This method has no parameters.
DotNet.CommonHelpers.Extensions
Converts an IEnumerable list to a DataTable
DataTable
Name | Type | Description |
---|---|---|
varlist | System.Collections.Generic.IEnumerable{``0} |
Name | Description |
---|---|
T | Type with parameters (getters and setters) |
class Test {
public int id { get; set; }
public string name { get; set; }
}
...
var input = new[] { new Test() { id=1, name="x" } };
var ret = input.EnumerableToDataTable(); // returns a datatable
DotNet.CommonHelpers.Extensions
Helpers for better looking dapper queries ex. var data = "SELECT ID, Name FROM dbo.MyTable".Query(cString);
Default connection string. It has to be set if you want to use Query commands without specifying the connection stirng
RawQuery.ConnectionString = "myconnection string...";
var data = "SELECT * FROM TestTable".Query<dynamic>();
Execute parameterized SQL.
The number of rows affected.
Name | Type | Description |
---|---|---|
cString | System.String | The connection string to query on. If cString is null you have to set it before the 1st call with RawQuery.ConnectionString |
sql | System.String | The SQL to execute for this query. |
param | System.Object | The parameters to use for this query. |
transaction | System.Data.IDbTransaction | The transaction to use for this query. |
commandTimeout | System.Nullable{System.Int32} | Number of seconds before command execution timeout. |
commandType | System.Nullable{System.Data.CommandType} | Is it a stored proc or a batch? |
"EXEC dbo.DoSomething @id".Execute(param: new {id=123}); // executes stored procedure dbo.DoSomething with the parameter @id = 123
Execute a command asynchronously using Task.
The number of rows affected.
Name | Type | Description |
---|---|---|
cString | System.String | The connection string to query on. If cString is null you have to set it before the 1st call with RawQuery.ConnectionString |
sql | System.String | The SQL to execute for this query. |
param | System.Object | The parameters to use for this query. |
transaction | System.Data.IDbTransaction | The transaction to use for this query. |
commandTimeout | System.Nullable{System.Int32} | Number of seconds before command execution timeout. |
commandType | System.Nullable{System.Data.CommandType} | Is it a stored proc or a batch? |
await "EXEC dbo.DoSomething @id".ExecuteAsync(param: new {id=123}); // executes stored procedure dbo.DoSomething with the parameter @id = 123
Execute parameterized SQL that selects a single value asynchronously using Task.
The first cell returned, as TEntity
.
Name | Type | Description |
---|---|---|
cString | System.String | The connection string to query on. If cString is null you have to set it before the 1st call with RawQuery.ConnectionString |
sql | System.String | The SQL to execute. |
param | System.Object | The parameters to use for this command. |
transaction | System.Data.IDbTransaction | The transaction to use for this command. |
commandTimeout | System.Nullable{System.Int32} | Number of seconds before command execution timeout. |
commandType | System.Nullable{System.Data.CommandType} | Is it a stored proc or a batch? |
Name | Description |
---|---|
TEntity | The type to return. |
var tableRowCount = await "SELECT count(1) FROM [dbo].[Table]".ExecuteScalarAsync<int>(); // gets number of records of dbo.Table
Execute parameterized SQL that selects a single value.
The first cell returned, as TEntity
.
Name | Type | Description |
---|---|---|
cString | System.String | The connection string to query on. If cString is null you have to set it before the 1st call with RawQuery.ConnectionString |
sql | System.String | The SQL to execute. |
param | System.Object | The parameters to use for this command. |
transaction | System.Data.IDbTransaction | The transaction to use for this command. |
commandTimeout | System.Nullable{System.Int32} | Number of seconds before command execution timeout. |
commandType | System.Nullable{System.Data.CommandType} | Is it a stored proc or a batch? |
Name | Description |
---|---|
TEntity | The type to return. |
var tableRowCount = "SELECT count(1) FROM [dbo].[Table]".ExecuteScalar<int>(); // gets number of records of dbo.Table
Executes a query asynchronously using Task, returning the data typed as TEntity
.
A sequence of data of the supplied type; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is created per row, and a direct column-name===member-name mapping is assumed (case insensitive).
Name | Type | Description |
---|---|---|
cString | System.String | The connection string to query on. If cString is null you have to set it before the 1st call with RawQuery.ConnectionString |
sql | System.String | The SQL to execute for the query. |
param | System.Object | The parameters to pass, if any. |
transaction | System.Data.IDbTransaction | The transaction to use, if any. |
commandTimeout | System.Nullable{System.Int32} | The command timeout (in seconds). |
commandType | System.Nullable{System.Data.CommandType} | The type of command to execute. |
Name | Description |
---|---|
TEntity | The type of results to return. |
var data = await "SELECT * FROM [dbo].[Table]".QueryAsync<dynamic>(); // gets results of the query as IEnumerble dynamic type
class Table{
public int ID {get;set;}
public string Name {get;set;}
}
var tableData = await "SELECT * FROM [dbo].[Table]".QueryAsync<Table>(); // gets results of the query and mapps them to IEnumerable<Table>
Executes a query, returning the data typed as TEntity
.
A sequence of data of the supplied type; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is created per row, and a direct column-name===member-name mapping is assumed (case insensitive).
Name | Type | Description |
---|---|---|
cString | System.String | The connection string to query on. If cString is null you have to set it before the 1st call with RawQuery.ConnectionString |
sql | System.String | The SQL to execute for the query. |
param | System.Object | The parameters to pass, if any. |
transaction | System.Data.IDbTransaction | The transaction to use, if any. |
buffered | System.Boolean | Whether to buffer results in memory. |
commandTimeout | System.Nullable{System.Int32} | The command timeout (in seconds). |
commandType | System.Nullable{System.Data.CommandType} | The type of command to execute. |
Name | Description |
---|---|
TEntity | The type of results to return. |
var data = "SELECT * FROM [dbo].[Table]".Query<dynamic>(); // gets results of the query as IEnumerble dynamic type
class Table{
public int ID {get;set;}
public string Name {get;set;}
}
var tableData = "SELECT * FROM [dbo].[Table]".Query<Table>(); // gets results of the query and mapps them to IEnumerable<Table>
DotNet.CommonHelpers.Extensions
Deep copying of objects using reflection
Copies an array (source) to a destination array of type destinationType
Name | Type | Description |
---|---|---|
source | System.Collections.Generic.List{System.Object} | |
destinationType | System.Type |
Makes a shallow copy of a similar object (same properties!)
Name | Type | Description |
---|---|---|
source | System.Object | |
destinationType | System.Type |
Copies an array (source) to a destination array of type T
Name | Type | Description |
---|---|---|
source | System.Object |
Copies an array (source) to a destination array of type T that skips the parameters in skipParams list
Name | Type | Description |
---|---|---|
source | System.Object | |
skipParams | System.Collections.Generic.List{System.String} |
Makes a shallow copy of a similar object (same properties!)
Name | Type | Description |
---|---|---|
source | System.Object | |
skipParams | System.Collections.Generic.List{System.String} |
Name | Description |
---|---|
T |
Makes a shallow copy of a similar object (same properties!)
Name | Type | Description |
---|---|---|
source | System.Object |
Name | Description |
---|---|
T |
Name | Type | Description |
---|---|---|
source | System.Object | |
destination | System.Object |
Name | Type | Description |
---|---|---|
source | System.Object | |
destination | System.Object | |
skipParams | System.Collections.Generic.List{System.String} | list of parameter names to be ignored |
DotNet.CommonHelpers.Extensions
Easier object serialization and deserialization
Deserializes an xml string in to an object of Type T
A new object of type T is successful, null if failed
Name | Type | Description |
---|---|---|
xml | System.String | Xml as string to deserialize from |
Name | Description |
---|---|
T | Any class type |
Serializes an object of type T in to an xml string
A string that represents Xml, empty otherwise
Name | Type | Description |
---|---|---|
obj | ``0 | Object to serialize |
Name | Description |
---|---|
T | Any class type |
Dotnet.CommonHelpers.Extensions
Additional extension methods for strings.
List of special character for trimming. Default values are ' ', '\t', '\r', '\n', '.', ',', '!', '?', '/', '\'
Converst a hex representation of the string into binary one
This method has no parameters.
var t1 = "A0".ConvertHexToBin(); // returns false
Returns empty string if string is null
Name | Type | Description |
---|---|---|
s | System.String |
Case insesitive equals operator
true if strings are equal (case insensitive)
Name | Type | Description |
---|---|---|
a | System.String | 1st parameter |
b | System.String | 2nd parameter |
var ret = "Test".EqualsCI("test"); // returns true;
Replaces multiple spaces with one
string with multiple spaces removed
Name | Type | Description |
---|---|---|
input | System.String |
var ret = "to je testno besedilo".FixSpaces(); // returns "to je testno besedilo"
Splits the string and keeps only the numbers. Anything other than a number is treated as a separator
This method has no parameters.
var numbers = "1,24b32test3 6".GetNumbers(); // returns {1,24,32,3,6}
Checks if a string starts with openingQuote and ends with closing quote
This method has no parameters.
bool hasQuotes = "someString".HasQuotes(); // returns false
Returns true if the Second word is an anagram of the current string
This method has no parameters.
var t1 = "ABC".IsAnagram("ABE"); // returns false
var t2 = "ELVIS".IsAnagram("LIVES"); // returns true
Returns the "lenght" leftmost characters
Name | Type | Description |
---|---|---|
s | System.String | |
length | System.Int32 |
Truncates the url, so it contains only the domain and two first folders, if they exist ex http://www.google.com/reader/test?234iojapjapriogjerg becomes http://www.google.com/reader
Truncated url
Name | Type | Description |
---|---|---|
input | System.String |
var ret = "http://www.google.si/search/dva".LimitDomain(); // returns "http://www.google.si/search"
Returns a MD5 hash of the string
This method has no parameters.
var ret = "test".MD5(); // returns "C8059E2EC7419F590E79D7F1B774BFE6"
Removes diacritics from a string (ščžć...)
This method has no parameters.
var x = "ščepec".RemoveDiacritics(); // returns "scepec"
Removes diacritics (ščž..) and normalizes string https://stackoverflow.com/questions/3288114/what-does-nets-string-normalize-do replaces spaces with hypens(-) removes + and .
This method has no parameters.
Removes HTML tags from string. (all text that is between < and >)
Text without html tags
Name | Type | Description |
---|---|---|
text | System.String |
var ret = "
Remove spaces and newlines from string
Clean string, without spaces and newlines
Name | Type | Description |
---|---|---|
input | System.String |
Removes all strings specified in tags
Cleaned string
Name | Type | Description |
---|---|---|
text | System.String | |
tags | System.String[] | Strings to be removed / replaced with empty. |
Reverses the string
This method has no parameters.
var ret = "ABCD".Reverse(); // returns "DCBA"
Get substring of specified number of characters on the right.
right lenght
of characters
Name | Type | Description |
---|---|---|
value | System.String | |
length | System.Int32 |
var ret = "testno".Right(2); // returns "no"
Returns a SHA1 hash of the string
This method has no parameters.
var ret = "test".SHA1(); // returns "C8059E2EC7419F590E79D7F1B774BFE6"
Split string at first space into two strings.
Split string
Name | Type | Description |
---|---|---|
text | System.String | String to be split |
var ret = "John Fitzgerald Kennedy".SplitAtFirstSpace() // returns {"John", "Fitzgerald Kennedy"}
Converts a delimited string array to an array of ints. Tokens that can't be converted are left out
This method has no parameters.
var ret = "1,3,e,c,11,abba".StringToIntArray(); // returns {1,3,11}
Strips html tags from input
Name | Type | Description |
---|---|---|
input | System.String |
Converts a string to Datetime. If no conversion is possible it returns null
This method has no parameters.
var ret1 = "3.2.2015".ToDate(); // returns [3.02.2016 00:00:00] datetime object
var ret1 = "33.2.2015".ToDate(); // returns null
Trims the ending of a string if a character is any of SpecialChars
This method has no parameters.
var ret = "! !?someString!".TrimEndSpecialChars(); // returns "! !?someString"
Trims a string of SpecialChars
This method has no parameters.
var ret = "! !?someString!".TrimSpecialChars(); // returns "someString"
Trims the beginning of a string if a character is any of SpecialChars
This method has no parameters.
var ret = "! !?someString!".TrimStartSpecialChars(); // returns "someString!"
Truncate the string before specified length, without breaking words.
Truncated string
Name | Type | Description |
---|---|---|
text | System.String | String to be truncated |
length | System.Int32 | Max length of the truncated string |
Parses a string to an int. If the string is not an int it returns null
This method has no parameters.
var ret = "! !?someString!".TryParseInt32(); // returns null
var out2 = "123".TryParseInt32(); // returns 123
DotNet.CommonHelpers.Extensions