un visualizzatore di eccezioni parte 1: helper

a questo link trovare un eccellente esempio di come gestire l'eccezioni sollevate dal controllo SqlDataSource.

Analogamente è possibile gestire l'eccezioni sollevate dai controlli ObjectDataSource e LinqDataSource.

Utilizzando molto, ognuno dei suddetti controlli, mi sono trovato presto a affrontare la manutenzione di una marea di pagine contenente la logica di visualizzazione delle eccezioni.

ho deciso quindi di realizzare un WebControl che associato a un DataSourceControl potesse intercettare l'eccezioni e visualizzare.

per prima cosa ci creeremo una serie di metodi helper, per associare runtime un gestore all'evento , in particolare realizzeremo degli Extension Methods.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using ReflectionExtension;

namespace ReflectionExtension
{
public static class ObjectReflectionExtension
{
public static void AddHanlder(this Object objectEventRaiser, object objectBoundToDelegate, string methodName, string eventName)
{
if (objectEventRaiser == null | objectBoundToDelegate == null | string.IsNullOrEmpty(methodName) | string.IsNullOrEmpty(eventName))
{
throw new ArgumentNullException();
}
else
{
Type _dataSourceType = objectEventRaiser.GetType();

EventInfo _eventInfo = _dataSourceType.GetEvent(eventName);

if (_eventInfo == null)
{
throw new ArgumentException(string.Format("Impossibile trovare l'evento {0}", eventName));
}
else
{
MethodInfo _methodInfo = objectBoundToDelegate.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);

if (_eventInfo == null)
{
throw new ArgumentException(string.Format("Impossibile trovare il metodo {0}", methodName));
}
else
{
Delegate _delegate = Delegate.CreateDelegate(_eventInfo.EventHandlerType, objectBoundToDelegate, _methodInfo);

_eventInfo.AddEventHandler(objectEventRaiser, _delegate);
}
}
}
}
public static bool IsNull(this Object item)
{
return item == null;
}

}
}

namespace WebControlReflectionExtension
{
public static class WebControlReflectionExtension
{

public static void AddInsertedHandler(this DataSourceControl dataSourceControl, object objectBoundToDelegate, string methodName)
{
ObjectReflectionExtension.AddHanlder(dataSourceControl, objectBoundToDelegate, methodName, "Inserted");
}

public static void AddDeleteddHandler(this DataSourceControl dataSourceControl, object objectBoundToDelegate, string methodName)
{
ObjectReflectionExtension.AddHanlder(dataSourceControl, objectBoundToDelegate, methodName, "Deleted");
}

public static void AddUpdateddHandler(this DataSourceControl dataSourceControl, object objectBoundToDelegate, string methodName)
{
ObjectReflectionExtension.AddHanlder(dataSourceControl, objectBoundToDelegate, methodName, "Updated");
}
}
}

Nella stessa categoria

Commenti
Daniele Bochicchio scrive:
un visualizzatore di eccezioni parte 1: helper

non posso allontarmi un attimo che mi sgarri alla regola numero uno...
29/05/2008 ore 16.20 | 1 risposta
»»»» nostromo scrive:
RE: un visualizzatore di eccezioni parte 1: helper

azz volevo approfittare della tua assenza

ma anche stavolta mi hai beccato

ciao marco
29/05/2008 ore 16.26

Aggiungi un nuovo commento »»»
Per inserire un commento, devi registrarti alla nostra community.

© 1998-2008 - nostromo - Il blog di Marco Leoncini

TagCloud
BLOG INFO
  • Post: 231
  • Commenti: 101
  • TrackBacks: 23
  • Feed blog e contenuti tecnici: RSS
  • Feed blog: RSS Atom OPML

MVP
CATEGORIE
I PIÙ LETTI DEL MESE
IN EVIDENZA