CodeSmith Community
Your Code. Your Way. Faster!

Handling EDS events

Latest post 11-25-2007 11:50 PM by vsagar41. 15 replies.
  • 08-17-2006 2:03 PM

    • Meech
    • Top 50 Contributor
    • Joined on 02-14-2006
    • Posts 98
    • Points 2,788

    Handling EDS events

    I'm struggling with some syntax here, I was wondering if somebody could help me out.
     
    I want to write some custom FormUtil methods, passing a custom entitydatasource as a parameter handling the selecting, selected, updating, updated, etc events.   (Mostly for exception handling)
     
    The BaseDataSource<Entity, Entitykey> class defines those events, but I am struggling with how to define the method signature.
     
    public static void handleSelect(FormView formView, BaseDataSource dataSource)
    public static void handleSelect(FormView formView, BaseDataSource<Entity, EntityKey> dataSource)
     
    Something along those lines.    Seems like it would be nice if all the custom datasources descended from a common simple ancestor.
    • Post Points: 75
  • 08-17-2006 10:59 PM In reply to

    Re: Handling EDS events

    What functionality will you be requiring for your methods?   Will you need to cast it back, or are you just trying to gain a reference to it?

    You can use either DataSourceControl, IListDataSource, if you're needing the actual BaseDataSource, the second declaration you've listed would work fine.

    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 08-17-2006 11:59 PM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: Handling EDS events

    I have created a new interface called IDataSourceEvents that you should be able to use with any typed data source control.  Try it out and see if it meets your needs.  This has been checked into SVN rev 296.

    Hope that helps.

    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 5
  • 08-18-2006 10:59 AM In reply to

    • Meech
    • Top 50 Contributor
    • Joined on 02-14-2006
    • Posts 98
    • Points 2,788

    Re: Handling EDS events

    I just wanted to implement some inline delegates (? right term ?)  to catch any errors during select/insert/update and display them in a masterpage control.
     
    Neither declaration compiles.   I'm not at my dev computer right now so I can't tell you the exact error.
     
    Bobby --
     
    That sounds like it'll do the trick.   I'll check it out later.  Thanks!!!
     
     
    • Post Points: 35
  • 10-19-2006 6:21 PM In reply to

    • Evan
    • Top 150 Contributor
    • Joined on 09-19-2006
    • Seattle, WA
    • Posts 30
    • Points 820

    Re: Handling EDS events

    I'm in a similar situation. I'd like to access events in the BaseDataSource (namely: Updated, AfterUpdated, Inserted, AfterInserted). For the life of me I can't figure the method sig to pass in a BaseDataSource, and the IDataSourcEvents interface won't let me tap into the AfterXxxx events. I'm a recently reformed Java programmer and generic classes are frying my brain:)
    • Post Points: 5
  • 10-20-2006 3:33 PM In reply to

    • Evan
    • Top 150 Contributor
    • Joined on 09-19-2006
    • Seattle, WA
    • Posts 30
    • Points 820

    Re: Handling EDS events

    I solved my problem, and I think the solution (and the issueI was trying to solve) might be of issue to at least a few.

    First: the issue. I wanted a generic way to tap into any EntityDataSource and trap Inserted, Updated, AfterInserted, and AfterUpdated events. I was interested in trapping these events so that I could retrieve any BrokenRulesLists that were generated by EntityNotValidExceptions and display messages on a page, as well as handle the corresponding exception. Further, I'm using the ServiceLayer and processors to maintain control over the business rules and workflow.

    The problem I encountered was accessing all of the necessary events from one method parameter. As the origianl poster described, it doesn't seem possible to pass a BaseDataSource<Entity, EntityKey> as a method parameter. I managed a solution by using typed parameters and constraints, like so:

     
     public void RegisterDataSource<D, P>(D ds, P provider)
                where D : SBRILD.Web.Data.IDataSourceEvents, SBRILD.Web.Data.ILinkedDataSource
                where P : SBRILD.Services.IComponentService
            {...}

    This ensures that the first parameter (my EntityDataSource) inherits from the interfaces that provide the events (I'm using the second typed parameter to ensure that the corresponding EDS provider is an IComponentService). I'm a former Java programmer, but this type of stuff is making me love C# (.Net 2.0, at least).

    The end result of all of this will be a nice way to trap BrokenRulesList messages, as well as other custom messages generated in a processor, and display them in a consolidated area on a page. Hope this might help someone!

    Cheers,

    Evan

    • Post Points: 35
  • 10-20-2006 3:40 PM In reply to

    Re: Handling EDS events

    It would really awesome if you could post a full example!

    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 10-20-2006 4:45 PM In reply to

    • Evan
    • Top 150 Contributor
    • Joined on 09-19-2006
    • Seattle, WA
    • Posts 30
    • Points 820

    Re: Handling EDS events

    Hey Robert,

    I'm on it. I've got a rough implementation now that I'll polish up and post early next week.

    Have a great weekend

    Evan
     

    • Post Points: 35
  • 11-06-2006 3:19 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: Handling EDS events

    Hi Evan!

    I'm curious to see your example too.

    JF


     

    • Post Points: 35
  • 11-06-2006 3:36 PM In reply to

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 924
    • Points 34,750

    Re: Handling EDS events

    Me too!

    swin

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 5
  • 11-17-2006 1:33 PM In reply to

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 924
    • Points 34,750

    Re: Handling EDS events

    Evan,

    any updates??

    TIA

    swin

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 35
  • 11-17-2006 1:56 PM In reply to

    Re: Handling EDS events

    It's posted here i believe:
    http://community.codesmithtools.com/forums/permalink/19483/19483/ShowThread.aspx#19483

    swin:

    Evan,

    any updates??

    TIA

    swin


    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 11-20-2006 6:21 PM In reply to

    • Evan
    • Top 150 Contributor
    • Joined on 09-19-2006
    • Seattle, WA
    • Posts 30
    • Points 820

    Re: Handling EDS events

    Robert,

    Thanks for listing that link. My apologies to everyone interested that I didn't post it here...deadlines, deadlines, deadlines!

    Cheers,

    Evan 


     

    • Post Points: 35
  • 11-22-2006 3:49 PM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 734
    • Points 17,040

    Re: Handling EDS events

    Evan,

    Great implementation! I’ve made a tiny enhancement to the class to be able to read broken rules for those business entities that are not used in the business process pipeline.


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI.WebControls;

    using RootNamespace.Web.Data;
    using RootNamespace.Services;
    using RootNamespace.Entities;
    using RootNamespace.Entities.Validation;

    namespace RootNamespace.Web.App_Code
    {
        /// <summary>
        /// This class is used for validation rules across entities
        /// </summary>
        public class EntityDataSourceEventAdapter
        {
            /// <summary>
            /// Is used to register DataSource and attaches Messages Event
            /// </summary>
            /// <typeparam name="D"></typeparam>
            /// <typeparam name="P"></typeparam>
            /// <param name="ds"></param>
            /// <param name="provider"></param>
            public void RegisterDataSource<D, P>(D ds, P provider)
                where D : IDataSourceEvents, ILinkedDataSource
                where P : IComponentService
            {
                MessageAdapter<D, P> ma = new MessageAdapter<D, P>();
                ma.RegisterIndividualDataSource(ds, provider);
                ma.DataSourceMessageEvent += new MessageAdapter<D, P>.DataSourceMessageReport(DataSourceMessageReported);
            }

            /// <summary>
            /// DataSourceMessageReported
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="message"></param>
            void DataSourceMessageReported(Object sender, string message)
            {
                MessageReported(sender, message);
            }

            public delegate void MessageRaised(Object sender, String message);
            public event MessageRaised MessageReported;
        }

        /// <summary>
        /// MessageAdapter
        /// </summary>
        /// <typeparam name="D"></typeparam>
        /// <typeparam name="P"></typeparam>
        public class MessageAdapter<D, P>
            where D : IDataSourceEvents, ILinkedDataSource
            where P : IComponentService
        {
            //Service provider
            private IComponentService service;
            private String messages = String.Empty;

            /// <summary>
            /// Register a datasource's Inserted and Updated events
            /// </summary>
            /// <param name="ds"></param>
            /// <param name="provider"></param>
            public void RegisterIndividualDataSource(D ds, P provider)
            {
                //Register Inserted and AfterInserted event handles
                ds.Inserted += new ObjectDataSourceStatusEventHandler(ds_Inserted);
                //ds.AfterInserted += new LinkedDataSourceEventHandler(ds_AfterInserted);

                //Register Updated and AfterUpdated event handlers
                ds.Updated += new ObjectDataSourceStatusEventHandler(ds_Updated);
                //ds.AfterUpdated += new LinkedDataSourceEventHandler(ds_AfterUpdated);

                //Register service provider
                service = provider;
            }

            /// <summary>
            /// Occurs when a DataSource's Inserted event is fired
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void ds_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
            {
                HandleMessages(sender, e);
            }       

            /// <summary>
            /// Occurs when a DataSource's Updated event is fired
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void ds_Updated(object sender, ObjectDataSourceStatusEventArgs e)
            {
                HandleMessages(sender, e);
            }

            #region Helpers

            /// <summary>
            /// Checks for error messages
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void HandleMessages(object sender, ObjectDataSourceStatusEventArgs e)
            {
                System.Collections.IEnumerable ds = (sender as ILinkedDataSource).GetEntityList();

                messages = GetEntityListErrors(ds);

                //Handle any EntityNotValidExceptions
                if (e.Exception != null && e.Exception.GetType() == typeof(EntityNotValidException))
                {
                    e.ExceptionHandled = true;
                }

                if (messages != String.Empty)
                {
                    DataSourceMessageEvent(sender, messages);
                }
            }

            /// <summary>
            /// Iterates through each of the entity and checks for the Broken Rules in the list
            /// </summary>
            /// <param name="ds"></param>
            /// <returns></returns>
            private string GetEntityListErrors(System.Collections.IEnumerable ds)
            {
                string errors = string.Empty;

                foreach (EntityBase entity in ds)
                {
                    //Get each BrokenRule from the BrokenRulesList
                    foreach (BrokenRule br in entity.BrokenRulesList)
                    {
                        errors += string.Format("{0}\n", br.Description);
                    }
                }

                return errors;
            }

            /// <summary>
            /// Iterates through each of the service's processors and checks for BrokenRules in the List
            /// </summary>
            /// <returns></returns>
            private string GetProcessorErrors()
            {
                string errors = string.Empty;

                //Iterate through each of the service's processors
                foreach (ProcessorBase proc in service.ProcessorList)
                {
                    //Get the result associated with this processor
                    IProcessorResult result = proc.ProcessResult;

                    //Get each BrokenRulesList from the processor
                    foreach (KeyValuePair<Type, BrokenRulesList> kvp in result.BrokenRulesLists)
                    {
                        //Get each BrokenRule from the BrokenRulesList
                        foreach (BrokenRule br in kvp.Value)
                        {
                            //Report broken rule
                            //MessageEvent(br.Description.ToString());
                            errors += string.Format("{0}\n", br.Description);
                        }
                    }

                    //Define an IWarningProcessorResult
                    IWarningProcessorResult warningResult = proc.ProcessResult as IWarningProcessorResult;

                    //If it exists
                    if (warningResult != null)
                    {
                        //Get each warning message in the list
                        foreach (String warning in warningResult.WarningList)
                        {
                            errors += string.Format("{0}\n", warning);
                        }
                    }
                }
                return errors;
            }
            #endregion

            public delegate void DataSourceMessageReport(Object sender, String message);
            public event DataSourceMessageReport DataSourceMessageEvent;
        }

        /// <summary>
        /// The interface that processors implement if they report warnings in addition to the
        /// BrokenRules reported by entity validation
        /// </summary>
        public interface IWarningProcessorResult
        {
            /// <summary>
            /// List of strings describing each warning
            /// </summary>
            List<String> WarningList { get;}
        }
    }
     

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

    • Post Points: 65
  • 02-06-2007 12:43 PM In reply to

    • Evan
    • Top 150 Contributor
    • Joined on 09-19-2006
    • Seattle, WA
    • Posts 30
    • Points 820

    Re: Handling EDS events

    Hi Mike,

    Great and useful addition. Apologies for not getting back sooner...been drowning in work!

    Cheers,

    Evan 

    • Post Points: 5
Page 1 of 2 (16 items) 1 2 Next > | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems