Asp.net interface-oriented extensible framework-Business Rule Engine extension module and Business Rule extension module

Source: Internet
Author: User

Asp.net interface-oriented extensible framework-Business Rule Engine extension module and Business Rule extension module

As the interface-oriented extensible framework continues to be developed, some function development has encountered a "bottleneck", and there are too many things to be written to death. However, the scalability of the Code to be written is very poor, and you are confused to find the code...

I think that several projects I have developed already have some configurable capabilities. I think about how to abstract the code from these areas. When I think of the "Business Rule Engine", if I find a few on the Internet that are not very eye-catching, I will take the time to create a "Wheel"

The business rule engine has corresponding modules in many mature workflow engines and is one of the core of the workflow. However, in addition to workflows, many businesses require the business rule engine. Therefore, it is necessary to act as a module independently.

In reality, many projects require customized branch requirements. Using the Business Rule Engine for logical Branch Routing, parameter "correction", interception, and so on should not be too powerful.

Here we will use the Community to post an example.

1. First read the article Model

A: ArticleRepository is an article Data Warehouse. It stores the article in the memory List.

B: The User is the author of the Article, the Article is the Article, and the ArticlePublish is DTO, which contains the information of the Article and the author.

2. Different articles are classified.

ArticleRepository toparticipant = new ArticleRepository {Name = ""}; ArticleRepository PerfectArticles = new ArticleRepository {Name = ""}; ArticleRepository NetArticles = new ArticleRepository {Name = ". net "}; ArticleRepository OtherArticles = new ArticleRepository {Name =" "};

A: There are two categories:. net articles and other articles, and "stick" and "essence ".

B: The article is divided into. net articles and other articles. In the future, it is hard to say that you do not need to add other types such as Java and PHP, so this area needs to be scalable.

C: Not everyone puts their posts on top and highlights, or they won't be able to play happily.

3. Push by authorization

First look at authorization-related Modeling

For simplicity and clarity, memory objects are used to simulate storage.

A: Role is A Role, and the Role has A weight field (Sort). The higher the value of Sort, the higher the permission.

(Do some people say that roles should not be equal? Control permissions by binding resources? I would like to ask if the country President and your village director can be equal? Role privileges and resource binding should be integrated. In addition, we demonstrate the business rule engine. instead of discussing the permission system, we can stop it first)

B: RolePermission is used to Grant and obtain permissions, and maintain the association between a user and permissions.

Public class RolePermission: IEntityAccess <User, Role>, IComparer <Role> {private Dictionary <User, Role> _ permission = new Dictionary <User, Role> (); public bool Grant (User user, Role role) {Role role0 = Get (user); if (role0! = Null & role0.Sort> = role. sort) return true; _ permission [user] = role; return true;} public Role Get (User user) {Role role = null; _ permission. tryGetValue (user, out role); return role;} int IComparer <Role>. compare (Role x, Role y) {return Comparer <Role>. default. compare (x, y );}}RolePermission

4. Continue scenario settings

Role manager = new Role {Id = 1, Name = "Administrator", Sort = 9999}; Role expert = new Role {Id = 2, Name = "expert ", sort = 999 };
User user1 = new User {Id = 1, Name = "Zhang San", Year = 3}; User user2 = new User {Id = 2, Name = "Li Si ", year = 10}; User user3 = new User {Id = 3, Name = "", Year = 0 };

5. Now you can start posting articles.

            RolePermission permission = new RolePermission();            ConfigRole(permission);            ArticlePublish post1 = new ArticlePublish(user1, new Article { Content = ".Net" });            ArticlePublish post2 = new ArticlePublish(user2, new Article { Content = "Java" });            ArticlePublish post3 = new ArticlePublish(user3, new Article { Content = "Php" });            Engine<ArticlePublish, int> engine = new Engine<ArticlePublish, int>();            ConfigCategory(engine);            Post(engine, post1, post2, post3);            Show(TopArticles, PerfectArticles, NetArticles, OtherArticles);
Private void ConfigRole (RolePermission permission) {permission. Grant (user3, expert );}Authorization code private static void Post (Engine <ArticlePublish, int> engine, params ArticlePublish [] articles) {foreach (var item in articles) {int id = 0; if (engine. run (item, ref id) Console. writeLine (string. concat ("article processed successfully, Id =", id. toString ()));}}Post Code private static void Show (params ArticleRepository [] repositorys) {foreach (var repository in repositorys) {Console. writeLine (new string ('-', 80); List <Article> list = repository. listAll (); if (list. count <1) {Console. writeLine (string. concat (repository. name, "NONE"); continue;} Console. writeLine (repository. name); foreach (var item in list) {Console. writeLine (string. concat ("Article {Id =", item. id, ", Content =", item. content, "}");} Console. writeLine (new string ('-', 80 ));}}Show all Article Codes
The article is processed successfully. Id = 1: The article is processed successfully. Id = 2: The article is processed successfully. Id = 3: The top is set to no ---------------------------------------------------------- the essence is none --------------------------------------------------------------------------------. netArticle {Id = 1, Content =. net} Other --------------------------------- other Article {Id = 2, Content = Java} Article {Id = 3, Content = Php} else }--------------------------------------------------------------------------------

Three articles have been published successfully, one. net article and two others. The results are very good.

Wait a moment. What is Engine <ArticlePublish, int>? Shouldn't it be ArticleRepository to publish an article?

Engine is a well-known business rule Engine. ArticleRepository does not publish an article, but it is up to the Engine to decide whether to send the article or who to use it. These are the business rules,

(ArticleRepository is only responsible for storage and reading, and has a very single responsibility)

6. Let's take a look at the definition of business rules.

        private void ConfigCategory(Engine<ArticlePublish, int> engine)        {            engine.When(post => post.Article.Content.Contains(".Net")).Then(post => NetArticles.Add(post.Article));            engine.Then(post => OtherArticles.Add(post.Article));        }

It is very simple. If the article contains. net keywords, use NetArticles for storage; otherwise, use OtherArticles for storage (Table sharding is so easy !!!)

7. Example of continued push

            Engine<ArticlePublish, int> pushEngine = new Engine<ArticlePublish, int>();            ConfigPush(permission, pushEngine);            ConfigYear(pushEngine);            Post(pushEngine, post1, post2, post3);            Show(TopArticles, PerfectArticles, NetArticles, OtherArticles);
Article processing succeeded. Id = 2 Article processing succeeded. Id = 3 ← top Article {Id = 3, Content = Php} else ------------------------------------- essence Article {Id = 2, content = Java} has }----------------------------------------------------------------------------------------------------------------------------------------------------------------. netArticle {Id = 1, Content =. net} Other --------------------------------- other Article {Id = 2, Content = Java} Article {Id = 3, Content = Php} else }--------------------------------------------------------------------------------

This time, I have an article in both the top-level and the top-level projects.

8. Let's see how the push rule is defined.

        private void ConfigPush(RolePermission permission, Engine<ArticlePublish, int> engine)        {            int topNum = 0;            int topLimit = 1;            engine.When(post => topNum < topLimit && Comparer<Role>.Default.Compare(permission.Get(post.User), expert) >= 0).Then(post => { topNum++; return TopArticles.Add(post.Article); });            engine.When(post => Comparer<Role>.Default.Compare(permission.Get(post.User), expert) >= 0).Then(post => PerfectArticles.Add(post.Article));        }
        private void ConfigYear(Engine<ArticlePublish, int> engine)        {            engine.When(post => post.User.Year >= 8).Then(post => PerfectArticles.Add(post.Article));        }

Explanation

A: if experts and the above permissions can be issued to the top, pushed to the top (first come first served first)

B: else if articles published by experts are pushed to the essence.

C: else if posts sent by Members over 8 years are pushed to the essence

D: else does nothing.

Note: Do not share the rationality of the above business rules with me. It is just a test example.

That's simple. The boss no longer has to worry about writing business rules.

 

The above Code uses Fluent code for business rule configuration. In the future, I also need to use file configuration for dynamic business rules for use in the container configuration file.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.