SoFunction
Updated on 2025-03-07

C# Create your own DSL using the Fluent API (recommended)

The role of DSL is to solve the communication problems between field experts and software developers. It sounds very bluffing, but it is not a profound thing. We can use the Fluent API to create our own DSL

DSL (Domain Specified Language) domain-specific language is a language that describes problems in a specific field. It sounds very intimidating, but it is actually not a profound thing. Take a look at the following code:

using FlunetApiDemo;

var Zhang San = "Zhang San"
                .It's a student()
                .height(1.62M)
                .weight(48M);

(Zhang San.BMI());
(Zhang San.BMIstate());

This code calculates the BMI and determines the status (slim, normal, overweight or obese) based on the student's height and weight. Seeing this, all students may have found the problem: students have elementary school students, middle school students and college students. Is the calculation algorithm the same? The same is true for boys and girls? In this question, everyone is a field expert and found the problem from the code I wrote that describes a specific problem. I need to modify the code to increase the age and gender factors.

From the above example, we can see the role of DSL: it is to solve communication problems between field experts and software developers. Domain experts usually do not know how to program and cannot judge whether the code written by developers meets the requirements of the domain. They can only wait until the software is written and judge the functions shown by the software operation. At this time, the cost has already occurred. After several rounds and turns, the progress has exceeded the time limit and the cost has exceeded the cost. DSL is written using domain-related terms, which can be understood by domain experts, and the language itself is based on a certain host language, such as C#, which can be compiled and run and is easy to verify. Therefore, proper DSL can clear up the barriers between field experts and developers, making the core part of the software's business development reliable and efficient. "Can be executed" is the biggest difference between DSL and pseudo-languages ​​used in the requirements stage or natural languages ​​with illustrations. When describing requirements, various illustrations or pseudo-languages ​​are often used to describe the business. Pseudo-languages ​​are generally a similar structured language. Such language-like things are often very harmful because they only roughly describe the process, and many implementation details are ignored or hidden. Since it is not a strict programming language, executable code cannot be generated, so it is impossible to verify right or wrong.

Combining the above example, we look at how to create your own DSL using Fluent Api. The technology it uses is essentially implementing extensions of existing types, which requires us to 1) declare a static class, 2) use static functions in the class, and 3) use this keyword to modify the types that need to be extended. The above "Zhang San" is student(), and "is student" is an extension of the string type. It returns the Student type you defined. This code is as follows:

namespace FlunetApiDemo
{
    public static class FluentExt
    {
        public static Student It's a student(this string  name)
        {
            return new Student { Name = name };
        }

        public static Student height(this Student student,decimal height)
        {
             = height;
            return student;
        }

        public static Student weight(this Student student, decimal weight)
        {
             = weight;
            return student;
        }

        public static decimal BMI(this Student student)
        {
            return  /  / ;
        }

        public static string BMIstate(this Student student)
        {
            var bmi=();
            if (bmi > 24) return "obesity";
            if (bmi > 21) return "overweight";
            if (bmi < 15) return "Slim";
            return "normal";
        }
    }
}

Only key attributes are defined in the Student class:

namespace FlunetApiDemo
{
    public  class Student
    {
        public string Name { get; set; }=;

        public decimal Height { get; set; } 

        public decimal Weight { get; set; }
        public override string ToString()
        {
            return Name;
        }
    }
}

How about it, it's quite simple. The complete code is uploaded to github:/zhenl/FlunetApiDemo

The final problem is the Chinese problem in the code. My principle is how to be convenient. Usually, when writing programs, we do not advocate using Chinese as variables or method names. Although many compilers in modern programming languages ​​are no longer limited to only supporting ASCII code, we still cannot ensure that there is no problem in some cases (for example, if Chinese-named methods are mapped to Web Api interface, clients that do not support Chinese may not be able to call this API). However, as a domain-specific language, DSL does not need to have this limitation. The main purpose of DSL is communication. If it must be written in English or Chinese pinyin, the effect will be greatly reduced, not to mention that many fields are mainly Chinese. I won’t go into it here. To give a few examples, “Tang Poetry”, “Song Ci” and “Yuan Song” are probably translated into English as English fields and experts and programmers cannot understand it.

This is the end of this article about C# using the Fluent API to create your own DSL. For more related C# Fluent API content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!