Skip to content

Latest commit

 

History

History
1642 lines (980 loc) · 76.1 KB

File metadata and controls

1642 lines (980 loc) · 76.1 KB

AndcultureCode.CSharp.Extensions

Contents

ApiClaimTypes type

Namespace

AndcultureCode.CSharp.Core.Constants

Summary

Commonly used Claim types for APIs

TODO: Migrate to AndcultureCode.CSharp.Core

IS_SUPER_ADMIN constants

Summary

Is the current user elevated to super admin

ROLE_ID constants

Summary

Active Role Id

ROLE_IDS constants

Summary

Available Role Ids

ROLE_TYPE constants

Summary

Active Role Type

USER_ID constants

Summary

Current User Id

USER_LOGIN_ID constants

Summary

Current User Login Id

AssemblyExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

Extensions for Assembly

GetLoadableTypes(assembly) method

Summary

Safely returns types that can be loaded from the given assembly

Returns
Parameters
Name Type Description
assembly System.Reflection.Assembly

ClaimsPrincipalExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

Extension methods for ClaimsPrincipal

IsAuthenticated(principal) method

Summary

Whether the current user is authenticated

Returns
Parameters
Name Type Description
principal System.Security.Claims.ClaimsPrincipal

IsSuperAdmin(principal) method

Summary

Retrieves whether the user is a super admin by way of their identity claims

Returns
Parameters
Name Type Description
principal System.Security.Claims.ClaimsPrincipal

IsUnauthenticated(principal) method

Summary

Whether the current user is unauthenticated

Returns
Parameters
Name Type Description
principal System.Security.Claims.ClaimsPrincipal

RoleId(principal) method

Summary

Retrieves user's current role id by way of their identity claims

Returns
Parameters
Name Type Description
principal System.Security.Claims.ClaimsPrincipal

RoleIds(principal) method

Summary

Retrieves user's role ids by way of their identity claims

Returns
Parameters
Name Type Description
principal System.Security.Claims.ClaimsPrincipal

UserId(principal) method

Summary

Retrieves user's id by way of identity claims

Returns
Parameters
Name Type Description
principal System.Security.Claims.ClaimsPrincipal

UserLoginId(principal) method

Summary

Retrieves user's UserLoginId from identity claims.

Returns
Parameters
Name Type Description
principal System.Security.Claims.ClaimsPrincipal

DateExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

DateTime/Offset Extensions

AtEndOfDay(date) method

Summary

Sets and returns the time to 11:59:59 on the given DateTimeOffset.

Returns
Parameters
Name Type Description
date System.DateTimeOffset

AtMidnight(date) method

Summary

Useful when you only care about the date, but do not want to lose the offset. Returns the midnight representation of the given DateTimeOffset.

Returns
Parameters
Name Type Description
date System.DateTimeOffset

CalculateAge() method

Summary

Convenience method to calculate an age from a birthdate

Parameters

This method has no parameters.

IsBetweenDates() method

Summary

Convenience method to default the inclusive parameter LINQ doesn't like optional parameters on methods used in expressions

Parameters

This method has no parameters.

IsBetweenDates() method

Summary

Provides filtering method for date ranges (excluding time portions)

Parameters

This method has no parameters.

SetTime(date,hour,minute,second) method

Summary

Sets the hour, minute, and second on the given DateTimeOffset with the supplied values.

Returns
Parameters
Name Type Description
date System.DateTimeOffset
hour System.Int32
minute System.Int32
second System.Int32

SubtractWeekdays() method

Summary

Convenience method to subtract weekdays

Parameters

This method has no parameters.

SubtractWeekdays() method

Summary

Convenience method to subtract weekdays

Parameters

This method has no parameters.

DictionaryExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

Extension methods for Dictionary

Merge``2(left,right,takeLastKey) method

Summary

'Merges' two dictionaries into one. If duplicate keys are encountered, either the first or last occurrence will be used. See 'takeLastKey'

Returns
Parameters
Name Type Description
left System.Collections.Generic.Dictionary{``0,``1} Dictionary to be merged into.
right System.Collections.Generic.Dictionary{``0,``1} Dictionary to be merged into the left dictionary.
takeLastKey System.Boolean Determines whether the value of the last occurrence of a key is used as the final value
when duplicates are encountered. If false, uses the value of the first occurrence.
Generic Types
Name Description
TKey
TValue

ExpressionExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

Expression extension methods

AndAlso``1(expr1,expr2) method

Summary

Adds another expression filter to original expression using AndAlso operator

Returns
Parameters
Name Type Description
expr1 System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}} Main filter expression
expr2 System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}} Additional filter expression on the same type of object as the main expression
Generic Types
Name Description
T Type of object in the main filter

AndAlso``2(expr1,expr2,navigationProperty) method

Summary

Adds another expression filter to original expression using AndAlso operator

Returns
Parameters
Name Type Description
expr1 System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}} Main filter expression
expr2 System.Linq.Expressions.Expression{System.Func{``1,System.Boolean}} Filter expression that filters on the navigated property
navigationProperty System.Linq.Expressions.Expression{System.Func{``0,``1}} Expression to the navigation property (eg e => e.PropA.PropB)
Generic Types
Name Description
T Type of object in the main filter
TNav Type of the navigation property (can be deeply nested)

OrElse``1(expr1,expr2) method

Summary

Adds another expression filter to original expression using OrElse operator

Returns
Parameters
Name Type Description
expr1 System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}} Main filter expression
expr2 System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}} Additional filter expression on the same type of object as the main expression
Generic Types
Name Description
T Type of object in the main filter

OrElse``2(expr1,expr2,navigationProperty) method

Summary

Adds another expression filter to original expression using OrElse operator

Returns
Parameters
Name Type Description
expr1 System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}} Main filter expression
expr2 System.Linq.Expressions.Expression{System.Func{``1,System.Boolean}} Filter expression that filters on the navigated property
navigationProperty System.Linq.Expressions.Expression{System.Func{``0,``1}} Expression to the navigation property (eg e => e.PropA.PropB)
Generic Types
Name Description
T Type of object in the main filter
TNav Type of the navigation property (can be deeply nested)

Or``1(expr1,expr2) method

Summary

Adds another expression filter to original expression using Or operator

Returns
Parameters
Name Type Description
expr1 System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}} Main filter expression
expr2 System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}} Additional filter expression on the same type of object as the main expression
Generic Types
Name Description
T Type of object in the main filter

Or``2(expr1,expr2,navigationProperty) method

Summary

Adds another expression filter to original expression using Or operator

Returns
Parameters
Name Type Description
expr1 System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}} Main filter expression
expr2 System.Linq.Expressions.Expression{System.Func{``1,System.Boolean}} Filter expression that filters on the navigated property
navigationProperty System.Linq.Expressions.Expression{System.Func{``0,``1}} Expression to the navigation property (eg e => e.PropA.PropB)
Generic Types
Name Description
T Type of object in the main filter
TNav Type of the navigation property (can be deeply nested)

HttpRequestExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

Extension methods for HttpRequest

X_FORWARDED_FOR constants

Summary

Standard X-Header for forwarding IP addresses in varying infrastructures

GetCookie(request,name) method

Summary

Returns the specified cookie by name

If no cookie is found, returns null

Returns
Parameters
Name Type Description
request Microsoft.AspNetCore.Http.HttpRequest The request to pull the cookie from
name System.String The name of the cookie to be returned

GetHeader(request,name) method

Summary

Attempts to retrieve requested header value

Parameters
Name Type Description
request Microsoft.AspNetCore.Http.HttpRequest
name System.String Header name/key

GetIpAddress(request) method

Summary

Retrieves the client's forwarded IP address, if present. Returns null otherwise. Ensure you have 'AddForwardedHeaders' enabled in startup.

Returns
Parameters
Name Type Description
request Microsoft.AspNetCore.Http.HttpRequest

GetUserAgent() method

Summary

Requesting user's agent

Parameters

This method has no parameters.

HasCookie(request,name) method

Summary

Returns whether or not the specified cookie is found in the request

If the cookie is found but its value is null/whitespace, it will return false.

Returns
Parameters
Name Type Description
request Microsoft.AspNetCore.Http.HttpRequest The request to check for the cookie
name System.String The name of the cookie to check for

HttpResponseMessageExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

HttpResponseMessage extension methods

FromJson``1(response) method

Summary

Deserializes http response into supplied object

Returns
Parameters
Name Type Description
response System.Net.Http.HttpResponseMessage
Generic Types
Name Description
T

IConfigurationRootExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

Extension methods for IConfigurationRoot

CONFIGURATION_VERSION_DEVELOPMENT_VALUE constants

Summary

Version value when application is run in development environment

CONFIGURATION_VERSION_KEY constants

Summary

Key value for build version number

CONFIGURATION_VERSION_TEMPLATE constants

Summary

Template string replaced with current version number

DEFAULT_DATABASE_KEY constants

Summary

Default connection string database key

GetDatabaseConnectionString(configuration,databaseKey) method

Summary

Retrieves web application's primary database connection string

Parameters
Name Type Description
configuration Microsoft.Extensions.Configuration.IConfigurationRoot
databaseKey System.String

GetDatabaseName(configuration) method

Summary

Retrieves the database name of the primary database connection string

Parameters
Name Type Description
configuration Microsoft.Extensions.Configuration.IConfigurationRoot

GetVersion(configuration,isDevelopment) method

Summary

Loads and conditionally updates the Version number based upon environment

Returns
Parameters
Name Type Description
configuration Microsoft.Extensions.Configuration.IConfigurationRoot
isDevelopment System.Boolean

IEnumerableExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

IEnumerable extension methods

Except``1() method

Summary

Returns items in source collection that do not exist in exclusion collection based on predicate

Parameters

This method has no parameters.

HasValues``1() method

Summary

Determines if the source collection is non-null and has values

Parameters

This method has no parameters.

HasValues``1() method

Summary

Determines if the source collection is non-null and has values

Parameters

This method has no parameters.

Intersect``1() method

Summary

Returns items in source collection that exist in inclusion collection based on the predicate

Parameters

This method has no parameters.

IsEmpty``1() method

Summary

Determines if the source list is empty

Parameters

This method has no parameters.

IsEmpty``1() method

Summary

Determines if the source list is empty

Parameters

This method has no parameters.

IsNullOrEmpty``1() method

Summary

Determines if the source list is null or empty

Parameters

This method has no parameters.

IsNullOrEmpty``1() method

Summary

Determines if the source list is null or empty

Parameters

This method has no parameters.

Join() method

Summary

Convenience method so joining strings reads better :)

Parameters

This method has no parameters.

Join() method

Summary

Convenience method for joining dictionary key values into a string

Parameters

This method has no parameters.

Join() method

Summary

Convenience method so joining a list of strings

Parameters

This method has no parameters.

Join() method

Summary

Convenience method for joining key value pairs

Parameters

This method has no parameters.

PickRandom``1() method

Summary

Returns a random value in the related IEnumerable list

Parameters

This method has no parameters.

PickRandom``1() method

Summary

Returns X number of random values in the related IEnumerable list

Parameters

This method has no parameters.

Shuffle``1() method

Summary

Returns source enumerable in randomized order

Parameters

This method has no parameters.

IQueryableExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

IQueryable extension methods

AppendOrderByExpression``1(query,propertyName,direction,isAdditionalOrderBy) method

Summary

Adds an additional sorting expression to the supplied IQueryable.

Returns
Parameters
Name Type Description
query System.Linq.IQueryable{``0} The IQueryable to add additional sorting to.
propertyName System.String The path to the property to order by (e.g. Path.To.Property).
direction AndcultureCode.CSharp.Extensions.Enumerations.OrderByDirection Order by which to sort items.
isAdditionalOrderBy System.Boolean Whether the order by is an addition to an existing OrderBy statement.
Generic Types
Name Description
T

HasValues``1() method

Summary

Determines if the source collection is non-null and has values

Parameters

This method has no parameters.

HasValues``1() method

Summary

Determines if the source collection is non-null and has values

Parameters

This method has no parameters.

IsEmpty``1() method

Summary

Determines if the source list is empty

Parameters

This method has no parameters.

IsEmpty``1() method

Summary

Determines if the source list is empty (based on the given predicate)

Parameters

This method has no parameters.

IsNullOrEmpty``1() method

Summary

Determines if the source list is null or empty

Parameters

This method has no parameters.

IsNullOrEmpty``1() method

Summary

Determines if the source list is null or empty (based on the given predicate)

Parameters

This method has no parameters.

OrderBy``1(query,propertyName,direction) method

Summary

Order by the string expression provided.

Returns
Parameters
Name Type Description
query System.Linq.IQueryable{``0} The IOrderedQueryable to add additional sorting to.
propertyName System.String The path to the property to order by (e.g. Path.To.Property).
direction AndcultureCode.CSharp.Extensions.Enumerations.OrderByDirection Order by which to sort items.
Generic Types
Name Description
T

PickRandom``1(source) method

Summary

Returns a random value in the related IQueryable list

Parameters
Name Type Description
source System.Linq.IQueryable{``0}
Generic Types
Name Description
T

PickRandom``1() method

Summary

Returns X number of random values in the related IQueryable list

Parameters

This method has no parameters.

Shuffle``1() method

Summary

Returns source enumerable in randomized order

Parameters

This method has no parameters.

ThenBy``1(query,propertyName,direction) method

Summary

Order then by the string expression provided.

Returns
Parameters
Name Type Description
query System.Linq.IOrderedQueryable{``0} The IOrderedQueryable to add additional sorting to.
propertyName System.String The path to the property to order by (e.g. Path.To.Property).
direction AndcultureCode.CSharp.Extensions.Enumerations.OrderByDirection Order by which to sort items.
Generic Types
Name Description
T

LambdaComparer`1 type

Namespace

AndcultureCode.CSharp.Extensions.Models

Summary

Generic comparator class for ease of use with comparison functions

#ctor(predicate) constructor

Summary

Constructor for new comparator based on predicate

Parameters
Name Type Description
predicate System.Func{`0,`0,System.Boolean} Predicate to be used for equality comparison

Equals() method

Summary

Inherit from parent.

Parameters

This method has no parameters.

GetHashCode() method

Summary

Inherit from parent.

Parameters

This method has no parameters.

OrderByDirection type

Namespace

AndcultureCode.CSharp.Extensions.Enumerations

Summary

Directions for ordering items.

Ascending constants

Summary

Sorted in ascending order.

Descending constants

Summary

Sorted in descending order.

StringExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

String extension methods

AsIndentedJson(str) method

Summary

Formats string for pretty printing of a JSON string

Returns
Parameters
Name Type Description
str System.String

IsInvalidHttpUrl(source) method

Summary

Determines if the supplied string is not a valid HTTP or HTTPS Url

Uses the native Uri class

Returns
Parameters
Name Type Description
source System.String

IsNotValidGuid(guidString) method

Summary

Determine if supplied string is not a valid Guid

Returns
Parameters
Name Type Description
guidString System.String

IsValidEmail(email) method

Summary

Determines if the supplied string is an email address

Parameters
Name Type Description
email System.String

IsValidGuid(guidString) method

Summary

Determine if supplied string is a valid Guid

Returns
Parameters
Name Type Description
guidString System.String

IsValidHttpUrl(source) method

Summary

Determines if the supplied string is a valid HTTP or HTTPS url

Uses the native Uri class

Parameters
Name Type Description
source System.String

ToBoolean() method

Summary

Converts a string representation of a boolean into an actual boolean

Parameters

This method has no parameters.

ToEnumerable``1(input,separator) method

Summary

Converts a string of ints into an enumerable

Returns
Parameters
Name Type Description
input System.String
separator System.Char

ToInt(number,defaultValue) method

Summary

Convert a string to integer, otherwise default the value

Returns

Converted string as an integer value

Parameters
Name Type Description
number System.String String that should be a number
defaultValue System.Int32 Default value used if the number cannot be parse (0 is default)

TryChangeType(value,conversionType) method

Summary

Returns true or false if the value can be converted

Returns
Parameters
Name Type Description
value System.Object
conversionType System.Type

TypeExtensions type

Namespace

AndcultureCode.CSharp.Extensions

Summary

Extensions for Type

GetPublicConstantValues``1() method

Summary

Retrieve all constant values for given type whose value matches type T

Parameters

This method has no parameters.

GetPublicPropertyInfo(type,propertyName) method

Summary

Get the PropertyInfo for specified property, if it exists on the type and is public, otherwise returns null.

Returns

The PropertyInfo for the property, if it exists and is public, null otherwise.

Parameters
Name Type Description
type System.Type
propertyName System.String

GetPublicPropertyValue(src,propertyName) method

Summary

Get the value of a property specified by propertyName, if it exists on the object and is public. If no such public property exists, returns null.

Returns

The property value, if it exists and is public, null otherwise.

Parameters
Name Type Description
src System.Object
propertyName System.String

GetPublicPropertyValue``1(src,propertyName) method

Summary

Get the value of a property specified by propertyName, if it exists on the object and is public, casted to type T. If no such property exists, returns null.

Returns

The property value, if it exists, null otherwise.

Parameters
Name Type Description
src System.Object
propertyName System.String

GetTypeName(obj) method

Summary

Returns the name the underlying type of any object

Returns
Parameters
Name Type Description
obj System.Object

GetTypeName(type) method

Summary

Returns the full name of the type as well as the assembly qualified name

Returns
Parameters
Name Type Description
type System.Type

HasPublicProperty(type,propertyName) method

Summary

Checks whether a property specified by propertyName exists on the specified type.

Returns

true if type has property with specified name, false otherwise

Parameters
Name Type Description
type System.Type
propertyName System.String

WhereWithAttribute``1() method

Summary

Filters the provided list of types to only those that are decorated with the TAttribute attribute at the class level.

Parameters

This method has no parameters.

WhereWithoutAttribute``1() method

Summary

Filters the provided list of types to only those that aren't decorated with the TAttribute attribute at the class level.

Parameters

This method has no parameters.