/// <summary> /// Gets the events by I ds. /// </summary> /// <param name="aggregateIDs">The aggregate I ds.</param> /// <returns></returns> public IEnumerable <AggregateTuple <IEnumerable <Event> > > GetEventsByIDs(IEnumerable <AggregateTuple <int> > aggregateIDs) { var xml = new XElement("r", aggregateIDs .Select(x => new XElement("i", new XAttribute("i", x.AggregateID), new XAttribute("s", x.Item1))) ); using (var connection = new SqlConnection(_connectionString)) { var sql = string.Format(@" Select AggregateId, Type, Blob From dbo.[{0}] Inner Join @xml.nodes(N'/r/i') _xml(item) On (AggregateID = _xml.item.value(N'@i', N'nvarchar(100)')) And (EventSequence > _xml.item.value(N'@s', N'int')) Order By AggregateID, EventSequence;", _tableName); var command = new SqlCommand(sql, connection) { CommandType = CommandType.Text }; command.Parameters.AddRange(new[] { new SqlParameter { ParameterName = "@xml", SqlDbType = SqlDbType.Xml, Value = xml.ToString() } }); connection.Open(); var events = new List <AggregateTuple <IEnumerable <Event> > >(); using (var r = command.ExecuteReader()) { var eventGroup = new List <Event>(); string lastAggregateID = null; var ordinal = new EventOrdinal(r, true); while (r.Read()) { var aggregateID = r.Field <string>(ordinal.AggregateID); if (lastAggregateID != aggregateID) { events.Add(new AggregateTuple <IEnumerable <Event> > { AggregateID = lastAggregateID, Item1 = eventGroup, }); lastAggregateID = aggregateID; eventGroup = new List <Event>(); } eventGroup.Add(MakeEvent(r, ordinal)); } if (eventGroup.Count > 0) { events.Add(new AggregateTuple <IEnumerable <Event> > { AggregateID = lastAggregateID, Item1 = eventGroup, }); } } return(events); } }
private Event MakeEvent(SqlDataReader r, EventOrdinal ordinal) { var type = TypeMapTo(r.Field <string>(ordinal.Type)); var blob = r.Field <string>(ordinal.Blob); return(type != null ? _serializer.ReadObject <Event>(type, blob) : null); }
/// <summary> /// Gets the events by ID. /// </summary> /// <param name="aggregateID">The aggregate ID.</param> /// <param name="startSequence">The start sequence.</param> /// <returns></returns> public IEnumerable <Event> GetEventsByID(object aggregateID, int startSequence) { using (var connection = new SqlConnection(_connectionString)) { var sql = string.Format(@" Select Type, Blob From dbo.[{0}] Where (AggregateID = @id) And (EventSequence > @sequence) Order By EventSequence;", _tableName); var command = new SqlCommand(sql, connection) { CommandType = CommandType.Text }; command.Parameters.AddRange(new[] { new SqlParameter { ParameterName = "@id", SqlDbType = SqlDbType.NVarChar, Value = aggregateID }, new SqlParameter { ParameterName = "@sequence", SqlDbType = SqlDbType.Int, Value = startSequence } }); connection.Open(); using (var r = command.ExecuteReader()) { var ordinal = new EventOrdinal(r, false); while (r.Read()) { yield return(MakeEvent(r, ordinal)); } } } }