un visualizzatore di eccezioni parte 1: helper

Marco Leoncini

di Marco Leoncini, in asp.net, mercoledì 28 maggio 2008 ore 14.56

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.

<p />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"); 
        } 
    } 
} 
<p />
Commenti

Per inserire un commento, devi avere un account.

Fai il login e torna a questa pagina, oppure registrati alla nostra community.



Segnala su: Facebook MSDN Social Twitter Segnalo Wikio Diggita Technorati Stumbleupon Google Yahoo FriendFeed Delicious Furl

Nella stessa categoria
I più letti del mese
TagCloud
BLOG INFO
  • 242 post, 108 commenti, 23 trackback
  • Feed blog e contenuti tecnici: RSS
  • Feed blog: RSS Atom
IN EVIDENZA