initial commit

This commit is contained in:
FH 2020-10-19 11:22:06 +02:00
commit 28d9e16729
13 changed files with 1566 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/bin/**/**
/obj/**/**
/App_Data/**/**

37
ExtensionsEx.cs Normal file
View file

@ -0,0 +1,37 @@
// ***********************************************************************
// Assembly : FCS.Lib
// Author : FH
// Created : 27-08-2016
//
// Last Modified By : Frede H.
// Last Modified On : 2020-08-30
// ***********************************************************************
// <copyright file="ExtensionsEx.cs" company="FCS">
// Copyright © FCS 2015-2020
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
namespace FCS.Lib
{
/// <summary>
/// Class ExtensionsEx.
/// </summary>
public static class ExtensionsEx
{
/// <summary>
/// ForEach loop
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items">The items.</param>
/// <param name="action">The action.</param>
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
foreach (var item in items)
action(item);
}
}
}

73
FCS.Lib.csproj Normal file
View file

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1F1FECFD-E07E-4B5C-A7F9-67FB89EE94A3}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FCS.Lib</RootNamespace>
<AssemblyName>FCS.Lib</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ExtensionsEx.cs" />
<Compile Include="Generators.cs" />
<Compile Include="IRepository.cs" />
<Compile Include="IRepositoryAsync.cs" />
<Compile Include="IRepositoryEx.cs" />
<Compile Include="Mogrifiers.cs" />
<Compile Include="StringOptions.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<DependentUpon>AssemblyInfo.tt</DependentUpon>
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
</Compile>
<Compile Include="Squid.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Content Include=".gitignore" />
<Content Include="Properties\AssemblyInfo.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>AssemblyInfo.cs</LastGenOutput>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\TextTransform.exe" -out "$(ProjectDir)Properties\AssemblyInfo.cs" "$(ProjectDir)Properties\AssemblyInfo.tt"</PreBuildEvent>
</PropertyGroup>
</Project>

6
FCS.Lib.csproj.user Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ShowAllFiles</ProjectView>
</PropertyGroup>
</Project>

298
Generators.cs Normal file
View file

@ -0,0 +1,298 @@
// ***********************************************************************
// Assembly : FCS.Lib
// Author : FH
// Created : 2020-07-01
//
// Last Modified By : FH
// Last Modified On : 2020-09-11
// ***********************************************************************
// <copyright file="Generators.cs" company="Frede Hundewadt">
// Copyright © FCS 2015-2020
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
namespace FCS.Lib
{
/// <summary>
/// Class StringUtils.
/// </summary>
public static class Generators
{
/// <summary>
/// Shorts the URL generator.
/// </summary>
/// <returns>System.String.</returns>
public static string ShortUrlGenerator()
{
return ShortUrlGenerator(6);
}
/// <summary>
/// Randoms the string.
/// </summary>
/// <param name="length">The lengt h.</param>
/// <returns>System.String.</returns>
public static string ShortUrlGenerator(int length)
{
const string charsLower = "cdfghjkmnpqrstvwxyz";
const string charsUpper = "BCDFGHJKLMNPQRSTVWXYZ-_";
const string charsNumeric = "23456789";
// Create a local array containing supported short-url characters
// grouped by types.
var charGroups = new[]
{
charsLower.ToCharArray(),
charsUpper.ToCharArray(),
charsNumeric.ToCharArray()
};
// Use this array to track the number of unused characters in each
// character group.
var charsLeftInGroup = new int[charGroups.Length];
// Initially, all characters in each group are not used.
for (var i = 0; i < charsLeftInGroup.Length; i++)
charsLeftInGroup[i] = charGroups[i].Length;
// Use this array to track (iterate through) unused character groups.
var leftGroupsOrder = new int[charGroups.Length];
// Initially, all character groups are not used.
for (var i = 0; i < leftGroupsOrder.Length; i++)
leftGroupsOrder[i] = i;
// Using our private randomizer
var random = RandomSeed();
// This array will hold short-url characters.
// Allocate appropriate memory for the short-url.
var shortUrl = new char[random.Next(length, length)];
// Index of the last non-processed group.
var lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;
// Generate short-url characters one at a time.
for (var i = 0; i < shortUrl.Length; i++)
{
// If only one character group remained unprocessed, process it;
// otherwise, pick a random character group from the unprocessed
// group list. To allow a special character to appear in the
// first position, increment the second parameter of the Next
// function call by one, i.e. lastLeftGroupsOrderIdx + 1.
int nextLeftGroupsOrderIdx;
if (lastLeftGroupsOrderIdx == 0)
nextLeftGroupsOrderIdx = 0;
else
nextLeftGroupsOrderIdx = random.Next(0,
lastLeftGroupsOrderIdx);
// Get the actual index of the character group, from which we will
// pick the next character.
var nextGroupIdx = leftGroupsOrder[nextLeftGroupsOrderIdx];
// Get the index of the last unprocessed characters in this group.
var lastCharIdx = charsLeftInGroup[nextGroupIdx] - 1;
// If only one unprocessed character is left, pick it; otherwise,
// get a random character from the unused character list.
var nextCharIdx = lastCharIdx == 0 ? 0 : random.Next(0, lastCharIdx + 1);
// Add this character to the short-url.
shortUrl[i] = charGroups[nextGroupIdx][nextCharIdx];
// If we processed the last character in this group, start over.
if (lastCharIdx == 0)
{
charsLeftInGroup[nextGroupIdx] =
charGroups[nextGroupIdx].Length;
}
// There are more unprocessed characters left.
else
{
// Swap processed character with the last unprocessed character
// so that we don't pick it until we process all characters in
// this group.
if (lastCharIdx != nextCharIdx)
{
var temp = charGroups[nextGroupIdx][lastCharIdx];
charGroups[nextGroupIdx][lastCharIdx] =
charGroups[nextGroupIdx][nextCharIdx];
charGroups[nextGroupIdx][nextCharIdx] = temp;
}
// Decrement the number of unprocessed characters in
// this group.
charsLeftInGroup[nextGroupIdx]--;
}
// If we processed the last group, start all over.
if (lastLeftGroupsOrderIdx == 0)
{
lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;
}
// There are more unprocessed groups left.
else
{
// Swap processed group with the last unprocessed group
// so that we don't pick it until we process all groups.
if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx)
{
var temp = leftGroupsOrder[lastLeftGroupsOrderIdx];
leftGroupsOrder[lastLeftGroupsOrderIdx] =
leftGroupsOrder[nextLeftGroupsOrderIdx];
leftGroupsOrder[nextLeftGroupsOrderIdx] = temp;
}
// Decrement the number of unprocessed groups.
lastLeftGroupsOrderIdx--;
}
}
// Convert password characters into a string and return the result.
return new string(shortUrl);
}
public static string GenerateUsername(StringOptions options = null)
{
options ??= new StringOptions
{
RequiredLength = 10,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
RequiredUniqueChars = 4,
RequireNonLetterOrDigit = false,
RequireNonAlphanumeric = false
};
return GenerateRandomString(options);
}
public static string GeneratePassword(StringOptions options = null)
{
options ??= new StringOptions
{
RequiredLength = 10,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
RequiredUniqueChars = 4,
RequireNonLetterOrDigit = true,
RequireNonAlphanumeric = true
};
return GenerateRandomString(options);
}
public static string GenerateRandomText(int length)
{
const string consonants = "bdfghjklmnprstvyBDFGHJKLMNPRSTVY";
const string vowels = "aeiouAEIOU";
var rndString = "";
var randomNum = RandomSeed();
while (rndString.Length < length)
{
rndString += consonants[randomNum.Next(consonants.Length)];
if (rndString.Length < length)
rndString += vowels[randomNum.Next(vowels.Length)];
}
return rndString;
}
/// <summary>
/// Generates the random password.
/// </summary>
/// <param name="options">The options.</param>
/// <returns>System.String.</returns>
public static string GenerateRandomString(StringOptions options = null)
{
options ??= new StringOptions
{
RequiredLength = 10,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
RequiredUniqueChars = 4,
RequireNonLetterOrDigit = true,
RequireNonAlphanumeric = true
};
var randomChars = new[]
{
"ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase
"abcdefghijkmnopqrstuvwxyz", // lowercase
"0123456789", // digits
"!@$?_-" // non-alphanumeric
};
// Using our private randomizer
var rand = RandomSeed();
var chars = new List<char>();
if (options.RequireUppercase)
chars.Insert(rand.Next(0, chars.Count),
randomChars[0][rand.Next(0, randomChars[0].Length)]);
if (options.RequireLowercase)
chars.Insert(rand.Next(0, chars.Count),
randomChars[1][rand.Next(0, randomChars[1].Length)]);
if (options.RequireDigit)
chars.Insert(rand.Next(0, chars.Count),
randomChars[2][rand.Next(0, randomChars[2].Length)]);
if (options.RequireNonAlphanumeric)
chars.Insert(rand.Next(0, chars.Count),
randomChars[3][rand.Next(0, randomChars[3].Length)]);
var rcs = randomChars[rand.Next(0, randomChars.Length)];
for (var i = chars.Count;
i < options.RequiredLength
|| chars.Distinct().Count() < options.RequiredUniqueChars;
i++)
chars.Insert(rand.Next(0, chars.Count),
rcs[rand.Next(0, rcs.Length)]);
return new string(chars.ToArray());
}
/// <summary>
/// Randoms the seed.
/// </summary>
/// <returns>Random.</returns>
public static Random RandomSeed()
{
// As the default randomizer is based on the current time
// so it produces the same "random" number within a second
// Use a crypto randomizer to create the seed value
// 4-byte array to fill with random bytes
var randomBytes = new byte[4];
// Generate 4 random bytes.
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(randomBytes);
}
// Convert 4 bytes into a 32-bit integer value.
var seed = ((randomBytes[0] & 0x7f) << 24) |
(randomBytes[1] << 16) |
(randomBytes[2] << 8) |
randomBytes[3];
// Return a truly randomized random generator
return new Random(seed);
}
}
}

49
IRepository.cs Normal file
View file

@ -0,0 +1,49 @@
// ***********************************************************************
// Assembly : FCS.Lib
// Author : FH
// Created : 05-13-2020
//
// Last Modified By : FH
// Last Modified On : 2020-08-30
// ***********************************************************************
// <copyright file="IRepository.cs" company="FCS">
// Copyright © FCS 2015-2020
// </copyright>
// <summary></summary>
// ***********************************************************************
namespace FCS.Lib
{
/// <summary>
/// Interface IRepository
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TKey">The type of the TKey.</typeparam>
public interface IRepository<T, in TKey> where T : class
{
/// <summary>
/// Gets the specified identifier.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>T.</returns>
T GetById(TKey id);
/// <summary>
/// Creates the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
void Create(T entity);
/// <summary>
/// Updates the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
void Update(T entity);
/// <summary>
/// Deletes the specified identifier.
/// </summary>
/// <param name="id">The identifier.</param>
void Delete(TKey id);
}
}

122
IRepositoryAsync.cs Normal file
View file

@ -0,0 +1,122 @@
// ***********************************************************************
// Assembly : FCS.Lib
// Author : FH
// Created : 03-10-2015
//
// Last Modified By : FH
// Last Modified On : 2020-08-30
// ***********************************************************************
// <copyright file="IRepositoryAsync.cs" company="FCS">
// Copyright © FCS 2015-2020
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace FCS.Lib
{
/// <summary>
/// Interface IRepositoryEx
/// </summary>
/// <typeparam name="TEntity">The type of the t entity</typeparam>
public interface IRepositoryAsync<TEntity> where TEntity : class
{
/// <summary>
/// Get all entities asynchronous
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>Task&lt;System.Boolean&gt;</returns>
Task<bool> AllAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Get all entities synchronous
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>Task&lt;System.Boolean&gt;</returns>
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Find matching entity asynchronous
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>Task&lt;TEntity&gt;</returns>
Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Find first matching entity asynchronous
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>Task&lt;TEntity&gt;</returns>
Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Get first entity matching query or default entity asynchronous
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>Task&lt;TEntity&gt;</returns>
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Add an entity
/// </summary>
/// <param name="entity">The entity.</param>
void Add(TEntity entity);
/// <summary>
/// Attach the entity
/// </summary>
/// <param name="entity">The entity.</param>
void Attach(TEntity entity);
/// <summary>
/// Delete the entity
/// </summary>
/// <param name="entity">The entity.</param>
void Delete(TEntity entity);
/// <summary>
/// Anies the specified predicate.
/// </summary>
/// <param name="predicate">The predicate.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
bool Any(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Get all entities
/// </summary>
/// <returns>IQueryable&lt;TEntity&gt;</returns>
IQueryable<TEntity> All();
/// <summary>
/// Find all matching entities matching query
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>IQueryable&lt;TEntity&gt;</returns>
IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Find first entity matching query
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>TEntity</returns>
TEntity First(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Find first matching entity or default entity
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>TEntity</returns>
TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Get entity by id
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>TEntity</returns>
TEntity GetById(string id);
}
}

122
IRepositoryEx.cs Normal file
View file

@ -0,0 +1,122 @@
// ***********************************************************************
// Assembly : FCS.Lib
// Author : FH
// Created : 03-10-2015
//
// Last Modified By : FH
// Last Modified On : 2020-08-30
// ***********************************************************************
// <copyright file="IRepositoryEx.cs" company="FCS">
// Copyright © FCS 2015-2020
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace FCS.Lib
{
/// <summary>
/// Interface IRepositoryEx
/// </summary>
/// <typeparam name="TEntity">The type of the t entity</typeparam>
public interface IRepositoryEx<TEntity> where TEntity : class
{
/// <summary>
/// Get all entities asynchronous
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>Task&lt;System.Boolean&gt;</returns>
Task<bool> AllAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Get all entities synchronous
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>Task&lt;System.Boolean&gt;</returns>
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Find matching entity asynchronous
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>Task&lt;TEntity&gt;</returns>
Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Find first matching entity asynchronous
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>Task&lt;TEntity&gt;</returns>
Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Get first entity matching query or default entity asynchronous
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>Task&lt;TEntity&gt;</returns>
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Add an entity
/// </summary>
/// <param name="entity">The entity.</param>
void Add(TEntity entity);
/// <summary>
/// Attach the entity
/// </summary>
/// <param name="entity">The entity.</param>
void Attach(TEntity entity);
/// <summary>
/// Delete the entity
/// </summary>
/// <param name="entity">The entity.</param>
void Delete(TEntity entity);
/// <summary>
/// Anies the specified predicate.
/// </summary>
/// <param name="predicate">The predicate.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
bool Any(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Get all entities
/// </summary>
/// <returns>IQueryable&lt;TEntity&gt;</returns>
IQueryable<TEntity> All();
/// <summary>
/// Find all matching entities matching query
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>IQueryable&lt;TEntity&gt;</returns>
IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Find first entity matching query
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>TEntity</returns>
TEntity First(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Find first matching entity or default entity
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>TEntity</returns>
TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Get entity by id
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>TEntity</returns>
TEntity GetById(string id);
}
}

356
Mogrifiers.cs Normal file
View file

@ -0,0 +1,356 @@
// ***********************************************************************
// Assembly : FCS.Lib
// Author : FH
// Created : 27-08-2016
//
// Last Modified By : Frede H.
// Last Modified On : 2020-08-30
// ***********************************************************************
// <copyright file="Mogrifiers.cs" company="FCS">
// Copyright © FCS 2015-2020
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Text;
namespace FCS.Lib
{
/// <summary>
/// Class Converters
/// </summary>
public static class Mogrifiers
{
/// <summary>
/// Reverse boolean
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
public static bool BoolReverse(bool value)
{
return !value;
}
/// <summary>
/// Boolean to integer
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <returns>System.Int32.</returns>
public static int BoolToInt(bool value)
{
return value ? 1 : 0;
}
/// <summary>
/// Boolean to string
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <returns>System.String.</returns>
public static string BoolToString(bool value)
{
return value ? "true" : "false";
}
/// <summary>
/// Enum to integer
/// </summary>
/// <param name="enumeration">The enumeration.</param>
/// <returns>System.Int32.</returns>
public static int EnumToInt(object enumeration)
{
return Convert.ToInt32(enumeration, CultureInfo.InvariantCulture);
}
/// <summary>
/// Enum to string.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>System.String.</returns>
public static string EnumToString(Enum value)
{
return value == null ? string.Empty : value.ToString();
}
/// <summary>
/// Integer to boolean.
/// </summary>
/// <param name="value">The value.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
public static bool IntToBool(int value)
{
return value == 1;
}
/// <summary>
/// Integer to enum.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <returns>T.</returns>
public static T IntToEnum<T>(int value)
{
return (T) Enum.ToObject(typeof(T), value);
}
/// <summary>
/// Integer to letter.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>System.String.</returns>
public static string IntToLetter(int value)
{
var empty = string.Empty;
var num = 97;
var str = "";
var num1 = 0;
var num2 = 97;
for (var i = 0; i <= value; i++)
{
num1++;
empty = string.Concat(str, Convert.ToString(Convert.ToChar(num), CultureInfo.InvariantCulture));
num++;
if (num1 != 26) continue;
num1 = 0;
str = Convert.ToChar(num2).ToString(CultureInfo.InvariantCulture);
num2++;
num = 97;
}
return empty;
}
/// <summary>
/// Lists to string using semicolon(;)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">The list.</param>
/// <returns>System.String.</returns>
public static string ListToString<T>(List<T> list)
{
return ListToString(list, ";");
}
/// <summary>
/// Lists to string userdefined delimiter
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">The list.</param>
/// <param name="delimiter">The delimiter.</param>
/// <returns>System.String.</returns>
public static string ListToString<T>(List<T> list, string delimiter)
{
var empty = string.Empty;
if (list == null) return empty;
var enumerator = (IEnumerator) list.GetType().GetMethod("GetEnumerator")?.Invoke(list, null);
while (enumerator != null && enumerator.MoveNext())
if (enumerator.Current != null)
empty = string.Concat(empty, enumerator.Current.ToString(), delimiter);
return empty;
}
/// <summary>
/// String to bool.
/// </summary>
/// <param name="value">The value.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
[SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "<Pending>")]
public static bool StringToBool(string value)
{
if (string.IsNullOrEmpty(value))
return false;
var flag = false;
var upper = value.ToUpperInvariant();
if (string.Compare(upper, "true", StringComparison.OrdinalIgnoreCase) == 0)
{
flag = true;
}
else if (string.CompareOrdinal(upper, "false") == 0)
{
}
else if (string.CompareOrdinal(upper, "1") == 0)
{
flag = true;
}
return flag;
}
/// <summary>
/// String to decimal.
/// </summary>
/// <param name="inString">The in string.</param>
/// <returns>System.Nullable&lt;System.Decimal&gt;.</returns>
public static decimal? StringToDecimal(string inString)
{
if (string.IsNullOrEmpty(inString)) return 0;
return
!decimal.TryParse(inString.Replace(",", "").Replace(".", ""), NumberStyles.Number,
CultureInfo.InvariantCulture, out var num)
? (decimal?) null
: decimal.Divide(num, new decimal((long) 100));
}
/// <summary>
/// String to enum.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <returns>T.</returns>
public static T StringToEnum<T>(string value)
{
return (T) Enum.Parse(typeof(T), value, true);
}
/// <summary>
/// String to list using semicolon(;).
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <returns>List&lt;T&gt;.</returns>
public static List<T> StringToList<T>(string value)
{
return StringToList<T>(value, ";");
}
/// <summary>
/// String to list userdefined delimiter.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <param name="delimiter">The delimiter.</param>
/// <returns>List&lt;T&gt;.</returns>
/// <exception cref="System.ArgumentNullException">value</exception>
/// <exception cref="System.ArgumentNullException">delimiter</exception>
/// <exception cref="ArgumentNullException">value</exception>
/// <exception cref="ArgumentNullException">delimiter</exception>
public static List<T> StringToList<T>(string value, string delimiter)
{
if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value));
if (string.IsNullOrEmpty(delimiter)) throw new ArgumentNullException(nameof(delimiter));
var ts = new List<T>();
var strArrays = value.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var str in strArrays)
{
var o = typeof(T).FullName;
if (o == null) continue;
var upperInvariant = o.ToUpperInvariant();
if (string.CompareOrdinal(upperInvariant, "system.string") == 0)
{
ts.Add((T) Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture));
}
else if (string.CompareOrdinal(upperInvariant, "system.int32") == 0)
{
ts.Add((T) Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture));
}
else if (string.CompareOrdinal(upperInvariant, "system.guid") == 0)
{
var guid = new Guid(str);
ts.Add((T) Convert.ChangeType(guid, typeof(T), CultureInfo.InvariantCulture));
}
}
return ts;
}
/// <summary>
/// String to stream using system default encoding.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>Stream.</returns>
public static Stream StringToStream(string value)
{
return StringToStream(value, Encoding.Default);
}
/// <summary>
/// Strings to stream with userdefined encoding.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="encoding">The encoding.</param>
/// <returns>Stream.</returns>
public static Stream StringToStream(string value, Encoding encoding)
{
return encoding == null ? null : new MemoryStream(encoding.GetBytes(value ?? ""));
}
/// <summary>
/// Returns timestamp for current date-time object.
/// </summary>
/// <returns>System.Int32.</returns>
public static long CurrentDateTimeToTimeStamp()
{
return Convert.ToUInt32(DateTimeToTimeStamp(DateTime.Now));
}
/// <summary>
/// Convert a DateTime object to timestamp
/// </summary>
/// <param name="dateTime">The date time.</param>
/// <returns>System.Int64.</returns>
public static long DateTimeToTimeStamp(DateTime dateTime)
{
var bigDate = new DateTime(2038, 1, 19, 0, 0, 0, 0);
var nixDate = new DateTime(1970, 1, 1, 0, 0, 0, 0);
if (dateTime >= bigDate)
return Convert.ToUInt32((bigDate - nixDate).TotalSeconds) +
Convert.ToUInt32((dateTime - bigDate).TotalSeconds);
return Convert.ToUInt32((dateTime - nixDate).TotalSeconds);
}
/// <summary>
/// Convert timestamp to DataTime format
/// </summary>
/// <param name="timeStamp">The time stamp.</param>
/// <returns>DateTime.</returns>
public static DateTime TimeStampToDateTime(long timeStamp)
{
var nixDate = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return nixDate.AddSeconds(timeStamp);
}
/// <summary>
/// Convert timespan to seconds
/// </summary>
/// <param name="timespan">The timespan.</param>
/// <returns>System.Int32.</returns>
public static long TimeSpanToSeconds(TimeSpan timespan)
{
return Convert.ToUInt32(timespan.Ticks / 10000000L);
}
/// <summary>
/// Converts seconds to timespan
/// </summary>
/// <param name="seconds">The seconds.</param>
/// <returns>TimeSpan.</returns>
public static TimeSpan SecondsToTimeSpan(long seconds)
{
return TimeSpan.FromTicks(10000000L * seconds);
}
/// <summary>
/// Converts timespan to minutes
/// </summary>
/// <param name="timespan">The timespan.</param>
/// <returns>System.Int32.</returns>
public static long TimespanToMinutes(TimeSpan timespan)
{
return Convert.ToUInt32(timespan.Ticks / 10000000L) / 60;
}
}
}

View file

@ -0,0 +1,20 @@
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("FCSInno")]
[assembly: AssemblyDescription("FCSInno")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Frede Hundewadt")]
[assembly: AssemblyProduct("FCSInno")]
[assembly: AssemblyCopyright("Copyright © FCS 2015-2020")]
[assembly: AssemblyTrademark("FCS © Innotec Danmark A/S")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("aaf08873-14e5-411d-8ec8-629782ac8f03")]
[assembly: AssemblyVersion("2.1.20289.1156")]
[assembly: AssemblyFileVersion("2.1.20289.1156")]

View file

@ -0,0 +1,41 @@
<#@ template debug="true" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#
int major = 2;
int minor = 1; // Official == even, Preview == odd
int build = 1;
int revision = 0;
string version = String.Format("{0}.{1}.{2}.{3}", major, minor, build, revision);
if (minor % 2 == 1) // Preview build
{
// Auto-generate the build and revision
DateTime buildTime = DateTime.Now;
build = (1000 * (buildTime.Year % 100)) + buildTime.DayOfYear;
revision = (100 * buildTime.Hour) + buildTime.Minute;
version = String.Format("{0}.{1}.{2:00000}.{3:0000}", major, minor, build, revision);
}
#>
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("FCSInno")]
[assembly: AssemblyDescription("FCSInno")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Frede Hundewadt")]
[assembly: AssemblyProduct("FCSInno")]
[assembly: AssemblyCopyright("Copyright © FCS 2015-2020")]
[assembly: AssemblyTrademark("FCS © Innotec Danmark A/S")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("aaf08873-14e5-411d-8ec8-629782ac8f03")]
[assembly: AssemblyVersion("<#= version #>")]
[assembly: AssemblyFileVersion("<#= version #>")]

382
Squid.cs Normal file
View file

@ -0,0 +1,382 @@
// ***********************************************************************
// Assembly : FCS.Lib
// Author : FH
// Created : 2020-07-01
//
// Last Modified By : FH
// Last Modified On : 2020-08-30
// ***********************************************************************
// <copyright file="Squid.cs" company="Frede Hundewadt">
// Copyright © FCS 2015-2020
// </copyright>
// <summary>Derived from https:github.com/csharpvitamins/CSharpVitamins.ShortGuid</summary>
// ***********************************************************************
using System;
using System.Diagnostics;
namespace FCS.Lib
{
/// <summary>
/// A wrapper for handling URL-safe Base64 encoded globally unique identifiers (GUID).
/// </summary>
/// <remarks>Special characters are replaced (/, +) or removed (==).</remarks>
[DebuggerDisplay("{" + nameof(Value) + "}")]
public readonly struct Squid : IEquatable<Squid>
{
/// <summary>
/// A read-only object of the Squid struct.
/// Value is guaranteed to be all zeroes.
/// Equivalent to <see cref="Guid.Empty" />.
/// </summary>
public static readonly Squid Empty = new Squid(Guid.Empty);
/// <summary>
/// Creates a new Squid from a Squid encoded string.
/// </summary>
/// <param name="value">A valid Squid encodd string.</param>
public Squid(string value)
{
Value = value;
Guid = Decode(value);
}
/// <summary>
/// Creates a new Squid with the given <see cref="System.Guid" />.
/// </summary>
/// <param name="obj">A valid System.Guid object.</param>
public Squid(Guid obj)
{
Value = Encode(obj);
Guid = obj;
}
/// <summary>
/// Gets the underlying <see cref="System.Guid" /> for the encoded Squid.
/// </summary>
/// <value>The unique identifier.</value>
#pragma warning disable CA1720 // Identifier contains type name
public Guid Guid { get; }
#pragma warning restore CA1720 // Identifier contains type name
/// <summary>
/// The encoded string value of the <see cref="Guid" />
/// as an URL-safe Base64 string.
/// </summary>
/// <value>The value.</value>
public string Value { get; }
/// <summary>
/// Returns the encoded URL-safe Base64 string.
/// </summary>
/// <returns>A <see cref="string" /> that represents this instance.</returns>
public override string ToString()
{
return Value;
}
/// <summary>
/// Returns a value indicating whether this object and a specified object represent the same type and value.
/// Compares for equality against other string, Guid and Squid types.
/// </summary>
/// <param name="obj">A Systerm.String, System.Guid or Squid object</param>
/// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
public override bool Equals(object obj)
{
return obj is Squid other && Equals(other);
}
/// <summary>
/// Equality comparison
/// </summary>
/// <param name="obj">A valid Squid object</param>
/// <returns>A boolean indicating equality.</returns>
public bool Equals(Squid obj)
{
return Guid.Equals(obj.Guid) && Value == obj.Value;
}
/// <summary>
/// Returns the hash code for the underlying <see cref="System.Guid" />.
/// </summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode()
{
unchecked
{
return (Guid.GetHashCode() * 397) ^ (Value != null ? Value.GetHashCode() : 0);
}
}
/// <summary>
/// Initialises a new object of the Squid using <see cref="Guid.NewGuid()" />.
/// </summary>
/// <returns>New Squid object</returns>
public static Squid NewGuid()
{
return new Squid(Guid.NewGuid());
}
/// <summary>
/// Encode string as a new Squid encoded string.
/// The encoding is similar to Base64 with
/// non-URL safe characters replaced, and padding removed.
/// </summary>
/// <param name="value">A valid <see cref="System.Guid" />.Tostring().</param>
/// <returns>A 22 character URL-safe Base64 string.</returns>
public static string Encode(string value)
{
var guid = new Guid(value);
return Encode(guid);
}
/// <summary>
/// Encode a <see cref="System.Guid" /> object to Squid.
/// The encoding is similar to Base64 with
/// non-URL safe characters replaced, and padding removed.
/// </summary>
/// <param name="obj">A valid <see cref="System.Guid" /> object.</param>
/// <returns>A 22 character URL-safe Base64 string.</returns>
public static string Encode(Guid obj)
{
var encoded = Convert.ToBase64String(obj.ToByteArray());
encoded = encoded
.Replace("/", "_")
.Replace("+", "-");
return encoded.Substring(0, 22);
}
/// <summary>
/// Decode Squid string to a <see cref="System.Guid" />.
/// See also <seealso cref="TryDecode(string, out System.Guid)" /> or
/// <seealso cref="TryParse(string, out System.Guid)" />.
/// </summary>
/// <param name="value">A valid Squid encoded string.</param>
/// <returns>A new <see cref="System.Guid" /> object from the parsed string.</returns>
public static Guid Decode(string value)
{
if (value == null) return Empty;
value = value
.Replace("_", "/")
.Replace("-", "+");
var blob = Convert.FromBase64String(value + "==");
return new Guid(blob);
}
/// <summary>
/// Squid to Guid.
/// </summary>
/// <param name="obj">A valid Squid object.</param>
/// <returns>System.Guid object.</returns>
public static Guid FromSquid(Squid obj)
{
return obj.Guid;
}
/// <summary>
/// String to Squid.
/// </summary>
/// <param name="value">String value to convert</param>
/// <returns>A Squid object.</returns>
public static Squid FromString(string value)
{
if (string.IsNullOrEmpty(value))
return Empty;
return TryParse(value, out Squid obj) ? obj : Empty;
}
/// <summary>
/// Decodes the given value to a <see cref="System.Guid" />.
/// </summary>
/// <param name="value">The Squid encoded string to decode.</param>
/// <param name="obj">A new <see cref="System.Guid" /> object from the parsed string.</param>
/// <returns>A boolean indicating if the decode was successful.</returns>
public static bool TryDecode(string value, out Guid obj)
{
try
{
// Decode as Squid
obj = Decode(value);
return true;
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception)
#pragma warning restore CA1031 // Do not catch general exception types
{
// Return empty Guid
obj = Guid.Empty;
return false;
}
}
/// <summary>
/// Tries to parse the given string value and
/// outputs the <see cref="Squid" /> object.
/// </summary>
/// <param name="value">The Squid encoded string or string representation of a Guid.</param>
/// <param name="obj">A new <see cref="Squid" /> object from the parsed string.</param>
/// <returns>A boolean indicating if the parse was successful.</returns>
public static bool TryParse(string value, out Squid obj)
{
// Parse as Squid string.
if (TryDecode(value, out var oGuid))
{
obj = oGuid;
return true;
}
// Parse as Guid string.
if (Guid.TryParse(value, out oGuid))
{
obj = oGuid;
return true;
}
obj = Empty;
return false;
}
/// <summary>
/// Tries to parse the string value and
/// outputs the underlying <see cref="System.Guid" /> object.
/// </summary>
/// <param name="value">The Squid encoded string or string representation of a Guid.</param>
/// <param name="obj">A new <see cref="System.Guid" /> object from the parsed string.</param>
/// <returns>A boolean indicating if the parse was successful.</returns>
public static bool TryParse(string value, out Guid obj)
{
// Try a Squid string.
if (TryDecode(value, out obj))
return true;
// Try a Guid string.
if (Guid.TryParse(value, out obj))
return true;
obj = Guid.Empty;
return false;
}
#region Operators
/// <summary>
/// Determines if both Squid objects have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Squid x, Squid y)
{
return x.Guid == y.Guid;
}
/// <summary>
/// Determines if both objects have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Squid x, Guid y)
{
return x.Guid == y;
}
/// <summary>
/// Determines if both objects have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Guid x, Squid y)
{
return y == x; // NB: order of arguments
}
/// <summary>
/// Determines if both Squid objects do not have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(Squid x, Squid y)
{
return !(x == y);
}
/// <summary>
/// Determines if both objects do not have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(Squid x, Guid y)
{
return !(x == y);
}
/// <summary>
/// Determines if both objects do not have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(Guid x, Squid y)
{
return !(x == y);
}
/// <summary>
/// Implicitly converts the Squid to
/// its string equivalent.
/// </summary>
/// <param name="oSquid">The o squid.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator string(Squid oSquid)
{
return oSquid.Value;
}
/// <summary>
/// Implicitly converts the Squid to
/// its <see cref="System.Guid" /> equivalent.
/// </summary>
/// <param name="oSquid">The o squid.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Guid(Squid oSquid)
{
return oSquid.Guid;
}
/// <summary>
/// Implicitly converts the string to a Squid.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Squid(string value)
{
if (string.IsNullOrEmpty(value))
return Empty;
return TryParse(value, out Squid oSquid) ? oSquid : Empty;
}
/// <summary>
/// Implicitly converts the <see cref="System.Guid" /> to a Squid.
/// </summary>
/// <param name="oGuid">The o unique identifier.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Squid(Guid oGuid)
{
return oGuid == Guid.Empty ? Empty : new Squid(oGuid);
}
#endregion
}
}

57
StringOptions.cs Normal file
View file

@ -0,0 +1,57 @@
// ***********************************************************************
// Assembly : FCS.Lib
// Author : FH
// Created : 2020-09-09
//
// Last Modified By : FH
// Last Modified On : 2020-08-30
// ***********************************************************************
// <copyright file="PasswordOptions.cs" company="Frede Hundewadt">
// Copyright © FCS 2015-2020
// </copyright>
// <summary></summary>
// ***********************************************************************
namespace FCS.Lib
{
/// <summary>
/// Class PasswordOptions.
/// </summary>
public class StringOptions
{
/// <summary>
/// Gets or sets the length of the required.
/// </summary>
/// <value>The length of the required.</value>
public int RequiredLength { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [require non letter or digit].
/// </summary>
/// <value><c>true</c> if [require non letter or digit]; otherwise, <c>false</c>.</value>
public bool RequireNonLetterOrDigit { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [require digit].
/// </summary>
/// <value><c>true</c> if [require digit]; otherwise, <c>false</c>.</value>
public bool RequireDigit { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [require lowercase].
/// </summary>
/// <value><c>true</c> if [require lowercase]; otherwise, <c>false</c>.</value>
public bool RequireLowercase { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [require uppercase].
/// </summary>
/// <value><c>true</c> if [require uppercase]; otherwise, <c>false</c>.</value>
public bool RequireUppercase { get; set; }
/// <summary>
/// Gets or sets the required unique chars.
/// </summary>
/// <value>The required unique chars.</value>
public int RequiredUniqueChars { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [require non alphanumeric].
/// </summary>
/// <value><c>true</c> if [require non alphanumeric]; otherwise, <c>false</c>.</value>
public bool RequireNonAlphanumeric { get; set; }
}
}