Giter Club home page Giter Club logo

unni's Introduction

Unni

Open-source libraries to create multifunctional and safe structure for your project

Unni.Infrastructure

Represents UnniResult class which allows you to easy transfer validation errors and inner exceptions between methods.

Installation

Install NuGet package: Unni.Infrastructure

Or using the command line below:

dotnet add package unni.infrastructure

Usage

Imports

using Unni.Infrastructure;

UnniResult

Divided on 2 classes - UnniResult and UnniResult<T>

UnniResult contains these properties:

Property Description
Success Indicates if executed method was successful
Errors Contains list of validation errors
InnerException Contains thrown exception from method

UnniResult<T> extends UnniResult and adds this property:

Property Description
Data Final result of executed method with type <T>

Examples

Let's create the simple console app with example of usage

  1. Create the new Console project
  2. Add Unni.Infrastructure library using NuGet
  3. Add the namespace to the Program.cs file
using Unni.Infrastructure;
  1. Add the method which represents a service for getting the list of numbers
public static UnniResult<List<int>> FillArray(int count)
{
    try
    {
        if (count == 0)
        {
            return new UnniResult<List<int>>("! Count can not be 0");
        }
        List<int> result = new List<int>(count);
        for (int i = 1; i <= count; i++)
        {
            result.Add(i);
        }
        return new UnniResult<List<int>>(result);
    }
    catch(Exception ex)
    {
        return new UnniResult<List<int>>(ex);
    }
}
  1. Add the method for displaying errors
public static void ShowErrorResult(UnniResult result)
{
    if (result.InnerException != null)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine($"- Oops, something went wrong!"); //Tell user about unexpected error
        Console.WriteLine($"- Exception occured: {result.InnerException.Message}"); //Log exception to proceed it in the future
        Console.ForegroundColor = ConsoleColor.White;
    }
    Console.ForegroundColor = ConsoleColor.Yellow;
    foreach (var error in result.Errors)
    {
        Console.WriteLine(error); //Tell user about validation errors
    }
    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine();
}
  1. Add the method for executing FillArray method
public static void ExecuteResut(int count)
{
    Console.WriteLine($"Executing method with count {count}");
    var result = FillArray(count);
    if (result.Success)
    {
        for (int i = 0; i < result.Data.Count; i++)
        {
            Console.WriteLine(result.Data[i]);
        }
    }
    else
    {
        ShowErrorResult(result);
    }
}
  1. Change the Main method to run some tests
public static void Main()
{
    //Testing validation errors
    ExecuteResut(0);

    //Testing inner exception
    ExecuteResut(-1);

    //Testing successful method execution
    ExecuteResut(5);
}
  1. Run the project and you will get this output
Executing method with count 0
! Count can not be 0

Executing method with count -1
- Oops, something went wrong!
- Exception occured: Non-negative number required. (Parameter 'capacity')

Executing method with count 5
1
2
3
4
5

unni's People

Contributors

nikitafursaybtech avatar undernickofficial avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.