开发者

How are fluent API's different from other API's?

开发者 https://www.devze.com 2023-01-01 14:50 出处:网络
I have come across fluent API\'s while studying DSLs. I have searched a lot on fluent API\'s...the basic conclusion which I could draw was that fluent a API uses method chaining in order to make the c

I have come across fluent API's while studying DSLs.

I have searched a lot on fluent API's...the basic conclusion which I could draw was that fluent a API uses method chaining in order to make the code fluent.

But I cannot understand - in object oriented languages we can always create an object and can call the methods related to it. Then how is a fluent A开发者_如何转开发PI different? What other features does a fluent API add?


With a fluent interface you write methods that return the object that the method was invoked on (usually self or this) and handle traditional return values as a state change in that object. If you look at say some of the Javascript libraries that use a fluent interface it makes it far easier to deal with lists and nulls as they can be handled the same way you would a single object. The disadvantage of fluent interfaces is that they tend to create monolithic god objects that have a whole heap of responsibilities.

I wouldn't want them to be used everywhere (because of the god object problem) but they are nice from time to time.


Your question is answered in the originating Fluent Interface blog post by Martin Fowler. The point is that the fluency in fluent API comes from the domain of a domain specific language, not only method chaining.


Fluent API is an advanced way of specifying model configuration that covers everything that data annotations can do in addition to some more advanced configuration not possible with data annotations.

And Web API is a programming interface/application type that provides communication or interaction between software applications.

0

精彩评论

暂无评论...
验证码 换一张
取 消