Giter VIP home page Giter VIP logo

enumeration's Introduction

Headspring Enumeration

Basic Usage

public class Color : Enumeration<Color, int>
{
    public static readonly Color Red = new Color(1, "Red");
    public static readonly Color Blue = new Color(2, "Blue");
    public static readonly Color Green = new Color(3, "Green");

    private Color(int value, string displayName) : base(value, displayName) { }
}

public void Using_the_color_example()
{
    Color myFavorite = Color.Blue;

    Color leastFavorite = Color.Green;
}

public class Color : Enumeration<Color, string>
{
    public static readonly Color Red = new Color("RED", "Red");
    public static readonly Color Blue = new Color("BLUE", "Blue");
    public static readonly Color Green = new Color("GREEN", "Green");

    private Color(string value, string displayName) : base(value, displayName) { }
}

public void Using_the_color_example()
{
    Color myFavorite = Color.Blue;

    Color leastFavorite = Color.Green;
}

Creating a select list

public class State : Enumeration<State, int>
{
    public static readonly State Alabama = new State(1, "AL", "Alabama");
    public static readonly State Alaska = new State(2, "AK", "Alaska");
    // .. many more
    public static readonly State Wyoming = new State(3, "WY", "Wyoming");

    private State(int value, string displayName, string description) : base(value, displayName)
    {
        Description = description;
    }

    public string Description { get; private set; }
}

public IEnumerable<SelectListItem> Creating_a_select_list(State selected)
{
    return State.GetAll().Select(
        x => new SelectListItem
        {
            Selected = x == selected,
            Text = x.Description,
            Value = x.Value.ToString()
        });
}

As dispatch table

public class Calculation : Enumeration<Calculation, int>
{
    public static readonly Calculation Add = new Calculation(1, "Add", (left, right) => left + right);
    public static readonly Calculation Subtract = new Calculation(2, "Subtract", (left, right) => left - right);
    public static readonly Calculation Multiply = new Calculation(3, "Multiply", (left, right) => left * right);

    private Calculation(int value, string displayName, Func<int, int, int> calculation)
        : base(value, displayName)
    {
        Go = calculation;
    }

    public Func<int, int, int> Go { get; private set; }
}

public int Using_the_calculator(Calculation input)
{
    return input.Go(2, 4);
}

Helpful query methods

public class Role : Enumeration<Role, int>
{
    public static readonly Role System = new Role(999, "System", "System", true);
    public static readonly Role Manager = new Role(1, "Manager", "Michelle", false);
    public static readonly Role Employee = new Role(2, "Employee", "Eric", false);
    public static readonly Role HR = new Role(3, "Human Resources", "Harry", false);

    private Role(int value, string displayName, string personaName, bool testrole)
        : base(value, displayName)
    {
        PersonaName = personaName;
        TestRole = testrole;
    }

    public string PersonaName { get; private set; }
    public bool TestRole { get; private set; }

    public static IEnumerable<Role> GetAllProductionRoles()
    {
        return GetAll().Where(r => !r.TestRole);
    }
}

enumeration's People

Contributors

castever avatar chrismissal avatar gbheadspring avatar khellang avatar lamchakchan avatar mhinze avatar pedroreys avatar plioi avatar zkwzk avatar

Watchers

 avatar  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.