Fluent and Swiss databinding are two different approaches used in MvvmCross for creating data bindings between views and view models. Here's a detailed comparison of these two methods:
Swiss Databinding
**Overview: Swiss databinding is a syntax used primarily in XML layout files or string-based bindings. It was introduced as a replacement for the earlier JSON-based binding syntax, offering a cleaner and less verbose way to define bindings.
**Syntax Example: In Swiss, a binding might look like this:
xml
local:MvxBind="Text TweetText, Converter=RemainingLength, ConverterParameter=140"
This syntax is straightforward and easy to read, making it suitable for use in XML files or when defining bindings in a string format.
**Usage: Swiss databinding is commonly used in platforms where XML layout files are prevalent, such as Android. However, it can also be used in other platforms for string-based bindings.
Fluent Databinding
**Overview: Fluent databinding is a C#-based syntax that allows developers to define bindings in code. This approach is particularly useful on platforms like iOS and macOS, where XML layout files are not as commonly used.
**Syntax Example: Using fluent bindings, the same binding as above would be defined like this:
csharp
this.CreateBinding(label)
.For(l => l.Text)
.To(vm => vm.TweetText)
.WithConversion("RemainingLength", 140);
This syntax provides strong typing and is more flexible when working directly in code.
**Usage: Fluent databinding is beneficial when you need more control over the binding process or when working with platforms that do not support XML layout files easily. It also allows for better integration with IDE features like IntelliSense, making development more efficient.
Key Differences
- Syntax and Location: Swiss databinding uses a string-based syntax typically found in XML files or string bindings, while fluent databinding uses a C# syntax directly in code.
- Platform Suitability: Swiss is more suited for Android and other platforms using XML layouts, whereas fluent databinding is ideal for iOS, macOS, and other platforms where code-based bindings are preferred.
- Type Safety: Fluent databinding offers strong typing, which can reduce runtime errors compared to the string-based Swiss syntax.
- Flexibility and Control: Fluent databinding provides more flexibility and control over the binding process, as it allows developers to define bindings programmatically.
In summary, while both Swiss and fluent databinding serve the same purpose of linking views to view models, they differ significantly in syntax, usage context, and the level of control they offer.
Citations:[1] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[2] https://www.mvvmcross.com/documentation/getting-started/getting-started
[3] https://stackoverflow.com/questions/25460030/when-to-use-mvvmcross-data-bindings
[4] https://www.mvvmcross.com/documentation/fundamentals/value-converters
[5] https://stackoverflow.com/questions/18029248/mvvmcross-binding-to-multiple-properties-on-android-control-using-swiss-syntax
[6] https://docs.devexpress.com/WindowsForms/113956/cross-platform-app-development/winforms-mvvm/concepts/data-bindings-and-notifications
[7] https://blog.ostebaronen.dk/2018/01/mvvmcross-binding-target.html
[8] https://www.packtpub.com/en-us/learning/how-to-tutorials/sharing-mvvmcross
[9] https://crosscuttingconcerns.com/MvvmCross-Fluent-Databinding
[10] https://yanxiaodi.gitbook.io/xamarin-mvvmcross-handbook/using-mvvmcross-to-create-your-first-xamarin-app/creating-the-ios-project/understanding-the-data-binding-syntax