Tips for writing a free interface in C # 3

I am after a few good tips for free communication in C #. I just found out about it myself, but I wanted to hear what others think outside of the articles I read. In particular, I follow:

  • When does she talk too much?
  • Are there any smooth patterns?
  • what is in C #, which makes the fluent interface freer (for example, extension methods)
  • Is it a complex, fluent interface that still remains free?
  • refactoring for free communication or refactoring an existing free interface.
  • Any good examples you've worked with or could recommend?

If you could post one piece of advice or thought, or something else in the message. I want to see how they also voted.

Thanks in advance.

+43
c # design-patterns fluent-interface
Oct 22 '08 at 7:13
source share
8 answers

In fourth place;

Yes, I think that a complex, fluent interface can be white.

I think runaway interfaces are more of a compromise. (although good!) There has been a lot of research on using natural language for programming, and as a rule, natural language is not accurate enough to express programs.

Free interfaces are built so that they write as a programming language, only a small subset of what you can express in a natural language is allowed, but they are read as a natural language.

If you look at the bullying of rhinos, for example, the part of the letter was complicated compared to a regular library. I took me longer to find out mainly because of the free interface, but it makes the code much easier to read. Since programs are usually written once and read many times, this is a good compromise.

So, to appreciate my point of view a little. A free interface that is difficult to write but easy to read can be white.

+18
Oct 22 '08 at 7:42
source share

The most difficult problem that I have encountered as a consumer with smooth interfaces is that most of them are not truly free intefaces - instead, they are really examples of what I call "legible interfaces".

A free interface implies that its main goal is to simplify SPEAK, while a clear interface implies that its main goal is to read it easily. Most runaway interfaces are generally ridiculously difficult to code, but, conversely, incredibly easy to READ later by others.

Assert().That().This(actual).Is().Equal().To(expected). Except().If(x => x.GreaterThan(10)); 

... much easier to read later than actually composing in code!

+27
Feb 01 '09 at 13:27
source share

You use brick when using inheritance along with smooth interfaces, because using polymorphic methods breaks your call chains, and you definitely don't want your interfaces to be immune by using ugly casting and paranthesis where they are not needed. I wrote an article about a template that provides a workaround using common constructors and common extension methods with common restrictions: http://liviutrifoi.wordpress.com/2009/02/16/fluent-interfaces-constraints-at-compile-time/

+11
Feb 16 '09 at 17:52
source share

Moq hides undoped methods like equals, ToString , etc. to make their free interface even easier to use.

A hiding system object is an article explaining the advantage of this.

+8
Aug 25 '09 at 10:44
source share

And to your second and third question;

Three smooth patterns that I noticed

The first uses the using statement (C # 2.0) to run the code in a specific context, for example:

 using(var transaction = new Transaction()) { // .. // .. } 

This uses the constructor and transaction removal tool to set up the transaction, and then runs the code in this context.

The second does almost the same thing, but with lambda, it is often used in Rhino Mocks, for example.

 (new Transaction()).Run( () => mycode(); ); 

The best-known free interface is to use return types to invoke chained methods. Basically, methods return this, so you can associate calls with the same object. But you can also return different objects to change the context depending on the method called. If you have an object that can only work in a transaction (sorry, I can’t think of another example), you can give it the StartTransaction method, which returns an initialized transaction in which you can start the call and stoptransaction in pseudo-code:

 class Runner { Transaction StartTransaction() { return new Transaction(this); } } class Transaction { Transaction Run() Transaction StopTransaction() } 

where the call looks like

 var runner = new Runner(); runner .StartTransaction() .Run() .StopTransaction(); 

Of course you need to add all kinds of error handling, etc.

+7
Oct 22 '08 at 7:57
source share

I also just jump on learning how to write a free interface for a small application at work. I asked and researched a little, and found that a good approach to writing a free interface is to use the "Builder template", read more about this here .

In fact, that’s how I started mine:

 public class Coffee { private bool _cream; private int _ounces; public Coffee Make { get new Coffee(); } public Coffee WithCream() { _cream = true; return this; } public Coffee WithOuncesToServe(int ounces) { _ounces = ounces; return this; } } 

Here's a cross-post for a similar question that I have for implementing closure in a free interface.

+7
Nov 25 '09 at 5:54
source share

It is one thing that you must take into account the morphology of the syntax of the English language and make sure that you do not introduce an undocumented serial link below.

 // Snarky employees get a raise. employees.WhereSnarky().GiveRaise(); 

against.

 // Depending on implementation, everyone may get a raise. employees.GiveRaise().WhereSnarky(); 
+4
Aug 20 '13 at 14:51
source share

Once upon a time I had the same doubts as now. I have done some research, and now I am writing a few posts to help with these topics.

Take a look on your blog:

Recommendations for a free interface design in C # 1

And in the following articles I will talk about each of the points you mentioned.

Regards Andre Vianna

+2
Aug 05 '10 at 1:24
source share



All Articles