Python getting become the heart of data analysis works. You can see variety of books…
Recently, while working on one of the Windows Azure migration engagement, we were need to…
Bill Wilder who is a MVP in Windows Azure has taken nice initiative in the…
In the first part of this article, we have seen the domain model and its usage for examination question paper as Level 0 in our step by step making of a DSL.
Let us pick up the Exam constructor.
var dotnetExam = new Exam(".NET Fundamentals", Level.Beginner);
On what way we can make this as domain-friendly.
To answer the first question, let us try to use C# "Named Argument" feature.
var dotExam = new Exam(title: ".NET Fundamentals", level: Level.Beginner);
//Exam.cs public string Title { get; set; } public Level Level { get; set; } public ListQuestions { get; private set; } public Exam() { Question.QNo = 1; Questions = new List (); }
and the instantiation would be
var dotnetExam = new Exam { Title = ".NET Fundamentals", Level = Level.Beginner };
If we relax the rule of Exam object instantiation, we can use C# optional parameter in the constructor as
//Exam.cs public Exam(string title="Untitled", Level level = Level.Beginner) { Title = title; Level = level; Question.QNo = 1; Questions = new List(); }
var dotnetExam = new Exam(title: ".NET Fundamentals");
Since, the one I'm preparing here is for Beginners, I left the default option for Level.
Almost, we have answered the first question. However, how are we going to answer the second question. Avoid using "new", "var" C# keywords. A static factory method would be the option for this. Where can we create that method? In Exam class itself or on a separate static class. Exam.create(...) wouldn't fit into the DSL. So, let us create a static class ExamBuilder.
//ExamBuilder.cs public static class ExamBuilder { public static Exam exam(string title = "Untitled", Level level = Level.Beginner) { return new Exam() { Title = title, Level = level }; } }
You may wonder, why the method name violate the .NET method naming convention. Yes, I'm violated, after all, I'm going to create this DSL for a domain user, not for a .NET developer. Readability matters in DSL. See the usage of this:
var dotnetExam = ExamBuilder.exam(title: ".NET Fundamentals");
Still, "var" creates some noises in the DSL. But as of now, it is a required devil. Otherwise, how can we call Exam's AddQuestion() method.
But, Why do we require intermediatery variable "dotnetExam" here, because as a DSL, the user will add questions immediately after the "exam()" method. So, we can enforce:
ExamBuilder.exam(".NET Fundamentals").addQuestion(...);
But, how can we pass "question" objects to its "AddOption" method to add options and how can we create second, third, forth...n number of questions.
< Prev | Next > |
---|
© 2011 Udooz.net All Rights Reserved.