Giter Site home page Giter Site logo

bubdm / dotnetrules Goto Github PK

View Code? Open in Web Editor NEW

This project forked from matthiaskainer/dotnetrules

0.0 0.0 0.0 13.25 MB

The DotNetRules Library is a .net rule engine that applies policies to objects without the need to call each Policy manually from code, but with one simple call.

License: MIT License

C# 100.00%

dotnetrules's Introduction

Abstract

The DotNetRules Library is an approach to apply policies to objects without the need to call each Policy manually from code, but with one simple call. Using this approach you can deploy your policies in an external library and deploy them independently of the core application.

Introduction

As a software developer, I went through a lot of situations where I had to validate conditions on an existing object. With the coming of domain driven design I found myself ending up with dozens of lines of fluent validation code that made my clean domain object look like spaghetti code all over again. Using a rule engine is fun, but most of the engines out there required a lot of perquisites and all I wanted was to write some code, so I started my own lightweight engine.

Execute all policies:

myObject.ApplyPolicies();

A simple policy:

using DotNetRules.Runtime;
[Policy(typeof(MyObject))]
internal class Policy : PolicyBase<MyObject>
{
    Given objectIsHuge = () => Subject.Size > 1000;

    Then shrinkIt = () =>
    {
        Subject.Data = Packaging.Compress(Subject.Data);
    };
}

A more advanced policy:

[Policy(typeof(Capability.Event), typeof(Guid))]
class EventWriteAccess : RelationPolicyBase<Capability.Event, Guid>
{
    static bool _allowed = false;

    Establish _context = target =>
    {
        _allowed = false;
    };

    Given _userCanWrite = () => Source.Owner == Target ||
                                Source.AdministatorsId.Contains(Target) ||
                                Source.EditorsId.Contains(Target);
								
	Or _userIsPlattformAdmin = () => Plattform.IsAdmin(Target);

    Then _grantPermission = () => _allowed = true;

    Return<bool> _return = () => _allowed;
}

Use it

Open your nuget console and do

Install-Package DotNetRules

or

Install-Package DotNetRules.Web.Mvc

dotnetrules's People

Contributors

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