ZayniFramework.Common 2.1.0

Common used .NET libraries such as extensions API, dynamic API, Reflection API, Paging API, TaskQueue and Result entities.

There is a newer version of this package available.
See the version list below for details.
Install-Package ZayniFramework.Common -Version 2.1.0
dotnet add package ZayniFramework.Common --version 2.1.0
<PackageReference Include="ZayniFramework.Common" Version="2.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ZayniFramework.Common --version 2.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Add namespace using in your code.

using ZayniFramework.Common;

Extensions API example.

DateTimeExtension

Test a DateTime range. Use the DateTime.IsBetween() extension method.

var dt    = new DateTime( 2013, 12, 24 );
var begin = new DateTime( 2013, 12, 1 );
var end   = new DateTime( 2013, 12, 31 );
Assert.IsTrue( dt.IsBetween( begin, end ) );
Assert.IsFalse( dt.IsBetween( new DateTime( 2013, 12, 28 ), end ) );

Convert DateTime to a UNIX UTC timestamp. Use the DateTime.ToUnixUtcTimestamp() extension method.

long timestamp = DateTime.Now.ToUnixUtcTimestamp( withMilliseconds: true );
ObjectExtension

Convert an object to a Dictionary. Use the Object.ConvertToDictionary() extension method.

public void ConvertToDictionary_Example()
{
    var model = new UserModel
    {
        Name = "Amber",
        Age  = 23,
        Sex  = "female"
    };

    IDictionary dict = model.ConvertToDictionary();

    string name = dict[ "Name" ] + "";
    string sex  = dict[ "Sex" ]  + "";
    int    age  = int.Parse( dict[ "Age" ] + "" );
}

Convert List<T> to DataTable. Use List<T>.ConvertToDataTable() extension method.

var list = new List<UserModel>() 
{
    new UserModel() 
    {
        Name = "Sylvia",
        Age  = 35,
        Sex  = "Female"
    },
    new UserModel() 
    {
        Name = "Kate",
        Age  = 20,
        Sex  = "Female"
    },
    new UserModel() 
    {
        Name = "John",
        Age  = 23,
        Sex  = "Male"
    }
};

DataTable dt = list.ConvertToDataTable( "SomeTableName" );
Assert.IsNotNull( dt );          

Distinct by conditions. Use IEnumerable<T>.Distinct( Func<TSource, TKey> selector ) extension method.

List<UserModel> models = MakeFakeUsers();
IEnumerable<UserModel> results = models.Distinct( k => new { Name = k.Name, Age = k.Age } );

Deep Clone Object. Use Object.CloneObject() or Object.Clone() extension method.

UserModel clone = obj.CloneObject<UserModel>();
UserModel copy  = obj.Clone<UserModel>();

Invoke the object's method. Use the Object.MethodInvoke() extension method.

public class MemberModel
{
    public string Name { get; set; }

    public string SayHello( string message ) => $"Hello, my name is {Name}. {message}";
}

public void MethodInvoke_Example()
{
    var model  = new MemberModel() { Name = "Sylvia" };
    var result = model.MethodInvoke( "SayHello", "This is a test." );
    Assert.AreEqual( "Hello, my name is Sylvia. This is a test.", result );
}

Get the property value. Use the Object.GetPropertyValue() extension method.

public void GetPropertyValueTest()
{
    var model = new MemberModel()
    {
        Name = "Kate"
    };

    object obj = model.GetPropertyValue( "Name" );
    Assert.IsNotNull( obj );

    string name = obj + "";
    Assert.AreEqual( model.Name, name );

    name = model.GetPropertyValue<string>( "Name" );
    Assert.AreEqual( model.Name, name );
}
StringExtension

Convert a string to byte array. Use the String.ToBytes() extension method.
Also demo how to use IEnumerable.IsNotNullAndEmptyArray() entension method.

public void ToBytes_Example()
{
    string test   = "This is a test string.";
    byte[] binary = test.ToBytes();
    Assert.IsTrue( binary.IsNotNullAndEmptyArray() );

    string result = System.Text.Encoding.UTF8.GetString( binary );
    Assert.AreEqual( test, result );
}

Remove the first appeared from a string. Use String.RemoveFirstAppeared() extension method.

string source = "AAABBBCCCAAA";
string result = source.RemoveFirstAppeared( "AAA" );
Assert.AreEqual( "BBBCCCAAA", result );

Remove the last appeared from a string. Use String.RemoveLastAppeared() extension method.

string source = "AAABBBCCCAAACCC";
string result = source.RemoveLastAppeared( "CCC" );
Assert.AreEqual( "AAABBBCCCAAA", result );

Test a string is null or an empty string.
Demo the String.IsNullOrEmpty(), String.IsNotNullOrEmpty(), String.FormatTo() extension method.

public void StringIsNullOrEmpty_Example()
{
    string test1 = "";
    Assert.IsTrue( test1.IsNullOrEmpty() );
    Assert.IsFalse( test1.IsNotNullOrEmpty() );

    string test2 = null;
    Assert.IsTrue( test2.IsNullOrEmpty() );
    Assert.IsFalse( test2.IsNotNullOrEmpty() );

    string test3 = "kkk";
    Assert.IsFalse( test3.IsNullOrEmpty() );
    Assert.IsTrue( test3.IsNotNullOrEmpty() );

    string expected1 = "Hello my name is Sylvia. I love Pony.";
    string expected2 = "I'm Pony. Sylvia is my lover.";

    string test4  = "Hello my name is {0}. I love {1}.";
    string result = test4.FormatTo( "Sylvia", "Pony" );
    Assert.AreEqual( expected1, result );

    string result2 = "I'm {0}. {1} is my lover.".FormatTo( "Pony", "Sylvia" );
    Assert.AreEqual( expected2, result2 );
}

Dynamic API example.

Add using.

using ZayniFramework.Common;
using ZayniFramework.Common.Dynamic;

Bind a property to dynamic object in runtime.

Use DynamicBroker.CreateDynamicObject() and DynamicBroker.BindProperty() static method.

public void BindPropertyTest()
{
    dynamic obj = DynamicBroker.CreateDynamicObject();
    Assert.IsNotNull( obj );

    bool isOk = DynamicBroker.BindProperty( obj, "UserName", "Sylvia" ); 
    Assert.IsTrue( isOk );

    string name = obj.UserName;
    Assert.AreEqual( "Sylvia", name );

    bool isOk2 = DynamicBroker.BindProperty( obj, "UserName", "Vivian" );
    Assert.IsTrue( isOk2 );

    string name2 = obj.UserName;
    Assert.AreEqual( "Vivian", name2 );
}

Bind a delegate to dynamic object in runtime.

Use DynamicBroker.BindMethod() static method.
Also demo Object.IsNotNull() extension method.

public void BindMethod_Example() 
{
    dynamic obj = DynamicBroker.CreateDynamicObject();
    Assert.IsNotNull( obj );

    bool isOk = DynamicBroker.BindProperty( obj, "UserName", "Sylvia" );
    Assert.IsTrue( isOk );

    string name = obj.UserName;
    Assert.AreEqual( "Sylvia", name );

    SaySomething handler = ( m ) => {
        return string.Format( "Hi, my name is {0}. {1}", obj.UserName, m );
    };

    bool isOk2 = DynamicBroker.BindMethod<SaySomething>( obj, "IntroduceYourself", handler );
    Assert.IsTrue( isOk2 );

    string message = obj.IntroduceYourself( "Nice to meet you." );
    Assert.IsTrue( message.IsNotNullOrEmpty() );
    Assert.AreEqual( "Hi, my name is Sylvia. Nice to meet you.", message );
}

TaskQueue example.

Add using.

using ZayniFramework.Common;
using ZayniFramework.Common.Tasks;

EnQueue an action to TaskQueue.

private static void ExecuteDirectly()
{
    var queue = new TaskQueue( false );

    for ( int i = 0; i < 50; i++ )
    {
        int j = i + 1;

        dynamic queueAction = new QueueAction( new Work().DoSomething );
        queueAction.Index   = j;
        queueAction.Message = new { Test = "123", NumberNo = 55 };
        queue.EnQueue( queueAction );
    }
}

More TaskQueue detail sample here.

Add namespace using in your code.

using ZayniFramework.Common;

Extensions API example.

DateTimeExtension

Test a DateTime range. Use the DateTime.IsBetween() extension method.

var dt    = new DateTime( 2013, 12, 24 );
var begin = new DateTime( 2013, 12, 1 );
var end   = new DateTime( 2013, 12, 31 );
Assert.IsTrue( dt.IsBetween( begin, end ) );
Assert.IsFalse( dt.IsBetween( new DateTime( 2013, 12, 28 ), end ) );

Convert DateTime to a UNIX UTC timestamp. Use the DateTime.ToUnixUtcTimestamp() extension method.

long timestamp = DateTime.Now.ToUnixUtcTimestamp( withMilliseconds: true );
ObjectExtension

Convert an object to a Dictionary. Use the Object.ConvertToDictionary() extension method.

public void ConvertToDictionary_Example()
{
    var model = new UserModel
    {
        Name = "Amber",
        Age  = 23,
        Sex  = "female"
    };

    IDictionary dict = model.ConvertToDictionary();

    string name = dict[ "Name" ] + "";
    string sex  = dict[ "Sex" ]  + "";
    int    age  = int.Parse( dict[ "Age" ] + "" );
}

Convert List<T> to DataTable. Use List<T>.ConvertToDataTable() extension method.

var list = new List<UserModel>() 
{
    new UserModel() 
    {
        Name = "Sylvia",
        Age  = 35,
        Sex  = "Female"
    },
    new UserModel() 
    {
        Name = "Kate",
        Age  = 20,
        Sex  = "Female"
    },
    new UserModel() 
    {
        Name = "John",
        Age  = 23,
        Sex  = "Male"
    }
};

DataTable dt = list.ConvertToDataTable( "SomeTableName" );
Assert.IsNotNull( dt );          

Distinct by conditions. Use IEnumerable<T>.Distinct( Func<TSource, TKey> selector ) extension method.

List<UserModel> models = MakeFakeUsers();
IEnumerable<UserModel> results = models.Distinct( k => new { Name = k.Name, Age = k.Age } );

Deep Clone Object. Use Object.CloneObject() or Object.Clone() extension method.

UserModel clone = obj.CloneObject<UserModel>();
UserModel copy  = obj.Clone<UserModel>();

Invoke the object's method. Use the Object.MethodInvoke() extension method.

public class MemberModel
{
    public string Name { get; set; }

    public string SayHello( string message ) => $"Hello, my name is {Name}. {message}";
}

public void MethodInvoke_Example()
{
    var model  = new MemberModel() { Name = "Sylvia" };
    var result = model.MethodInvoke( "SayHello", "This is a test." );
    Assert.AreEqual( "Hello, my name is Sylvia. This is a test.", result );
}

Get the property value. Use the Object.GetPropertyValue() extension method.

public void GetPropertyValueTest()
{
    var model = new MemberModel()
    {
        Name = "Kate"
    };

    object obj = model.GetPropertyValue( "Name" );
    Assert.IsNotNull( obj );

    string name = obj + "";
    Assert.AreEqual( model.Name, name );

    name = model.GetPropertyValue<string>( "Name" );
    Assert.AreEqual( model.Name, name );
}
StringExtension

Convert a string to byte array. Use the String.ToBytes() extension method.
Also demo how to use IEnumerable.IsNotNullAndEmptyArray() entension method.

public void ToBytes_Example()
{
    string test   = "This is a test string.";
    byte[] binary = test.ToBytes();
    Assert.IsTrue( binary.IsNotNullAndEmptyArray() );

    string result = System.Text.Encoding.UTF8.GetString( binary );
    Assert.AreEqual( test, result );
}

Remove the first appeared from a string. Use String.RemoveFirstAppeared() extension method.

string source = "AAABBBCCCAAA";
string result = source.RemoveFirstAppeared( "AAA" );
Assert.AreEqual( "BBBCCCAAA", result );

Remove the last appeared from a string. Use String.RemoveLastAppeared() extension method.

string source = "AAABBBCCCAAACCC";
string result = source.RemoveLastAppeared( "CCC" );
Assert.AreEqual( "AAABBBCCCAAA", result );

Test a string is null or an empty string.
Demo the String.IsNullOrEmpty(), String.IsNotNullOrEmpty(), String.FormatTo() extension method.

public void StringIsNullOrEmpty_Example()
{
    string test1 = "";
    Assert.IsTrue( test1.IsNullOrEmpty() );
    Assert.IsFalse( test1.IsNotNullOrEmpty() );

    string test2 = null;
    Assert.IsTrue( test2.IsNullOrEmpty() );
    Assert.IsFalse( test2.IsNotNullOrEmpty() );

    string test3 = "kkk";
    Assert.IsFalse( test3.IsNullOrEmpty() );
    Assert.IsTrue( test3.IsNotNullOrEmpty() );

    string expected1 = "Hello my name is Sylvia. I love Pony.";
    string expected2 = "I'm Pony. Sylvia is my lover.";

    string test4  = "Hello my name is {0}. I love {1}.";
    string result = test4.FormatTo( "Sylvia", "Pony" );
    Assert.AreEqual( expected1, result );

    string result2 = "I'm {0}. {1} is my lover.".FormatTo( "Pony", "Sylvia" );
    Assert.AreEqual( expected2, result2 );
}

Dynamic API example.

Add using.

using ZayniFramework.Common;
using ZayniFramework.Common.Dynamic;

Bind a property to dynamic object in runtime.

Use DynamicBroker.CreateDynamicObject() and DynamicBroker.BindProperty() static method.

public void BindPropertyTest()
{
    dynamic obj = DynamicBroker.CreateDynamicObject();
    Assert.IsNotNull( obj );

    bool isOk = DynamicBroker.BindProperty( obj, "UserName", "Sylvia" ); 
    Assert.IsTrue( isOk );

    string name = obj.UserName;
    Assert.AreEqual( "Sylvia", name );

    bool isOk2 = DynamicBroker.BindProperty( obj, "UserName", "Vivian" );
    Assert.IsTrue( isOk2 );

    string name2 = obj.UserName;
    Assert.AreEqual( "Vivian", name2 );
}

Bind a delegate to dynamic object in runtime.

Use DynamicBroker.BindMethod() static method.
Also demo Object.IsNotNull() extension method.

public void BindMethod_Example() 
{
    dynamic obj = DynamicBroker.CreateDynamicObject();
    Assert.IsNotNull( obj );

    bool isOk = DynamicBroker.BindProperty( obj, "UserName", "Sylvia" );
    Assert.IsTrue( isOk );

    string name = obj.UserName;
    Assert.AreEqual( "Sylvia", name );

    SaySomething handler = ( m ) => {
        return string.Format( "Hi, my name is {0}. {1}", obj.UserName, m );
    };

    bool isOk2 = DynamicBroker.BindMethod<SaySomething>( obj, "IntroduceYourself", handler );
    Assert.IsTrue( isOk2 );

    string message = obj.IntroduceYourself( "Nice to meet you." );
    Assert.IsTrue( message.IsNotNullOrEmpty() );
    Assert.AreEqual( "Hi, my name is Sylvia. Nice to meet you.", message );
}

TaskQueue example.

Add using.

using ZayniFramework.Common;
using ZayniFramework.Common.Tasks;

EnQueue an action to TaskQueue.

private static void ExecuteDirectly()
{
    var queue = new TaskQueue( false );

    for ( int i = 0; i < 50; i++ )
    {
        int j = i + 1;

        dynamic queueAction = new QueueAction( new Work().DoSomething );
        queueAction.Index   = j;
        queueAction.Message = new { Test = "123", NumberNo = 55 };
        queue.EnQueue( queueAction );
    }
}

More TaskQueue detail sample here.

NuGet packages (21)

Showing the top 5 NuGet packages that depend on ZayniFramework.Common:

Package Downloads
ZayniFramework.Logging
Support file log, database log, email log, windows event log and console log. Also you can define log event, then you can pipe your log message to anywhere you want.
ZayniFramework.Serialization
The serialization module support JSON (System.Text.Json or Newtonsoft.Json) or XML serialization. You can also define your own serialization component and register it into SerializerFactory.
ZayniFramework.Middle.Service.Entity
The Middle.ServiceHost.Entity define the entities and DTOs for Middle.ServiceHost and Middle.ServiceHost.Client module.
ZayniFramework.Validation
The Validation module provide many kinds of validation attributes and Validator for .NET applications.
ZayniFramework.Middle.Service
The Middle.Service module provide a self-hosted .NET middleware framework to separate the communication protocol detail with your business logic code. You can use it to build the backend microservices easier. Support TCPSocket, WebSocket and RabbitMQ protocol. You can also implement the IRemoteHost interface and register it into the Middle.Service module as an extension module for this middleware framework. Have a nice day... :)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.19.1 95 1/6/2021
2.19.0 109 1/1/2021
2.18.3 123 12/27/2020
2.18.2 924 8/29/2020
2.18.1 3,766 8/26/2020
2.18.0 465 8/20/2020
2.17.135 159 8/19/2020
2.17.134 1,025 7/28/2020
2.17.133 239 7/27/2020
2.17.132 254 7/18/2020
2.17.131 514 7/11/2020
2.16.130 604 6/13/2020
2.15.128 407 6/3/2020
2.15.127 539 5/31/2020
2.15.126 1,310 4/30/2020
2.14.122 446 4/13/2020
2.13.100 570 3/12/2020
2.12.51 593 2/18/2020
2.11.50 398 2/10/2020
2.10.44 629 1/30/2020
2.9.43 775 1/11/2020
2.8.42 629 1/10/2020
2.8.41 488 1/5/2020
2.7.40 552 1/2/2020
2.7.39 458 1/2/2020
2.7.38 618 1/1/2020
2.6.37 543 12/23/2019
2.6.35 786 12/4/2019
2.6.1 449 12/2/2019
2.6.0 458 11/28/2019
2.5.2 653 11/26/2019
2.5.1 569 11/12/2019
2.5.0 451 11/9/2019
2.4.3 595 10/16/2019
2.4.2 410 10/16/2019
2.4.1 574 9/20/2019
2.3.28 522 9/19/2019
2.3.27 613 8/30/2019
2.3.26 615 8/20/2019
2.3.25 562 8/12/2019
2.3.22 457 7/31/2019
2.3.21 668 7/20/2019
2.3.20 603 6/22/2019
2.3.19 516 6/14/2019
2.3.18 541 6/13/2019
2.3.17 588 6/13/2019
2.3.15 594 6/8/2019
2.3.14 572 6/8/2019
2.3.13 617 5/30/2019
2.3.12 563 5/24/2019
2.3.11 589 5/24/2019
2.3.10 616 5/21/2019
2.3.9 563 5/9/2019
2.3.8 590 5/8/2019
2.3.7 631 4/30/2019
2.3.6 604 4/23/2019
2.3.5 640 4/19/2019
2.3.4 550 4/18/2019
2.3.3 578 4/17/2019
2.3.2 594 4/6/2019
2.3.1 599 12/15/2018
2.3.0 607 12/7/2018
2.2.6 730 11/25/2018
2.2.3 396 11/23/2018
2.2.2 517 11/20/2018
2.2.1 717 11/17/2018
2.2.0 538 11/16/2018
2.1.0 559 11/15/2018
2.0.1 524 11/6/2018
2.0.0 534 11/4/2018
Show less