private static string GetObjectId(object update, IdGetter idGetter, IdSetter idSetter) { string id = idGetter(update); if (idSetter != null && string.IsNullOrEmpty(id)) { id = GenerateId(); idSetter(update, id); } return(id); }
protected EntityCollectionManager (TParent parent, Action <TEntityBaseEventClass> raiseEventThroughParent, IEventHandlerRegistrar <TEntityBaseEventInterface> appliersRegistrar) : base(parent, raiseEventThroughParent, appliersRegistrar) { appliersRegistrar.For <TEntityRemovedEventInterface>( e => { var id = IdGetter.GetId(e); ManagedEntities.Remove(id); }); }
public Collection(TRootQueryModel aggregate) { _aggregate = aggregate; _aggregate.RegisterEventAppliers() .For <TEntityCreatedEventInterface>( e => { var component = (TEntitity)Activator.CreateInstance(typeof(TEntitity), nonPublic: true); component.RootQueryModel = _aggregate; _entities.Add(IdGetter.GetId(e), component); _entitiesInCreationOrder.Add(component); }) .For <TEntityBaseEventInterface>(e => _entities[IdGetter.GetId(e)].ApplyEvent(e)); }
internal void GenerateIdGetter() { if (m_idGetter != null) { return; } var method = new DynamicMethod( "IdGetter_" + m_entryTypeName, typeof(uint), new Type[] { typeof(T) }, typeof(T), true ); var ilgen = method.GetILGenerator(20); ilgen.Emit(OpCodes.Ldarg_0); if (m_fields[0].FieldInfo != null) { ilgen.Emit(OpCodes.Ldfld, m_fields[0].FieldInfo); } else if (m_fields[0].Getter != null) { ilgen.Emit(OpCodes.Callvirt, m_fields[0].Getter); } else { throw new InvalidOperationException(); } if (m_fields[0].DBCTypeId != StoredTypeId.UInt32) { ilgen.Emit(OpCodes.Conv_U4); } ilgen.Emit(OpCodes.Ret); m_idGetter = (IdGetter)method.CreateDelegate(typeof(IdGetter)); }
public bool Update(IdGetter idGetter, object update) { string id = GetObjectId(update, idGetter, null); if (string.IsNullOrEmpty(id)) { throw new ArgumentException("Object to be updated must have a non null/empty Id"); } Row row; if (_rows.TryGetValue(id, out row)) { // can only update items that already exist and don't have any pending // modifications if (row.State != RowState.Unchanged) { return(false); } row.State = RowState.Updating; var change = new JArray(); change.Add("U"); change.Add(Id); change.Add(id); change.Add(new JObject()); if (update is IDictionary <string, object> ) { var dict = (update as IDictionary <string, object>); foreach (var field in dict) { object fieldValue = field.Value; JToken value = fieldValue != null?SerializeValue(fieldValue) : null; var operations = DetermineOperations(row.Data, field.Key, value); change = AddPendingOperations(field.Key, value, operations, change); } } else { foreach (var field in update.GetType().GetFields()) { if (field.GetCustomAttributes(typeof(IgnoreAttribute), true).Length == 0) { object fieldValue = field.GetValue(update); JToken value = fieldValue != null?SerializeValue(fieldValue) : null; var operations = DetermineOperations(row.Data, field.Name, value); change = AddPendingOperations(field.Name, value, operations, change); } } foreach (var prop in update.GetType().GetProperties()) { // we are only interested in read/writable fields if (prop.GetCustomAttributes(typeof(IgnoreAttribute), true).Length == 0 && prop.CanRead && prop.CanWrite) { object propValue = prop.GetValue(update, null); var value = propValue != null?SerializeValue(propValue) : null; var operations = DetermineOperations(row.Data, prop.Name, value); change = AddPendingOperations(prop.Name, value, operations, change); } } } if (change.Last.HasValues) { _pendingChanges.Add(change); } return(true); } return(false); }
public bool Insert(IdGetter idGetter, IdSetter idSetter, object insert) { string id = GetObjectId(insert, idGetter, idSetter); if (string.IsNullOrEmpty(id)) { throw new ArgumentException("Object to be inserted must have a non null/empty Id"); } Row row; if (_rows.TryGetValue(id, out row)) { // can't insert something unless it doesn't exist // or has just been deleted if (row.State != RowState.Deleting) { return(false); } } else { row = new Row(); _rows.Add(id, row); } row.State = RowState.Inserting; var change = new JArray(); change.Add("I"); change.Add(Id); change.Add(id); JObject data = new JObject(); if (insert is IDictionary <string, object> ) { var dict = (insert as IDictionary <string, object>); foreach (var field in dict) { object fieldValue = field.Value; if (fieldValue != null) { data[field.Key] = SerializeValue(fieldValue); } } } else { foreach (var field in insert.GetType().GetFields()) { if (field.GetCustomAttributes(typeof(IgnoreAttribute), true).Length == 0) { object fieldValue = field.GetValue(insert); if (fieldValue != null) { data[field.Name] = SerializeValue(fieldValue); } } } foreach (var prop in insert.GetType().GetProperties()) { if (prop.GetCustomAttributes(typeof(IgnoreAttribute), true).Length == 0) { object propValue = prop.GetValue(insert, null); // we are only interested in read/writable fields if (prop.CanRead && prop.CanWrite && propValue != null) { data[prop.Name] = SerializeValue(propValue); } } } } change.Add(data); _pendingChanges.Add(change); return(true); }