public void CommitEntity(ref object entity, UpdateAction action, Type entityType) { EntityUpdater update = new EntityUpdater((EntityBase)entity, action); MethodInfo[] methods = GetType().GetMethods(); foreach (MethodInfo mi in methods) { if (mi.Name == "CommitEntity" && mi.IsGenericMethod && mi.GetParameters().Length == 1) { MethodInfo genericMi = mi.MakeGenericMethod(new Type[] { entityType }); genericMi.Invoke(this, new object[] { update }); break; } } entity = update.Entity; }
// This is a bit clumsy. We only need // this to be templated because of the refetch. public void CommitEntity <T>(ref EntityUpdater adapter, bool refetch) where T : EntityBase, new() { if (refetch) { throw new NotImplementedException("Refetching an entity is not yet supported"); } SqlCommand cmd = null; switch (adapter.Action) { case UpdateAction.Delete: cmd = adapter.GetDeleteCommand(); break; case UpdateAction.Insert: cmd = adapter.GetInsertCommand(); break; case UpdateAction.Update: cmd = adapter.GetUpdateCommand(); break; } // TODO: // If any of the GetXXXCommand methods // can't build a suitable command then they return // null. Instead of ignoring the update, we should // probably throw a suitable error message. if (null != cmd) { cmd.Connection = m_Connection; if (m_Connection.State == ConnectionState.Closed) { m_Connection.Open(); } try { cmd.ExecuteNonQuery(); adapter.RefreshEntityAutoIncrementValue(cmd); if (refetch) { // TODO: // EntityUpdater.Entity property should be readonly // to enforce rules within EntityUpdater. // Is a refetch even necessary if we've already // taken care of IDENTITY fields? //adapter.Entity = // FetchOneEntity( // new EntityFetcher<T>(adapter.Entity.GetIdentifierDbField() == adapter.Entity.GetValueForDbField(adapter.Entity.GetIdentifierDbField()))); } } finally { if (m_OwnsConnection && m_Connection != null && m_Connection.State != ConnectionState.Closed) { m_Connection.Close(); } } cmd.Dispose(); } }
public void CommitEntity <T>(ref EntityUpdater adapter) where T : EntityBase, new() { CommitEntity <T>(ref adapter, false); }