Category Archives: C#

WPF Automatic FlowDirection

When developing WPF for a BiDi world – the world I live in now – I think it’s important to show the RIGHT paragraph settings for text to user.

The following Converter helps achieve such ability automatically based on text.

using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Data;

namespace DNE.WPF.Converters {

	/// <summary>
	/// This is a Helper Converter.<br/>
	/// It helps determine the <see cref="FlowDirection"/> property, automatically.
	/// </summary>
	public class TextToFlowDirectionConverter : IValueConverter {

		#region IValueConverter Members

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			string format = parameter as string;
			if (!string.IsNullOrEmpty(format)) {
				return IsRTL(string.Format(culture, format, value)) ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
			}else{
				return IsRTL(string.Format(culture, "{0}", value)) ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			return null;
		}

		#endregion

		private bool IsRTL(string input) {
			bool result = false;
			Regex rPersianChars = new Regex("^(?:[^a-z\u060C\u061B\u061F\u0621-\u064A\u064B-\u0652\u0660-\u066D\u0670-\u06F9\uFB50-\uFBB1\uFBD3-\uFD3F\uFDF2\uFDF4\uFDFA-\uFDFB\uFE70-\uFE72\uFE74\uFE76-\uFEFC]*)(?<fachar>[\u060C\u061B\u061F\u0621-\u064A\u064B-\u0652\u0660-\u066D\u0670-\u06F9\uFB50-\uFBB1\uFBD3-\uFD3F\uFDF2\uFDF4\uFDFA-\uFDFB\uFE70-\uFE72\uFE74\uFE76-\uFEFC]+?)");
			MatchCollection mc = rPersianChars.Matches(input);
			if (mc.Count > 0 && mc.Cast<Match>().Any(m => m.Groups["fachar"].Success)) {
				result = true;
			}
			return result;
		}

	}

}

The usage would be really easy:

<Window x:Class="WPFTest.MainWindow"
		xmlns:dneConv="clr-namespace:DNE.WPF.Converters;assembly=DNE.WPF"
		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
		xmlns:sys="clr-namespace:System;assembly=mscorlib"
		Title="MainWindow" Height="350" Width="525">

 <Window.Resources>
  <dneConv:TextToFlowDirectionConverter x:Key="TextToFlowDirectionConverter"/>
 </Window.Resources>

 <TextBox x:Name="EmailField"  FlowDirection="{Binding Text, Converter={StaticResource TextToFlowDirectionConverter}, RelativeSource={RelativeSource Self}}"/>

</Window>

I hope this helps my fellow Iranian Devs 🙂

Suppression mechanism for DevTrends’ WCFDataAnnotations

If you’ve ever developed a Windows Communication Service (WCF) project, you’ll know that there comes a time you’re gonna need to validate objects passing through your service’s pipeline and luckily enough there’s already a very well-designed, easy-to-use Nuget package available for you; it’s called WCFDataAnnotations.

There is only simething left out on this great plugin. An escape door! When you decorate your service implementation with [ValidateDataAnnotationsBehavior], all of the service operations will get validated and there’s no way to tell the plugin to ignore validation on some of your operations.

Fortunately, the solution was really easy. Add the following class to its source code and decorate the service operations with [SuppressValidation] to disable validation on those operations.

SuppressValidationAttribute.cs

/*
 * Suppression Mechanism for DevTrends' WCF validation plugin.
 * Created by Mohammad M. Saffari http://blog.saffari.space
 * 2015-05-13
*/
using System;
using System.Linq;
using System.ServiceModel.Description;
 
namespace DevTrends.WCFDataAnnotations {
	/// <summary>
	/// Disables all validation when applied to a WCF service operation
	/// </summary>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
	public class SuppressValidationAttribute : Attribute, IOperationBehavior {
 
		private bool _isActive = true;
 
		/// <summary>
		/// Enables or Disables the <see cref="SuppressValidationAttribute"/><para/>
		/// Default is <value>True</value> which means the <see cref="SuppressValidationAttribute"/> is Active
		/// </summary>
		public bool IsActive {
			get { return _isActive; }
			set { _isActive = value; }
		}
 
		/// <summary>
		/// Disables all validation when applied to a WCF service operation
		/// </summary>
		/// <param name="isActive">Default is <value>True</value> which means the <see cref="SuppressValidationAttribute"/> is Active</param>
		public SuppressValidationAttribute(bool isActive = true) {
			_isActive = isActive;
		}
 
 
		#region IOperationBehavior Members
 
		/// <summary>
		/// 
		/// </summary>
		/// <param name="operationDescription"></param>
		/// <param name="bindingParameters"></param>
		public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { }
 
		/// <summary>
		/// 
		/// </summary>
		/// <param name="operationDescription"></param>
		/// <param name="clientOperation"></param>
		public void ApplyClientBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.ClientOperation clientOperation) { }
 
		/// <summary>
		/// Removes the <see cref="ValidatingParameterInspector"/> from inspectors.
		/// </summary>
		/// <param name="operationDescription"></param>
		/// <param name="dispatchOperation"></param>
		public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation) {
			//dispatchOperation.ParameterInspectors.Add(new SuppressValidationParameterInspector());
			if (_isActive) {
				ValidatingParameterInspector[] pia = dispatchOperation.ParameterInspectors
						.Where(pi => pi is ValidatingParameterInspector)
						.Cast<ValidatingParameterInspector>()
						.ToArray();
				foreach (var vpi in pia) {
					dispatchOperation.ParameterInspectors.Remove(vpi);
				}
			}
		}
 
		/// <summary>
		/// 
		/// </summary>
		/// <param name="operationDescription"></param>
		public void Validate(OperationDescription operationDescription) { }
 
		#endregion
	}
}

 

Deep Object Copy

یه وقتهایی لازم میشه که ۲ تا Instance از ۲ تا کلاس مختلف که Property های شبیه به هم دارن رو به صورت یک طرفه Sync کنیم. در واقع میخوایم یکیشون رو توی اون یکی کپی کنیم.

برای بهتر روشن شدن موضوع یه مثال میزنم. فرض کنید که 4 تا کلاس داریم به این صورت:

Tester.DeepCopy.Models.Province
Tester.DeepCopy.Models.City
Tester.DeepCopy.ClumsyModels.ClumsyProvince
Tester.DeepCopy.ClumsyModels.ClumsyCity

کد داخل این کلاسها رو میتونین تصور کنین که چی خواهد بود:

Tester.DeepCopy.Models:

using System.Text;

namespace Tester.DeepCopy.Models {
	public class City {
		public long Id { get; set; }
		public long ProvinceId { get; set; }
		public string Title { get; set; }

		public Province Province { get; set; }

		public override string ToString() {
			StringBuilder sb = new StringBuilder();
			sb.Append(&amp;amp;quot;{&amp;amp;quot;);
			System.ComponentModel.PropertyDescriptorCollection pdcSource = 
				System.ComponentModel.TypeDescriptor.GetProperties(this.GetType());
			for (int i = 0; i < pdcSource.Count; i++) {
				System.ComponentModel.PropertyDescriptor pd = pdcSource[i];
				sb.AppendFormat(&amp;amp;quot;{0}={1}&amp;amp;quot;, pd.Name, pd.GetValue(this));
				if (i < pdcSource.Count - 1) sb.Append(&amp;amp;quot;, &amp;amp;quot;);
			}
			sb.Append(&amp;amp;quot;}&amp;amp;quot;);
			return sb.ToString();
		}
	}

	public class Province {
		public long Id { get; set; }
		public string Title { get; set; }

		public override string ToString() {
			StringBuilder sb = new StringBuilder();
			sb.Append(&amp;amp;quot;{&amp;amp;quot;);
			System.ComponentModel.PropertyDescriptorCollection pdcSource = 
				System.ComponentModel.TypeDescriptor.GetProperties(this.GetType());
			for (int i = 0; i < pdcSource.Count; i++) {
				System.ComponentModel.PropertyDescriptor pd = pdcSource[i];
				sb.AppendFormat(&amp;amp;quot;{0}={1}&amp;amp;quot;, pd.Name, pd.GetValue(this));
				if (i < pdcSource.Count - 1) sb.Append(&amp;amp;quot;, &amp;amp;quot;);
			}
			sb.Append(&amp;amp;quot;}&amp;amp;quot;);
			return sb.ToString();
		}
	}
}

Tester.DeepCopy.ClumsyModels:

using System.Text;

namespace Tester.DeepCopy.ClumsyModels {
	public class ClumsyCity {
		public string Title { get; set; }
		public long Id { get; set; }
		public ClumsyProvince Province { get; set; }
		public long ProvinceId { get; set; }

		public override string ToString() {
			StringBuilder sb = new StringBuilder();
			sb.Append(&amp;amp;quot;{&amp;amp;quot;);
			System.ComponentModel.PropertyDescriptorCollection pdcSource = 
				System.ComponentModel.TypeDescriptor.GetProperties(this.GetType());
			for (int i = 0; i < pdcSource.Count; i++) {
				System.ComponentModel.PropertyDescriptor pd = pdcSource[i];
				sb.AppendFormat(&amp;amp;quot;{0}={1}&amp;amp;quot;, pd.Name, pd.GetValue(this));
				if (i < pdcSource.Count - 1) sb.Append(&amp;amp;quot;, &amp;amp;quot;);
			}
			sb.Append(&amp;amp;quot;}&amp;amp;quot;);
			return sb.ToString();
		}
	}

	public class ClumsyProvince {
		public long Id { get; set; }
		public string Title { get; set; }

		public override string ToString() {
			StringBuilder sb = new StringBuilder();
			sb.Append(&amp;amp;quot;{&amp;amp;quot;);
			System.ComponentModel.PropertyDescriptorCollection pdcSource = System.ComponentModel.TypeDescriptor.GetProperties(this.GetType());
			for (int i = 0; i < pdcSource.Count; i++) {
				System.ComponentModel.PropertyDescriptor pd = pdcSource[i];
				sb.AppendFormat(&amp;amp;quot;{0}={1}&amp;amp;quot;, pd.Name, pd.GetValue(this));
				if (i < pdcSource.Count - 1) sb.Append(&amp;amp;quot;, &amp;amp;quot;);
			}
			sb.Append(&amp;amp;quot;}&amp;amp;quot;);
			return sb.ToString();
		}
	}
}

همونطور که میبینین، کد داخل کلاسها، شبیه به هم هست، ولی کلاسهای توی ۲ تا Namespace مجزا هستند و ترتیب Property ها یکسان نیست.

حالا موقعیتی رو در نظر بگیرید که یک Instance از کلاس City دارید و میخواین اون رو کپی کنین توی یه Instance از کلاس ClumsyCity. ابتدایی ترین راه اینه که Propertyهاش رو نظیر به نظیر برابر کنین با Property های City؛ ولی اگه تعداد Proprty ها زیاد بود چی؟ اگه فقط همین 2 تا کلاس نبود و مثلاً با 20 تا کلاس باید همین کار رو میکردین چطور!!؟

مثلاً موقعیتی رو در نظر بگیرید که توی یک Solution شما هم برنامه ی Web-Based دارید و  هم پروژه‌ی Smart Device. دیتابیسهای روی Deviceها رو میخواین Sync کنین با سرور اصلی. چه می کنین!!؟

اینجاست که DeepCopy که یه Extension Method خیلی ساده هست به درد میخوره:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;

namespace DNE.ExtensionMethods {
	/// <summary>
	/// My Extension Methods
	/// </summary>
	public static class Extensions {
		/// <summary>
		/// 
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="target"></param>
		/// <param name="source"></param>
		public static void DeepCopy(this Object target, T source) {
			if (source == null) { target = null; return; }
			if (target == null) { throw new ArgumentNullException(&amp;amp;quot;target&amp;amp;quot;); }

			PropertyDescriptorCollection pdcSource = TypeDescriptor.GetProperties(source.GetType());
			PropertyDescriptorCollection pdcTarget = TypeDescriptor.GetProperties(target.GetType());

			pdcSource.Cast().ToList().ForEach(pdS => {
				PropertyDescriptor pdT = pdcTarget.Find(pdS.Name, true);
				if (pdT != null) {
					if (pdT.PropertyType == pdS.PropertyType) {
						pdT.SetValue(target, pdS.GetValue(source));
					} else {
						ConstructorInfo ciCtor = pdT.PropertyType.GetConstructor(Type.EmptyTypes);
						if (pdT.GetValue(target) == null) {
							pdT.SetValue(target, ciCtor.Invoke(null));
							pdT.GetValue(target).DeepCopy(pdS.GetValue(source));
						} else {
							pdT.GetValue(target).DeepCopy(pdS.GetValue(source));
						}
					}
				}
			});
		}
	}
}

طرز استفاده اش هم خیلی ساده است:

static void TestDeepCopy() {
	City c1 = new City {
		Id = 1,
		ProvinceId = 1,
		Title = "Tehran",
		Province = new Province { 
			Id = 1,
			Title = "Tehran"
		}
	};
	ClumsyCity c2 = new ClumsyCity();
	c2.DeepCopy(c1);

	Console.WriteLine("City: {0}", c1);
	Console.WriteLine("Clumsy City: {0}", c2);

}

نتیجه‌ی اجرای متد بالا اینطور میشود:

City: {Id=1, ProvinceId=1, Title=Tehran, Province={Id=1, Title=Tehran}}
Clumsy City: {Title=Tehran, Id=1, Province={Id=1, Title=Tehran}, ProvinceId=1}
Press any key to continue . . .