手机界面设计模式
It’s used to link multiple methods calls into a single compound statement. Method Chaining requires that every method return an interface that supports chaining. It may take some time to get used to because even setters in a Fluent world should have a return interface, which goes against what we’re used to in traditional data patterns.
它用于将多个方法调用链接到单个复合语句中。 方法链接要求每个方法都返回一个支持链接的接口。 可能要花一些时间才能习惯,因为即使在Fluent的世界中,设置员也应该具有返回接口,这与我们在传统数据模式中惯用的接口相反。
Example
例
//Method Chainingvar students = StudentDetailsUsingMethodChaining.Select().WithCourse("Course 1").Fetch;Conjunctions words like AND, OR, NOR, FOR, and NOT are required when combining multiple paths into one statement.
将多个路径组合到一条语句中时,需要使用AND , OR , NOR , FOR和NOT之类的连接词。
Example
例
//Conjunctionsvar conjectionStudents = StudentDetailsUsingConjuction.Select().WithCourse("Course 1").And.Exclude("student 1").Fetch;Let’s take the example of a student who has applied for different courses like Course 1, Course 2, and so on.
让我们以一个已经申请了不同课程(例如课程1,课程2等)的学生为例。
Our main goal is to achieve LINQ like method chaining as shown below as Select, WithCourse & Fetch.
我们的主要目标是实现类似于LINQ的方法链接,如下所示,如Select, WithCourse & Fetch.
//Method Chainingvar students = StudentDetailsUsingMethodChaining.Select().WithCourse("Course 1").Fetch;Please find below the examples of entity class & their relevant Interface.
请在下面找到实体类及其相关接口的示例。
An entity that consists of the student name and course object.
由学生姓名和课程对象组成的实体。
public class StudentModel{ public string Name { get; set; } public CourseModel Courses { get; set; }}An entity that consists of course name and its weightage.
由课程名称及其权重组成的实体。
public class CourseModel{ public string CourseName { get; set; } public string Weightage { get; set; }}Fluent Interface with a property that returns ICourses.
流利的接口,具有返回ICourses的属性。
public interface IStudent{ ICourses Courses { get; }}Fluent Interface with method WithCourses, which filters a list of courses by course name & returns IStudent interface.
具有方法WithCourses的Fluent接口,该方法按课程名称过滤课程列表并返回IStudent接口。
public interface ICourses{ IStudent WithCourse(string courseName);}For populating data, the below method is used.
要填充数据,请使用以下方法。
private List<CourseModel> courses = new List<CourseModel>();private List<StudentModel> students = new List<StudentModel>();public void PrepareData(){ var course1 = new CourseModel() { CourseName = "Course 1", Weightage = "5" };var course2 = new CourseModel() { CourseName = "Course 2", Weightage = "4" };var course3 = new CourseModel() { CourseName = "Course 3", Weightage = "3" }; courses.Add(course1); courses.Add(course2); courses.Add(course3);students.Add(new StudentModel() { Name = "student 1", Courses = course1 } ); students.Add(new StudentModel() { Name = "student 2", Courses = course1 } ); students.Add(new StudentModel() { Name = "student 3", Courses = course1 } ); students.Add(new StudentModel() { Name = "student 4", Courses = course2 } ); students.Add(new StudentModel() { Name = "student 5", Courses = course1 } ); students.Add(new StudentModel() { Name = "student 6", Courses = course3 } );}The below class contains a static method “Select()” which in turn creates a Student class object and return Courses associated with it.
下面的类包含一个静态方法“ Select()” ,该方法又创建了一个Student类对象并返回与之关联的Courses。
public class StudentDetailsUsingMethodChaining{public static FluentApiExample.MethodChaining.ICourses Select() { return new FluentApiExample.MethodChaining.Student().Courses; }}The “StudentDetailsUsingMethodChaining” class contains a static method called “Select()” which returns an “ICourses” interface.
“ StudentDetailsUsingMethodChaining”类包含一个名为“ Select()”的静态方法,该方法返回“ ICourses”接口。
Now “ICourses” interface contains a method “WithCourse,” which filters the students based upon course name and returns an “IStudent” interface.
现在,“ ICourses”界面包含“ WithCourse”方法,该方法根据课程名称过滤学生并返回“ IStudent”界面。
The “IStudent” interface contains a method “Fetch,” which returns the filtered students by course name.
“ IStudent”界面包含“ Fetch”方法,该方法按课程名称返回过滤的学生。
var students = StudentDetailsUsingMethodChaining.Select().WithCourse("Course 1").Fetch;foreach (var student in students){ Console.WriteLine($@"Students of Course 1 : {student.Name}");}//Output//Students of Course 1 : student 1//Students of Course 1 : student 2//Students of Course 1 : student 3//Students of Course 1 : student 5Learn to apply conjunctions to fetch a list of students opted for Course 1 excluding student 1.
学习应用连接词来获取选择了课程1的学生(学生1除外)的列表。
Our main goal is to achieve LINQ like method chaining as shown below as Select, WithCourse, Exclude & Fetch.
我们的主要目标是实现类似于LINQ的方法链接,如下所示,如Select, WithCourse, Exclude & Fetch.
//Conjunctionsvar conjectionStudents = StudentDetailsUsingConjuction.Select().WithCourse("Course 1").And.Exclude("student 1").Fetch;Two new methods are introduced, namely, Include & Exclude.
引入了两种新方法,即Include & Exclude 。
public interface ICourses{ IStudent WithCourse(string courseName); IStudent Include(string studentName); IStudent Exclude(string studentName);}New conjunction is added named “AND.”
添加了名为“ AND”的新连接。
public interface IStudent{ ICourses Courses { get; } ICourses And { get; } List<StudentModel> Fetch { get; }}ICourses interface implementation is updated with new methods.
使用新方法更新了ICourses接口的实现。
//....public Courses(IEnumerable<StudentModel> students){ this.students = students.ToList();}public IStudent Include(string studentName){ return new Student( students.Where(x => x.Name.Equals(studentName)) .ToList() );}public IStudent Exclude(string studentName){ return new Student( students.Where(x => !x.Name.Equals(studentName)) .ToList() );}//....The “StudentDetailsUsingMethodChaining” class contains a static method called “Select()” which returns an “ICourses” interface.
“ StudentDetailsUsingMethodChaining”类包含一个名为“ Select()”的静态方法,该方法返回“ ICourses”接口。
Now “ICourses” interface contains a method “WithCourse,” which filters the students based upon course name and returns an “IStudent” interface.
现在,“ ICourses”界面包含“ WithCourse”方法,该方法根据课程名称过滤学生并返回“ IStudent”界面。
The “AND” conjunction passed the IStudent Interface to Courses, which contains a new method “Exclude,” which filters the existing collection by student name & returns the “IStudent” interface.
“ AND”联合将IStudent接口传递给课程,其中包含新方法“ Exclude” ,该方法按学生姓名过滤现有馆藏并返回“ IStudent”接口。
The “IStudent” interface contains a method “Fetch,” which returns the filtered students by course name.
“ IStudent”界面包含“ Fetch”方法,该方法按课程名称返回过滤的学生。
//Conjunctionsvar conjectionStudents = StudentDetailsUsingConjuction.Select().WithCourse("Course 1").And.Exclude("student 1").Fetch;foreach (var student in conjectionStudents){ Console.WriteLine($@"Students of Course 1 : {student.Name}");}//Output//Students of Course 1 : student 2//Students of Course 1 : student 3//Students of Course 1 : student 5The below consists of implementations of method chaining and conjunctions differentiated with a namespace.
以下内容由方法链接和以命名空间区分的连接的实现组成。
Follow me on LinkedIn Instagram Facebook Twitter
跟随我的LinkedIn的Instagram的Facebook的Twitter
翻译自: https://medium.com/@singhsukhpinder/fluent-interface-design-pattern-17eb43ee0a4b
手机界面设计模式