Structr
  • Welcome
  • Utilities
    • Abstractions
      • Check
      • Ensure
      • Money
      • HierarchyId
      • Providers
        • SequentialGuidProvider
      • Extensions
        • DateTime
        • Dictionary
        • DirectoryInfo
        • Enumerable
        • Enum
        • Expression
        • Int
        • Long
        • MemberInfo
        • Object
        • Queryable
        • ServiceCollection
        • String
        • Type
      • Helpers
        • AsyncHelper
        • BindHelper
      • JsonConverters
        • DateOnly
        • TimeOnly
        • StringNumber
    • Collections
      • AutoMapper extensions
    • IO
      • FileHelper
      • MimeTypeHelper
      • PathHelper
      • SequentialFileName
    • Configuration
      • Providers
        • JSON-file
        • XML-file
        • In-Memory
        • Consul
      • Get settings
      • Set settings
      • Customization
    • Email
      • Razor
    • Navigation
      • Menu
      • Breadcrumbs
    • Security
  • Domain
    • Domain
      • Entities
      • Value objects
  • Data Access
    • Entity Framework Core
    • Entity Framework 6
  • Use Cases
    • Operations
      • Filtering
      • Decoration
    • Notices
    • Validation
    • Specifications
    • Stateflows
      • StateMachine
      • Configurations
  • Presentation
    • ASP.NET Core
      • Client
      • Http
      • JavaScript
      • Json
      • Mvc
      • Referrer
      • Rewrite
      • Routing
      • TagHelpers
      • Validation
Powered by GitBook
On this page
  • Installation
  • Contents
  • Samples for some of methods
  • AddRangeOverride
  • GetDisplayName
  • ToFileSizeString

Was this helpful?

Edit on GitHub
  1. Utilities

Abstractions

PreviousWelcomeNextCheck

Last updated 2 years ago

Was this helpful?

Structr.Abstractions package contains number of classes and extension methods to supply wide range of developer basic needs in different situations.

Big part of package consists of extensions for most popular types allowing to avoid redundant code in widespread cases.

For example You need to add elements to dictionary while overriding values for already existing keys, then you choice is dictionary extension. Or some text should be formatted to hyphen-case style. Then string extension supplies your needs. Maybe your collection needs some tricky ordering by several fields and directions - advanced supplies everything needed. Checking input variables is most common case and many of us are bored by typing another if-null-then-throw statements. (InRange, GreaterThan, etc.) does all this job in one line.

This isn't all. There are extensions for more than ten types, tools for working with async methods, enums, sequential guids, tree-like structures, money types and more. List of all possibilities located below as do some samples.

Installation

Abstractions package is available on .

dotnet add package Structr.Abstractions

Contents

Samples for some of methods

AddRangeOverride

Example of mentioned above AddRangeOverride dictionary extension:

var dictionary = new Dictionary<int, string>
{
    { 1, "One" },
    { 2, "Two" },
    { 3, "Three" },
    { 4, "Four" }
};
var newDictionary = new Dictionary<int, string>
{
    { 1, "One_overridden" },
    { 3, "Three_overridden" },
    { 5, "Five_new" }
};
dictionary.AddRangeOverride(newDictionary);

So after applying extension method will look like:

{ 1, "One_overridden" },
{ 2, "Two" },
{ 3, "Three_overridden" },
{ 4, "Four" },
{ 5, "Five_new" }

GetDisplayName

Allows to get Display attribute value for enums:

private enum FooBarBaz
{
    Foo,
    [Display(Name = "BarBarBar")]
    Bar,    
    [Display(Name = "displayNameForEnumBaz", ResourceType = typeof(SomeResources))]
    Baz
}

string d1 = FooBarBaz.Foo.GetDisplayName(); // Foo, because no display name was provided
string d2 = FooBarBaz.Bar.GetDisplayName(); // BarBarBar
string d3 = FooBarBaz.Baz.GetDisplayName(); // Value will be taken from SomeResources file

ToFileSizeString

Converts long variable to human readable file size in kilobytes, megabytes etc.

12L.ToFileSizeString(); // ---> 12.0 bytes
2200L.ToFileSizeString(); // ---> 2.1 KB
3330000L.ToFileSizeString(); // ---> 3.2 MB
AddRangeOverride
ToHyphenCase
OrderBy
Ensure.NotNull
NuGet
Check
Ensure
Money
HierarchyId
Providers
SequentialGuidProvider
Extensions
DateTime
Dictionary
DirectoryInfo
Enumerable
Enum
Expression
Long
MemberInfo
Object
Queryable
ServiceCollection
String
Type
Helpers
AsyncHelper
BindHelper
JsonConverters
DateOnly
TimeOnly
StringNumber