Beispiel #1
0
        private void ApplyMethodWithCaching(object instanceToApply, DomainEvent eventToApply, Dictionary <string, MethodInfo> cache)
        {
            try
            {
                var        eventType = eventToApply.GetType();
                var        localKey  = string.Format("{0},{1}", GetType().FullName, eventType);
                MethodInfo method;

                // Check of the handler (method info) for this event has been cached
                if (cache.ContainsKey(localKey))
                {
                    method = cache[localKey];
                }
                else
                {
                    // Get the convention-based handler
                    method = instanceToApply.GetAppliedEventMethodNamed(eventToApply.GetEventMethodName(), eventType);
                    cache.Add(localKey, method);
                }

                // Call the handler if it exists; otherwise dynamically call "Apply."
                if (method != null)
                {
                    method.Invoke(instanceToApply, new object[] { eventToApply });
                }
                else
                {
                    instanceToApply.AsDynamic().Apply(eventToApply);
                }
            }
            catch (Exception)
            {
                // The entity has no internal handler for the event.  No
                // need to trow an error.
            }
        }