static void StoreEventProvider(IEventProvider eventProvider, SqlTransaction transaction) { const string commandText = "INSERT INTO EventProvider(EventProviderId, Type) VALUES(@eventProviderId, @type)"; using (var command = new SqlCommand(commandText, transaction.Connection, transaction)) { command.Parameters.Add(new SqlParameter("@eventProviderId", eventProvider.Id)); command.Parameters.Add(new SqlParameter("@type", eventProvider.GetType().FullName)); command.ExecuteNonQuery(); } }
private void LoadRemainingHistoryEvents(Guid id, IEventProvider <TDomainEvent> aggregateRoot) { var events = _domainEventStorage.GetEventsSinceLastSnapShot(id, aggregateRoot.GetType()); var domainEvents = events as TDomainEvent[] ?? events.ToArray(); if (domainEvents.Any()) { aggregateRoot.LoadFromHistory(domainEvents); return; } aggregateRoot.LoadFromHistory(_domainEventStorage.GetAllEvents(id)); }
private static int GetEventProviderVersion(IEventProvider eventProvider, SQLiteTransaction transaction) { const string commandText = @" INSERT OR IGNORE INTO EventProviders VALUES (@eventProviderId, @type, 0); SELECT Version FROM EventProviders WHERE EventProviderId = @eventProviderId"; using (var sqLiteCommand = new SQLiteCommand(commandText, transaction.Connection, transaction)) { sqLiteCommand.Parameters.Add(new SQLiteParameter("@eventProviderId", eventProvider.Id)); sqLiteCommand.Parameters.Add(new SQLiteParameter("@type", eventProvider.GetType().FullName)); sqLiteCommand.Parameters.Add(new SQLiteParameter("@version", eventProvider.Version)); return(Convert.ToInt32(sqLiteCommand.ExecuteScalar())); } }
/// <summary> /// Registers the specified object with the Event Manager. /// </summary> /// <param name="registrant">The object to register.</param> /// <param name="registeredProviders">The Dictionary, keyed on Type, of registered Event Provider instances.</param> /// <param name="registeredEvents">The Dictionary, keyed on Type, of registered Events.</param> /// <returns>A Result containing the result of the operation.</returns> private Result RegisterProvider(object registrant, Dictionary <Type, List <string> > registeredProviders, Dictionary <Type, List <KeyValuePair <string, string> > > registeredEvents) { logger.EnterMethod(xLogger.Params(registrant.GetType(), new xLogger.ExcludedParam(), new xLogger.ExcludedParam())); logger.Debug("Registering object of Type '" + registrant.GetType().Name + "..."); Result retVal = new Result(); // check to ensure the provided object implements IEventProvider logger.Trace("Checking to see if the registrant implements IEventProvider..."); if (!(registrant is IEventProvider)) { return(retVal.AddError("The specified object does not implement IEventProvider and can not be registered.")); } logger.Trace("Casting the registrant object to IEventProvider..."); IEventProvider provider = (IEventProvider)registrant; // check to see if the specified type has previously been registered logger.Trace("Checking the registration of the Type and event list..."); if (registeredProviders.ContainsKey(provider.GetType())) { logger.Trace("The Type '" + provider.GetType() + "' is registered. Checking the instance name..."); // check to see if the type contains the specified provider instance if (registeredProviders[provider.GetType()].Contains(provider.EventProviderName)) { // if it does, exit. return(retVal.AddError("The provider '" + provider.EventProviderName + "' has already been registered.")); } else { // if not, add it. logger.Trace("The instance '" + provider.EventProviderName + "' isn't registered. Adding it to the list..."); registeredProviders[provider.GetType()].Add(provider.EventProviderName); } } else { // if the provider dictionary doesn't contain the Type, add the key and initialize the provider list with this // provider name. logger.Trace("The Type '" + provider.GetType() + "' hasn't yet been registered. Adding it to the Dictionary with the instance '" + provider.EventProviderName + "'..."); List <string> list = new List <string>(); list.Add(provider.EventProviderName); registeredProviders.Add(provider.GetType(), list); } // next, add the Type to the event dictionary. check to make sure it doesn't exist, just in case. it shouldn't. logger.Trace("Checking whether the events for '" + provider.GetType() + "' have been registered..."); if (!registeredEvents.ContainsKey(provider.GetType())) { logger.Trace("Adding the Type '" + provider.GetType() + "' to the event Dictionary with an empty list..."); registeredEvents.Add(provider.GetType(), new List <KeyValuePair <string, string> >()); } else { logger.Trace("Type '" + provider.GetType() + "' is already registered; no changes made to the event Dictionary."); } // at this point we have ensured that the type and instance exist in both the provider and event dictionaries. next we // will iterate over the events in the specified Type, attach our generic handler and, if an event is missing from the // registered events dictionary, we will add it. begin by retrieving a list of events for the specified Type logger.Trace("Retrieving list of events from the Type '" + provider.GetType() + "'..."); EventInfo[] events = provider.GetType().GetEvents(BindingFlags.Instance | BindingFlags.Public); // iterate over each event. add the event to the list of registered events for the Type if it doesn't yet exist, then // attach our generic event handler. logger.Debug("The Type '" + provider.GetType().Name + "' contains " + events.Length + " events. Enumerating..."); foreach (EventInfo eventInfo in events) { // attempt to fetch the description from the Event attribute that *should* be attached to the event. logger.Trace("Attempting to fetch the description for event '" + eventInfo.Name + "'..."); string description = string.Empty; EventAttribute eventAttribute = (EventAttribute)eventInfo.GetCustomAttributes().Where(a => a is EventAttribute).FirstOrDefault(); if (eventAttribute != default(Attribute)) { // the attribute was attached; grab the description. description = eventAttribute.Description; logger.Trace("Fetched '" + description + "'."); } else { logger.Trace("Event attribute was not attached to the event '" + eventInfo.Name + "; skipping registration."); continue; } logger.Debug("Processing event '" + eventInfo.Name + "' with description '" + description + "'..."); // create a new key value pair for the current event KeyValuePair <string, string> currentEvent = new KeyValuePair <string, string>(eventInfo.Name, description); // check to see if the event has been registered and add it if it hasn't. if (!registeredEvents[provider.GetType()].Exists(k => k.Key == currentEvent.Key)) { logger.Trace("Adding event '" + eventInfo.Name + "' to the event dictionary..."); registeredEvents[provider.GetType()].Add(currentEvent); } // attach the generic event handler to the event logger.Trace("Attaching event handler to '" + eventInfo.Name + "'..."); eventInfo.AddEventHandler(provider, Delegate.CreateDelegate(eventInfo.EventHandlerType, this, GetType().GetMethod("ProviderEventRaised", BindingFlags.NonPublic | BindingFlags.Instance))); } retVal.LogResult(logger.Debug); logger.ExitMethod(retVal); return(retVal); }
private static string CreateMessage(IEventProvider<IDomainEvent> eventProvider) { return string.Format("The AggregateRoot {0} of Type {1} was changed",eventProvider.Id,eventProvider.GetType().FullName); }
private static void CreateEventProvider(IEventProvider provider, SqlTransaction transaction) { using (var command = new SqlCommand(InsertNewProviderQuery, transaction.Connection)) { command.Transaction = transaction; command.Parameters.AddWithValue("Id", provider.Id); command.Parameters.AddWithValue("Type", provider.GetType().ToString()); command.Parameters.AddWithValue("Version", provider.Version); command.ExecuteNonQuery(); } }
private static string CreateMessage(IEventProvider <IDomainEvent> eventProvider) { return(string.Format("The AggregateRoot {0} of Type {1} was changed", eventProvider.Id, eventProvider.GetType().FullName)); }
private static int GetVersion(IEventProvider eventProvider) { const string selectOrInsertEventsource = @" INSERT IGNORE INTO EventSource VALUES (@id, @name, @type, 0); SELECT Version FROM EventSource WHERE Id = @id"; var aggregateType = eventProvider.GetType(); using (var connection = new MySqlConnection(connectionString)) { using (var command = connection.CreateCommand()) { command.CommandText = selectOrInsertEventsource; command.Parameters.Add("@id", MySqlDbType.Guid).Value = eventProvider.Id; command.Parameters.Add("@name", MySqlDbType.VarChar).Value = aggregateType.Name; command.Parameters.Add("@type", MySqlDbType.VarChar).Value = aggregateType.FullName; connection.Open(); var queryResult = command.ExecuteScalar(); var version = Convert.ToInt32(queryResult); return version; } } }