UI Object Connector Implementation of Mediator Pattern

When we consume business objects in UI, the logic can become pretty complex for activities like setting the business object value, getting the object value and setting the user interface from the object value. We can minimize the complexity of UI interaction with the business object using mediator pattern.
Download

  Download source code for UI Object Connector Implementation of Mediator Pattern

UI Object Connector Implementation of Mediator Pattern 

 

Table of Contents

Introduction 

When we consume business objects in UI, the logic can become pretty complex for activities like setting the business object value, getting the object value and setting the user interface from the object value. We can minimize the complexity of UI interaction with the business object using mediator pattern.

I have been writing and recording a lot of architecture related videos on design patterns, UML, FPA estimation, C# projects, etc. You can watch my videoshere.

The Problem

In almost all projects, we have the basic three tier architecture as shown in the below figure. So the UI creates the business object, sets the values or gets the values and flourishes the UI.

The three big things we need to do on the UI are stated below:

  1. Setting the business object values from the user interface: 

    public void setObjectFromUI(clsInvoice objInvoice){objInvoice.CustomerName = txtCustomerName.Text;objInvoice.CustomerAddress = txtCustomerAddress.Text;objInvoice.Amount = Convert.ToDouble(lblTotalAmountToBePaid.Text);objInvoice.PaidAmount = Convert.ToDouble(txtAmountPaid.Text);objInvoice.InvoiceDate = Convert.ToDateTime(txtInvoiceDate.Text);objInvoice.InvoiceComments = txtComments.Text;objInvoice.InvoiceReference = txtInvoiceNumber.Text;objInvoice.Quantity = Convert.ToInt16(TxtQuantity.Text);objInvoice.TaxAmount = Convert.ToDouble(txtTaxAmount.Text);}
  2. Getting the values from the objects and displaying the same in the UI:

    txtCustomerName.Text = objInvoice.CustomerName;txtCustomerAddress.Text = objInvoice.CustomerAddress;lblTotalAmountToBePaid.Text = objInvoice.Amount.ToString();txtAmountPaid.Text = objInvoice.PaidAmount.ToString();txtInvoiceDate.Text = objInvoice.InvoiceDate.ToString();txtComments.Text = objInvoice.InvoiceComments.ToString();txtInvoiceNumber.Text = objInvoice.InvoiceReference.ToString();TxtQuantity.Text = objInvoice.Quantity.ToString();txtTaxAmount.Text = objInvoice.TaxAmount.ToString();
  3. Clearing the UI values for fresh and new inputs: 

    public void clearText(){txtInvoiceNumber.Text = "";txtComments.Text = "";txtInvoiceDate.Text = "";TxtQuantity.Text = "";lblTotalAmountToBePaid.Text = "";txtTaxAmount.Text = "";txtAmountPaid.Text = "";txtCustomerName.Text = "";txtCustomerAddress.Text = "";}

All the above things can clutter your UI. The most important thing is that the UI is tied up with the business object for various reasons.

Solution

The answer to this is if we can make a mediator who takes care of which UI objects map which object property that will solve our problem. So what we need to do is implement a simple UI object connector which takes the values from UI and sets to the object. This can be easily implemented by using mediator pattern. In case you are not aware of it, you watch this video to understand the concept.

UI Object Connector Implementation

Below is our simple class which will be used in a simple UI:

public class clsSampleClass{private string strProperty1="Default1";private string strProperty2="Default2";public string Property1{set{strProperty1 = value;}get{return strProperty1;}}public string Property2{set{strProperty2 = value;}get{return strProperty2;}}}

For the mediator class, we will have two array lists - one which will have the text box and the other will have the property. Every item of the array, i.e. Text box will have a corresponding item of property where the value will be set: 

private ArrayList objtextboxes = new ArrayList();private ArrayList objPropertyInfo = new ArrayList();

The user interface, i.e. the ASPX page will be exposed a method ‘Add’ which will take the corresponding text box and the property to which this text box is tied up: 

public void Add(TextBox objtextBox, string strPropertyName){objtextboxes.Add(objtextBox);objPropertyInfo.Add(strPropertyName);}

To set the values from the object to the UI, we will use reflection. So get the text box and the corresponding property and set the same using reflection: 

public void setValuesToObject(clsSampleClass objSample){// get the object typeType objType = objSample.GetType();// browse through all the UI objects and set the object// from the UI to the corresponding propertyfor(int i=0;i<objtextboxes.Count;i++){// get the UI objectPropertyInfo objProperty = objType.GetProperty(objPropertyInfo[i].ToString());// set the object property from the UI objectobjProperty.SetValue(objSample,((TextBox)objtextboxes[i]).Text,null);}}

The same holds true when we set the values back to UI: 

public void setValuesToUI(clsSampleClass objSample){Type objType = objSample.GetType();for (int i = 0; i < objtextboxes.Count; i++){PropertyInfo objProperty = objType.GetProperty(objPropertyInfo[i].ToString());((TextBox)objtextboxes[i]).Text= objProperty.GetValue(objSample, null).ToString();}}

If you want to clear the text box, just call all the objects of text boxes in the array list and set the same to nothing: 

public void ClearTextBox(){Type objType = Type.GetType("clsSampleClass");for (int i = 0; i < objtextboxes.Count; i++){((TextBox)objtextboxes[i]).Text = "";}}

The Neat Client Code 

Now we have a one liner client code. If you want to set the values from the UI to the object, just call setValuesToObject:

objMediator.setValuesToObject(objSample);

If you want to set the values of object to the UI, just call setValuesToUI

objMediator.setValuesToUI(objSample);

If you want to clear all UI, call ClearTextBox:

objMediator.ClearTextBox();

转自:http://www.dotnetfunda.com/articles/article244.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
“Many teams have a master developer who makes a rapid stream of good decisions all day long. Their code is easy to understand, quick to modify, and feels safe and comfortable to work with. If you ask how they thought to write something the way they did, they always have a good reason. This book will help you become the master developer on your team. The breadth and depth of topics will engage veteran programmers, who will pick up new tricks and improve on old habits, while the clarity makes it accessible to even novice developers.”, by Russ Rufer, Silicon Valley Patterns Group, “Many people don't realize how readable code can be and how valuable that readability is. Kent has taught me so much, I'm glad this book gives everyone the chance to learn from him.”, by Martin Fowler, chief scientist, ThoughtWorks, “Code should be worth reading, not just by the compiler, but by humans. Kent Beck distilled his experience into a cohesive collection of implementation patterns. These nuggets of advice will make your code truly worth reading.”, by Gregor Hohpe, author of Enterprise Integration Patterns, “In this book Kent Beck shows how writing clear and readable code follows from the application of simple principles. Implementation Patterns will help developers write intention revealing code that is both easy to understand and flexible towards future extensions. A must read for developers who are serious about their code.”, by Sven Gorts, “Implementation Patterns bridges the gap between design and coding. Beck introduces a new way of thinking about programming by basing his discussion on values and principles.”, by Diomidis Spinellis, author of Code Reading and Code Quality, Software Expert Kent Beck Presents a Catalog of Patterns Infinitely Useful for Everyday Programming, Great code doesn't just function: it clearly and consistently communicates your intentions, allowing other programmers to understand your code, rely on it, and modify it with confidence. But great code doesn't just happen. It is the outcome of hundreds of small but critical decisions programmers make every single day. Now, legendary software innovator Kent Beck—known worldwide for creating Extreme Programming and pioneering software patterns and test-driven development–focuses on these critical decisions, unearthing powerful “implementation patterns” for writing programs that are simpler, clearer, better organized, and more cost effective., Beck collects 77 patterns for handling everyday programming tasks and writing more readable code. This new collection of patterns addresses many aspects of development, including class, state, behavior, method, collections, frameworks, and more. He uses diagrams, stories, examples, and essays to engage the reader as he illuminates the patterns. You'll find proven solutions for handling everything from naming variables to checking exceptions., This book covers:, The value of communicating through code and the philosophy behind patterns, How and when to create classes, and how classes encode logic, Best practices for storing and retrieving state, Behavior: patterns for representing logic, including alternative paths, Writing, naming, and decomposing methods, Choosing and using collections, Implementation pattern variations for use in building frameworks, Implementation Patterns will help programmers at all experience levels, especially those who have benefited from software patterns or agile methods. It will also be an indispensable resource for development teams seeking to work together more efficiently and build more maintainable software. No other programming book will touch your day-to-day work more often.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值