Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

News

My Friends

My Links

Permanent Posts

Portuguese Communities

September 2010 - Posts

BizTalk 2010

Well, BizTalk 2010 has arrived. This time, you the Developer version is fully featured and free for use in non comercial scenarios. Here are some must-have links, check out specially the Training Kit, and, of course, the Developer Edition download:

Prerequisites

BizTalk Server 2010 - Developer Edition

BizTalk Server 2010 Training Kit

BizTalk Server 2010 Documentation

BizTalk Server Best Practices Analyzer V1.2

BizTalk Server 2010 ESB Toolkit Documentation

BizTalk Adapter Pack 2010 Documentation

A4SWIFT 2010 Documentation

BizTalk RFID Server 2010 and BizTalk RFID Mobile 2010 Documentation

BizTalk 2010 Accelerator for HL7 Documentation

Microsoft BizTalk Adapters 2010 for Enterprise Applications Installation Guide

BizTalk Server 2010 Management Pack Documentation

BizTalk Server 2010 Technical Overview

BizTalk Server 2010 BAM Poster

BizTalk Server 2010 Runtime Architecture Poster

BizTalk Server 2010 Capabilities Poster

BizTalk Server 2010 Scale-out Configurations Poster

BizTalk Server 2010 ESB Toolkit Architecture Poster

BizTalk Server 2010 Database Infrastructure Poster

BizTalk Adapter Pack 2010 Poster

BizTalk 2010 Accelerator for RosettaNet (BTARN) Documentation

BizTalk Server 2010 FileAct and InterAct Adapters Documentation

BizTalk Server 2010 Monitoring Management Pack

Bookmark and Share
Detaching an Entity in LINQ to SQL

LINQ to SQL, shamelesly, does not have a Detach method, like most other O/RMs. In one particular case, I needed one - or, at least, I thought I did - so I went to write one, which wouldn't require me to use a base class. Thanks to Reflector, here's what I came up with (only tested it in version 1, not 4):


		public static void DisableAllEventHandlers(Object control)
		{
			EventDescriptorCollection ec = TypeDescriptor.GetEvents(control);

			for (Int32 i = 0; i < ec.Count; ++i)
			{
				DisableEventHandlers(control, ec[i].Name);
			}
		}

		private static FieldInfo FindField(Object obj, String fieldName)
		{
			FieldInfo fi = null;
			Type type = obj.GetType();

			while (type != null)
			{
				fi = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

				if (fi != null)
				{
					break;
				}
				else
				{
					type = type.BaseType;
				}
			}

			return (fi);
		}

		private static Object FindProperty(Object obj, String propertyName)
		{
			Type type = obj.GetType();
			PropertyInfo pi = null;
			Object value = null;

			while (type != null)
			{
				pi = type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance);

				if (pi != null)
				{
					value = pi.GetValue(obj, null);
					break;
				}
				else
				{
					type = type.BaseType;
				}
			}

			return (value);
		}

		public static EventHandler DisableEventHandlers(Object control, String eventName)
		{
			FieldInfo eventFieldInfo = FindField(control, String.Concat("Event", eventName));
			EventHandler oldEventHandler = null;
			EventHandlerList eventsHandlerList = null;
			Object eventKeyObject = null;

			if (eventFieldInfo != null)
			{
				//standard process for .NET controls
				eventKeyObject = eventFieldInfo.GetValue(control);

				eventsHandlerList = FindProperty(control, "Events") as EventHandlerList;

				if ((eventKeyObject != null) && (eventsHandlerList != null))
				{
					oldEventHandler = eventsHandlerList[eventKeyObject] as EventHandler;
					eventsHandlerList[eventKeyObject] = null;
				}
			}
			else
			{
				//alternative process, for events other than the standard .NET ones
				eventFieldInfo = FindField(control, eventName);

				if (eventFieldInfo != null)
				{
					oldEventHandler = eventFieldInfo.GetValue(control) as EventHandler;
					eventFieldInfo.SetValue(control, null);
				}
			}

			return (oldEventHandler);
		}

		public Boolean IsAttached<TEntity>(TEntity entity)
		{
			Object services = typeof(DataContext).GetProperty("Services", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty).GetValue(this, null);
			Object changeTracker = services.GetType().GetProperty("ChangeTracker", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty).GetValue(services, null);

			Boolean isTracked = (Boolean)changeTracker.GetType().BaseType.GetMethod("IsTracked", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(changeTracker, new Object[] { entity });

			return (isTracked);
		}

		public void Detach<TEntity>(TEntity entity)
		{
			DisableAllEventHandlers(entity);

			Object services = typeof(DataContext).GetProperty("Services", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty).GetValue(this, null);
			Object changeTracker = services.GetType().GetProperty("ChangeTracker", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty).GetValue(services, null);
			Object identityManager = services.GetType().GetField("identifier", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic).GetValue(services);
			Object caches = identityManager.GetType().GetField("caches", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic).GetValue(identityManager);

			Object items = changeTracker.GetType().GetField("items", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(changeTracker);
			MetaType type = this.Mapping.GetMetaType(entity.GetType());

			Object existingEntity = identityManager.GetType().GetMethod("FindLike", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(identityManager, new Object[] { type, entity });

			Object trackedObject = changeTracker.GetType().GetMethod("GetTrackedObject", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(changeTracker, new Object[] { entity });

			Boolean result = (Boolean) identityManager.GetType().GetMethod("RemoveLike", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(identityManager, new Object[] { type , entity });

			Boolean isTracked = (Boolean)changeTracker.GetType().BaseType.GetMethod("IsTracked", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(changeTracker, new Object[] { entity });

			if (isTracked == true)
			{
				changeTracker.GetType().GetMethod("StopTracking", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(changeTracker, new Object[] { entity });
			}
		}
Bookmark and Share
Two Clicks Required

Sometimes, when I have an UpdatePanel and multiple validators on a page, I have to click twice on a LinkButton, even if it has CausesValidation set to false. The control is outside the UpdatePanel, so I'm not sure what causes this. It is a frequent problem, as can be seen on Google.

I am looking into it now, so far, I have a simple solution that works:


function forcePostback(control)
{
	var old = Page_ValidationActive;
	Page_ValidationActive = false;
	__doPostBack(control, '');
	Page_ValidationActive = old;
}

When I have more information, I will post about it. If you happen to know the problem and its solution, please drop me a line!

Bookmark and Share
Unit Testing Asp.NET Applications with with Moles and Pex

It has been a while since my last post. I will try to recover, there are some things I would like to talk about. For now, check out this fine tutorial for unit testing ASP.NET applications using Pex and Moles: http://research.microsoft.com/en-us/projects/pex/molesaspnet.pdf.

Of course, don't forget Microsoft's Lightweight Automation Test Framework and WatiN! I will post on these great libraries later on.

Bookmark and Share
More Posts