Best Practices for Two-Way Data Binding in Large .NET Applications

arunkumar.mr 20 Reputation points
2024-03-19T17:01:43.8666667+00:00

Hello Microsoft Q&A community,

I’m working on a large-scale .NET application, and I’ve been using two-way data binding in my XAML views. However, I’ve heard that this approach can become problematic in large projects. I’d like to understand the best practices and potential pitfalls related to two-way data binding.

Lets assume I have a form which has 50 to 100 input fields and each fields has its own visibility and triggers some other field properties. and some properties triggering API calls. so, is this two way binding triggers any unwanted API calls?

When does it become an anti-pattern in large applications?

How can I optimize performance and maintainability when using two-way data binding?

Is there any other option available to replace two way binding to avoid the above mentioned problems?

Any insights, experiences, or recommended practices would be greatly appreciated. If you’ve encountered challenges related to two-way data binding in large .NET projects, please share your expertise.

Thank you!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,347 questions
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
722 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 11,256 Reputation points Microsoft Vendor
    2024-03-28T06:22:50.5566667+00:00

    Hello @arunkumar.mr, It is due to the set properties are called. Try the following code.

    public sealed partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }
        private string _customer;
        public string Customer
        {
            get => _customer;
            set
            {
                _customer = value;
                NotifyPropertyChanged(nameof(Customer));
                {
                    _address = "Customer Address";
                    _tax = "Customer Tax";
                    NotifyPropertyChanged(nameof(Address));
                    NotifyPropertyChanged(nameof(Tax));
                }
            }
        }
        private string _address;
        public string Address
        {
            get => _address;
            set
            {
                _address = value;
                NotifyPropertyChanged(nameof(Address));
                _tax = "Address Tax";
                NotifyPropertyChanged(nameof(Tax));
            }
        }
        private string _tax;
        public string Tax
        {
            get => _tax;
            set
            {
                _tax = value;
                NotifyPropertyChanged(nameof(Tax));
                Debug.WriteLine("\n\nTrigger API call\n\n");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful