fluent-interface

When to return 'this' instead of 'void' in a method and why?

坚强是说给别人听的谎言 提交于 2021-02-08 10:12:51
问题 What are the benefits (or drawbacks) of returning a reference to 'this' object in a method that modifies itself? When should returning a 'this' be used as apposed to void? When looking at an answer on code review stack exchange, I noticed that the answer used a 'return this' in a self operating method. Simplified class from original: class Item { public Item(string name) { Name = name; } public string Name { get; private set; } public Item AddComponent(ItemComponent component) { _components

Specifying Foreign Key Entity Framework Code First, Fluent Api

不打扰是莪最后的温柔 提交于 2021-02-07 12:51:36
问题 I have a question about defining Foreign Key in EF Code First Fluent API. I have a scenario like this: Two class Person and Car. In my scenario Car can have assign Person or not (one or zero relationship). Code: public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } public class Car { public int Id { get; set; } public string Name { get; set; } public Person Person { get; set; } public int? PPPPP { get; set; } } class

Specifying Foreign Key Entity Framework Code First, Fluent Api

微笑、不失礼 提交于 2021-02-07 12:50:26
问题 I have a question about defining Foreign Key in EF Code First Fluent API. I have a scenario like this: Two class Person and Car. In my scenario Car can have assign Person or not (one or zero relationship). Code: public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } public class Car { public int Id { get; set; } public string Name { get; set; } public Person Person { get; set; } public int? PPPPP { get; set; } } class

How can I make method chaining fluent in C?

Deadly 提交于 2021-02-05 05:46:06
问题 There is an existing C API that looks like this: //data typedef struct {int properties;} Widget; //interface Widget* SetWidth(Widget *const w, int width){ // ... return w; } Widget* SetHeight(Widget *const w, int height){ // ... return w; } Widget* SetTitle(Widget *const w, char* title){ // ... return w; } Widget* SetPosition(Widget *const w, int x, int y){ // ... return w; } The first parameter is always a pointer to the instance, and the functions that transform the instance always return

Is there any disadvantage to returning this instead of void?

荒凉一梦 提交于 2021-01-02 05:13:06
问题 Say instead of returning void a method you returned a reference to the class even if it didn't make any particular semantic sense. It seems to me like it would give you more options on how the methods are called, allowing you to use it in a fluent-interface-like style and I can't really think of any disadvantages since you don't have to do anything with the return value (even store it). So suppose you're in a situation where you want to update an object and then return its current value.

Fluent interface with Python

南笙酒味 提交于 2020-06-25 05:36:49
问题 I have a Python function "send_message" which takes three arguments: send_message("i like windmills", to="INBOX", from="OUTBOX") I am thinking about putting a fluent interface on top of it. Ideally I'd like to write the following: send_message("i like windmills").to("INBOX").from("OUTBOX") or send_message("i like windmills").from("OUTBOX").to("INBOX") Update: The to() information is mandatory but the from() is not (as with real letters). So this one would also be a valid call: send_message("i

What's the point of DSLs / fluent interfaces

﹥>﹥吖頭↗ 提交于 2020-01-11 16:40:31
问题 I was recently watching a webcast about how to create a fluent DSL and I have to admit, I don't understand the reasons why one would use such an approach (at least for the given example). The webcast presented an image resizing class, that allows you to specify an input-image, resize it and save it to an output-file using the following syntax (using C#): Sizer sizer = new Sizer(); sizer.FromImage(inputImage) .ToLocation(outputImage) .ReduceByPercent(50) .OutputImageFormat(ImageFormat.Jpeg)

Cannot define composite PK with Fluent API in VB.NET

此生再无相见时 提交于 2020-01-06 01:54:25
问题 I am trying to define a composite PK for a class where one of the columns is a FK to another table. The code compiles without any errors but when I try to migrate the changes I get the following error PM> Update-Database -Force -TargetMigration:0 Specify the '-Verbose' flag to view the SQL statements being applied to the target database. System.InvalidOperationException: The properties expression 'x => new VB$AnonymousType_0`2(RtepNumber = x.RtepNumber, ContractId = x.Contract.ContractId)' is

Designing Fluent interface methods

≡放荡痞女 提交于 2020-01-03 02:59:48
问题 I am trying to write a DSL I have methods that return strings but if I want to combine the strings I need to use a + symbol but I would like to call the methods together but I'm unsure how to achieve it I have methods at the moment such as MyStaticClass.Root() MyStaticClass.And() MyStaticClass.AnyInt() which return strings I would like to be able to do Root().And().AnyInt() which result in a string 回答1: The methods should return a wrapper class. The methods are also instance methods of the

Fluent interfaces and leaky abstractions

百般思念 提交于 2020-01-01 04:18:10
问题 What is a fluent interface? I can't find a good definition of this, but all I get are long code examples in a language I am not very familiar with (e.g. C++). Also, what is a leaky abstraction? Thanks 回答1: A fluent interface is an API that allows you to write code that reads more or less like normal English. For example: Find.All.Questions(Where.IsAnswered == true); Method-chaining is usually used as part of the implementation, but there is more to it than that. To quote Fowler: I've also