| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888 | using System.Collections.Concurrent;using System.Reflection;using System.Text.RegularExpressions;using InABox.Core;using NPOI.POIFS.FileSystem;namespace InABox.Database{    public static class UserTrackingCache    {        public static ConcurrentBag<UserTracking> Cache = new();        public static DateTime Date = DateTime.MinValue;    }    // public interface ICacheStore<T>    // {    //     void LoadCache();    //     T GetCacheItem();    //     void UpdateCache();    // }    public class Store<T> : IStore, IStore<T> where T : Entity, new()    {        public bool IsSubStore { get; set; }        public Guid UserGuid { get; set; }        public string UserID { get; set; }        public string Platform { get; set; }        public string Version { get; set; }        public IProvider Provider { get; set; }        public virtual void Init()        {                    }                private Type GetTrackingType(Type type)        {            var attr = type.GetCustomAttribute<UserTrackingAttribute>();            if (attr == null)                return type;            if (!attr.Enabled)                return null;            if (attr.Parent != null)                return GetTrackingType(attr.Parent);            return type;        }        private void UpdateUserTracking(UserTrackingAction action)        {            if (IsSubStore)                return;            if (!DbFactory.IsSupported<UserTracking>())                return;            if (string.IsNullOrWhiteSpace(UserID))                return;            var type = GetTrackingType(typeof(T));            if (type == null)                return;            if (UserTrackingCache.Date != DateTime.Today)            {                var tdata = Provider.Query(                    new Filter<UserTracking>(x => x.Date).IsEqualTo(DateTime.Today),                    new Columns<UserTracking>().Default(ColumnType.IncludeForeignKeys)                );                UserTrackingCache.Cache = new ConcurrentBag<UserTracking>(tdata.Rows.Select(x => x.ToObject<UserTracking>()));                UserTrackingCache.Date = DateTime.Today;            }            var tracking = UserTrackingCache.Cache.FirstOrDefault(x =>                Equals(x.User.ID, UserGuid) && DateTime.Equals(x.Date, DateTime.Today) && string.Equals(x.Type, type.Name));            if (tracking == null)            {                tracking = new UserTracking();                tracking.Date = DateTime.Today;                tracking.Type = type.Name;                tracking.User.ID = UserGuid;                UserTrackingCache.Cache.Add(tracking);            }            tracking.Increment(DateTime.Now, action);            Provider.Save(tracking);        }        public IStore FindSubStore(Type t)        {            var defType = typeof(Store<>).MakeGenericType(t);            var subType = DbFactory.Stores.Where(myType => myType.IsSubclassOf(defType)).FirstOrDefault();            var result = (IStore)Activator.CreateInstance(subType == null ? defType : subType);            result.UserGuid = UserGuid;            result.UserID = UserID;            result.Platform = Platform;            result.Version = Version;            result.IsSubStore = true;            result.Provider = Provider;            return result;        }        public IStore<TEntity> FindSubStore<TEntity>() where TEntity : Entity, new()        {            var defType = typeof(Store<>).MakeGenericType(typeof(TEntity));            var subType = DbFactory.Stores.Where(myType => myType.IsSubclassOf(defType)).FirstOrDefault();            var store = (Store<TEntity>)Activator.CreateInstance(subType == null ? defType : subType);            store.UserGuid = UserGuid;            store.UserID = UserID;            store.Platform = Platform;            store.Version = Version;            store.IsSubStore = true;            store.Provider = Provider;            return store;        }        private Filter<T>? RunScript(ScriptType type, Filter<T>? filter)        {            var scriptname = type.ToString();            var key = string.Format("{0} {1}", typeof(T).EntityName(), scriptname);            if (DbFactory.LoadedScripts.ContainsKey(key))            {                var script = DbFactory.LoadedScripts[key];                Logger.Send(LogType.Information, UserID, string.Format("{0}.{1} Executing..", typeof(T).EntityName(), scriptname));                try                {                    script.SetValue("Store", this);                    script.SetValue("Filter", filter);                    var result = script.Execute();                    Logger.Send(LogType.Information, UserID, string.Format("{0}.{1} returns {2}", typeof(T).EntityName(), scriptname, result));                    return result ? script.GetValue("Filter") as Filter<T> : filter;                }                catch (Exception eExec)                {                    Logger.Send(LogType.Information, UserID,                        string.Format("{0}.{1} Invoke Exception: {2}", typeof(T).EntityName(), scriptname, eExec.Message));                }            }            return filter;        }        private IEnumerable<T> RunScript(ScriptType type, IEnumerable<T> entities)        {            var scriptname = type.ToString();            var variable = typeof(T).EntityName().Split('.').Last() + "s";            var key = string.Format("{0} {1}", typeof(T).EntityName(), scriptname);            if (DbFactory.LoadedScripts.ContainsKey(key))            {                var script = DbFactory.LoadedScripts[key];                script.SetValue("Store", this);                script.SetValue(variable, entities);                Logger.Send(LogType.Information, UserID, string.Format("{0}.{1} Executing..", typeof(T).EntityName(), scriptname));                foreach (var entity in entities)                    try                    {                        var result = script.Execute();                        Logger.Send(LogType.Information, UserID,                            string.Format("{0}.{1} returns {2}: {3}", typeof(T).EntityName(), scriptname, result, entity));                        return result ? script.GetValue(variable) as IEnumerable<T> : entities;                    }                    catch (Exception eExec)                    {                        var stack = new List<string>();                        var eStack = eExec;                        while (eStack != null)                        {                            stack.Add(eStack.Message);                            eStack = eStack.InnerException;                        }                        stack.Reverse();                        var message = string.Join("\n", stack);                        Logger.Send(LogType.Information, UserID,                            string.Format("{0}.{1} Invoke Exception: {2}", typeof(T).EntityName(), scriptname, message));                    }            }            return entities;        }        private CoreTable RunScript(ScriptType type, CoreTable table)        {            var scriptname = type.ToString();            var variable = typeof(T).EntityName().Split('.').Last() + "s";            var key = string.Format("{0} {1}", typeof(T).EntityName(), scriptname);            if (DbFactory.LoadedScripts.ContainsKey(key))            {                var script = DbFactory.LoadedScripts[key];                Logger.Send(LogType.Information, UserID, string.Format("{0}.{1} Executing..", typeof(T).EntityName(), scriptname));                try                {                    script.SetValue("Store", this);                    script.SetValue(variable, table);                    var result = script.Execute();                    Logger.Send(LogType.Information, UserID,                        string.Format("{0}.{1} returns {2}: {3}", typeof(T).EntityName(), scriptname, result, table));                    return result ? script.GetValue(variable) as CoreTable : table;                }                catch (Exception eExec)                {                    Logger.Send(LogType.Information, UserID,                        string.Format("{0}.{1} Invoke Exception: {2}", typeof(T).EntityName(), scriptname, eExec.Message));                }            }            return table;        }        //#region Session / Transaction Handling        //void //OpenSession(String action, bool write)        //{        //    if (!IsSubStore)        //        Provider.OpenSession<T>(action, write);        //}        //void //CloseSession(String action, bool write)        //{        //    if (!IsSubStore)        //        Provider.CloseSession<T>(action, write);        //}        //#endregion        #region List Functions        private IEnumerable<object[]> DoList(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null)        {            UpdateUserTracking(UserTrackingAction.Read);            //last = DateTime.Now;            //OpenSession("List", false);            //LogStep("OpenSession");            try            {                var flt = PrepareFilter(filter);                flt = RunScript(ScriptType.BeforeQuery, flt);                var result = Provider.List(flt, columns, sort);                //LogStep("PopulateTable");                AfterList(result);                //CloseSession("List", false);                //LogStep("CloseSession");                return result;            }            catch (Exception e)            {                //CloseSession("List", false);                throw new Exception(e.Message + "\n\n" + e.StackTrace + "\n");            }        }        public IEnumerable<object[]> List(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null)        {            return DoList(filter, columns, sort);        }        public IEnumerable<object[]> List(Filter<Entity>? filter = null, Columns<Entity>? columns = null, SortOrder<Entity>? sort = null)        {            return DoList((Filter<T>?)filter, (Columns<T>)columns, (SortOrder<T>)sort);        }        protected virtual void AfterList(IEnumerable<object[]> data)        {        }        #endregion        #region Query Functions        protected virtual CoreTable OnQuery(Filter<T>? filter, Columns<T>? columns, SortOrder<T>? sort)        {            return Provider.Query(filter, columns, sort);        }                private CoreTable DoQuery(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null)        {            UpdateUserTracking(UserTrackingAction.Read);            //last = DateTime.Now;            //OpenSession("Query", false);            //LogStep("OpenSession");            try            {                var flt = PrepareFilter(filter);                flt = RunScript(ScriptType.BeforeQuery, flt);                var result = OnQuery(filter, columns, sort);                //LogStep("PopulateTable");                AfterQuery(result);                result = RunScript(ScriptType.AfterQuery, result);                //CloseSession("Query", false);                //LogStep("CloseSession");                return result;            }            catch (Exception e)            {                //CloseSession("Query", false);                throw new Exception(e.Message + "\n\n" + e.StackTrace + "\n");            }        }        public CoreTable Query(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null)        {            return DoQuery(filter, columns, sort);        }        public CoreTable Query(Filter<Entity>? filter = null, Columns<Entity>? columns = null, SortOrder<Entity>? sort = null)        {            return DoQuery((Filter<T>?)filter, (Columns<T>)columns, (SortOrder<T>)sort);        }        protected virtual void AfterQuery(CoreTable data)        {        }        #endregion        #region Load Functions        protected virtual Filter<T>? PrepareFilter(Filter<T>? filter)        {            return filter;        }        private T[] DoLoad(Filter<T>? filter = null, SortOrder<T>? sort = null)        {            UpdateUserTracking(UserTrackingAction.Read);            //OpenSession("Load", false);            T[] results = null;            try            {                var flt = PrepareFilter(filter);                flt = RunScript(ScriptType.BeforeQuery, flt);                results = Provider.Load(flt, sort);                AfterLoad(results);                //CloseSession("Load", false);                results = RunScript(ScriptType.AfterLoad, results) as T[];                return results;            }            catch (Exception e)            {                //CloseSession("Load", false);                throw new Exception(e.Message + "\n\n" + e.StackTrace + "\n");            }        }        public Entity[] Load(Filter<Entity>? filter = null, SortOrder<Entity>? sort = null)        {            return DoLoad((Filter<T>?)filter, sort != null ? (SortOrder<T>)sort : null);        }        public T[] Load(Filter<T>? filter = null, SortOrder<T>? sort = null)        {            return DoLoad(filter, sort);        }        protected virtual void AfterLoad(IEnumerable<T> items)        {            foreach (var item in items)                item.SetObserving(true);        }        #endregion        #region Saving Functions        private static readonly Regex IsNumeric = new(@"^\d+$");        private void CheckAutoIncrement(params T[] entities)        {            if (ProcessNumericAutoInc(entities))                return;            ProcessStringAutoIncrement(entities);        }        public static string AutoIncrementPrefix { get; set; }                private bool ProcessStringAutoIncrement(params T[] entities)        {            if (!entities.Any())                return false;            var autoinc = entities.First() as IStringAutoIncrement<T>;            if (autoinc != null)            {                var prop = CoreUtils.GetPropertyFromExpression<T, string>(autoinc.AutoIncrementField());                var bRequired = false;                foreach (var entity in entities)                    bRequired = bRequired || string.IsNullOrWhiteSpace(prop.GetValue(entity) as string);                if (bRequired)                {                                        var filter = new Filter<T>(prop.Name).IsGreaterThanOrEqualTo(String.Format("{0}0", AutoIncrementPrefix))                        .And(prop.Name).IsLessThanOrEqualTo(String.Format("{0}9999999999", AutoIncrementPrefix));                                        var filter2 = autoinc.AutoIncrementFilter();                    if (filter2 != null)                        filter = filter.And(filter2);                    // if (!string.IsNullOrWhiteSpace(AutoIncrementPrefix))                    // {                    //     var prefixfilter = new Filter<T>(prop.Name).BeginsWith(AutoIncrementPrefix);                    //     filter = filter == null ? prefixfilter : filter.And(prefixfilter);                    // }                    var newvalue = 0;                    var row = Provider.Query(                        filter,                        new Columns<T>(new[] { prop.Name }),                        new SortOrder<T>(prop.Name, SortDirection.Descending),                        1                    ).Rows.FirstOrDefault();                    if (row != null)                    {                        var id = row.Get<string>(prop.Name);                        if (!string.IsNullOrWhiteSpace(AutoIncrementPrefix))                            id = id.Substring(AutoIncrementPrefix.Length);                        id = new string(id.Where(c => char.IsDigit(c)).ToArray());                        int.TryParse(id, out newvalue);                    }                    foreach (var entity in entities)                        if (string.IsNullOrWhiteSpace(prop.GetValue(entity) as string))                        {                            newvalue++;                            prop.SetValue(entity, AutoIncrementPrefix + string.Format(autoinc.AutoIncrementFormat(), newvalue));                        }                    return true;                }            }            return false;        }                private bool ProcessNumericAutoInc(params T[] entities)        {            if (!entities.Any())                return false;            var autoinc = entities.First() as INumericAutoIncrement<T>;            if (autoinc != null)            {                var prop = CoreUtils.GetPropertyFromExpression(autoinc.AutoIncrementField());                                var bRequired = false;                foreach (var entity in entities)                    bRequired = bRequired || prop.GetValue(entity).Equals(0);                if (bRequired)                {                                        var row = Provider.Query(                        autoinc.AutoIncrementFilter(),                         new Columns<T>(new[] { prop.Name }),                         new SortOrder<T>(prop.Name,SortDirection.Descending),                        1                    ).Rows.FirstOrDefault();                    int newvalue = row != null ? row.Get<int>(prop.Name) : 0;                    foreach (var entity in entities)                    {                        if (prop.GetValue(entity).Equals(0))                        {                            newvalue++;                            prop.SetValue(entity, newvalue);                        }                    }                    return true;                }            }            return false;        }        protected virtual void BeforeSave(T entity)        {            // Check for (a) blank code fields and (b) duplicate codes            // There may be more than one code on an entity (why would you do this?)            // so it's a bit trickier that I would hope            Filter<T> codes = null;            var columns = new Columns<T>(x => x.ID);            var props = CoreUtils.PropertyList(typeof(T), x => x.GetCustomAttributes<UniqueCodeEditor>().Any(), true);            foreach (var key in props.Keys)                if (entity.HasOriginalValue(key) || entity.ID == Guid.Empty)                {                    var code = CoreUtils.GetPropertyValue(entity, key) as string;                    if (string.IsNullOrWhiteSpace(code))                        throw new NullCodeException(typeof(T), key);                    var expr = CoreUtils.GetPropertyExpression<T, object>(key); //CoreUtils.GetMemberExpression(typeof(T),key)                    codes = codes == null ? new Filter<T>(expr).IsEqualTo(code) : codes.Or(expr).IsEqualTo(code);                    columns.Add(key);                }            if (codes != null)            {                var filter = new Filter<T>(x => x.ID).IsNotEqualTo(entity.ID);                filter = filter.And(codes);                var others = Provider.Query(filter, columns);                var duplicates = new Dictionary<string, object>();                foreach (var row in others.Rows)                foreach (var key in props.Keys)                {                    var eval = CoreUtils.GetPropertyValue(entity, key);                    var cval = row.Get<string>(key);                    if (Equals(eval, cval))                        duplicates[key] = eval;                }                if (duplicates.Any())                    throw new DuplicateCodeException(typeof(T), duplicates);            }        }        protected virtual void AfterSave(T entity)        {        }        protected virtual void OnSave(T entity, ref string auditnote)        {            CheckAutoIncrement(entity);            Provider.Save(entity);        }        private void DoSave(T entity, string auditnote)        {            UpdateUserTracking(UserTrackingAction.Write);            entity = RunScript(ScriptType.BeforeSave, new[] { entity }).First();            //OpenSession("Save", true);            //try            //{            var changes = entity.ChangedValues();            //UpdateInternalLinks(entity);            // Process any AutoIncrement Fields before we apply the Unique Code test            // Thus, if we have a unique autoincrement, it will be populated prior to validation            CheckAutoIncrement(entity);            BeforeSave(entity);            //OpenSession("Save", true);            try            {                OnSave(entity, ref auditnote);            }            catch (Exception e)            {                //CloseSession("Save", true);                throw e;            }            //CloseSession("Save", true);            if (DbFactory.IsSupported<AuditTrail>())            {                var notes = new List<string>();                if (!string.IsNullOrEmpty(auditnote))                    notes.Add(auditnote);                if (!string.IsNullOrEmpty(changes))                    notes.Add(changes);                if (notes.Any()) AuditTrail(entity, notes);            }            AfterSave(entity);            //UpdateExternalLinks(entity, false);            entity = RunScript(ScriptType.AfterSave, new[] { entity }).First();            //entity.CommitChanges();            //CloseSession("Save", false);            //}            //catch (Exception e)            //{            //	//CloseSession("Save", false);            //    throw new Exception(e.Message + "\n\n" + e.StackTrace + "\n");            //}        }        protected void AuditTrail(IEnumerable<Entity> entities, IEnumerable<string> notes)        {            var updates = new List<AuditTrail>();            foreach (var entity in entities)            {                var audit = new AuditTrail                {                    EntityID = entity.ID,                    Timestamp = DateTime.Now,                    User = UserID,                    Note = string.Join(": ", notes)                };                updates.Add(audit);            }            Provider.Save(updates);        }        protected void AuditTrail(Entity entity, IEnumerable<string> notes)        {            AuditTrail(new[] { entity }, notes);        }        public void Save(T entity, string auditnote)        {            DoSave(entity, auditnote);        }        public void Save(Entity entity, string auditnote)        {            var ent = (T)entity;            DoSave(ent, auditnote);        }        public void Save(IEnumerable<T> entities, string auditnote)        {            DoSave(entities, auditnote);        }        public void Save(IEnumerable<Entity> entities, string auditnote)        {            var updates = new List<T>();            foreach (var entity in entities)                updates.Add((T)entity);            DoSave(updates, auditnote);        }        protected virtual void OnSave(IEnumerable<T> entities, ref string auditnote)        {            CheckAutoIncrement(entities.ToArray());            Provider.Save(entities);        }        private void DoSave(IEnumerable<T> entities, string auditnote)        {            UpdateUserTracking(UserTrackingAction.Write);            entities = RunScript(ScriptType.BeforeSave, entities.ToList());            //OpenSession("Save", true);            //try            //{            // Process any AutoIncrement Fields before we apply the Unique Code test            // Thus, if we have a unique autoincrement, it will be populated prior to validation            CheckAutoIncrement(entities.ToArray());            var changes = new Dictionary<T, string>();            foreach (var entity in entities)            {                changes[entity] = entity.ChangedValues();                //UpdateInternalLinks(entity);                BeforeSave(entity);            }            try            {                //OpenSession("Save", true);                OnSave(entities, ref auditnote);                //CloseSession("Save", true);            }            catch (Exception e)            {                //CloseSession("Save", true);                throw e;            }            if (DbFactory.IsSupported<AuditTrail>())            {                var audittrails = new List<AuditTrail>();                foreach (var entity in entities)                {                    var notes = new List<string>();                    if (!string.IsNullOrEmpty(auditnote))                        notes.Add(auditnote);                    if (changes.ContainsKey(entity) && !string.IsNullOrEmpty(changes[entity]))                        notes.Add(changes[entity]);                    if (notes.Any())                    {                        var audit = new AuditTrail                        {                            EntityID = entity.ID,                            Timestamp = DateTime.Now,                            User = UserID,                            Note = string.Join(": ", notes)                        };                        audittrails.Add(audit);                        //Provider.Save<AuditTrail>(audit);                    }                }                if (audittrails.Any())                    Provider.Save(audittrails);            }            foreach (var entity in entities)            {                AfterSave(entity);                //UpdateExternalLinks(entity, false);                //entity.CommitChanges();            }            entities = RunScript(ScriptType.AfterSave, entities);            //}            //catch (Exception e)            //{            //    throw e;            //}        }        #endregion        #region Delete Functions        protected virtual void BeforeDelete(T entity)        {        }        protected virtual void OnDelete(T entity)        {            Provider.Delete(entity, UserID);        }        protected virtual void OnDelete(IEnumerable<T> entities)        {            Provider.Delete(entities, UserID);        }        private void DoDelete(T entity, string auditnote)        {            UpdateUserTracking(UserTrackingAction.Write);            entity = RunScript(ScriptType.BeforeDelete, new[] { entity }).First();            //OpenSession("Delete",false);            try            {                BeforeDelete(entity);                //OpenSession("Delete",true);                try                {                    OnDelete(entity);                }                catch (Exception e)                {                    Logger.Send(LogType.Error, "", $"Error in DoDelete(T entity, string auditnote):\n{CoreUtils.FormatException(e)}");                    //CloseSession("Delete", true);                    //throw e;                }                //CloseSession("Delete",true);                AfterDelete(entity);                //UpdateExternalLinks(entity, true);                //CloseSession("Delete", false);                entity = RunScript(ScriptType.AfterDelete, new[] { entity }).First();            }            catch (Exception e)            {                //CloseSession("Delete", false);                throw new Exception(e.Message + "\n\n" + e.StackTrace + "\n");            }        }        private void DoDelete(IEnumerable<T> entities, string auditnote)        {            UpdateUserTracking(UserTrackingAction.Write);            entities = RunScript(ScriptType.BeforeDelete, entities);            //OpenSession("Delete", false);            try            {                foreach (var entity in entities)                    BeforeDelete(entity);                //OpenSession("Delete", true);                try                {                    OnDelete(entities);                }                catch (Exception e)                {                    Logger.Send(LogType.Error, "", $"Error in DoDelete(IEnumerable<T> entities, string auditnote):\n{CoreUtils.FormatException(e)}");                    ////CloseSession("Delete", true);                }                ////CloseSession("Delete", true);                foreach (var entity in entities) AfterDelete(entity);                //UpdateExternalLinks(entity, true);                ////CloseSession("Delete", false);                entities = RunScript(ScriptType.AfterDelete, entities);            }            catch (Exception e)            {                ////CloseSession("Delete", false);                throw new Exception(e.Message + "\n\n" + e.StackTrace + "\n");            }        }        public void Delete(T entity, string auditnote)        {            DoDelete(entity, auditnote);        }        public void Delete(Entity entity, string auditnote)        {            DoDelete((T)entity, auditnote);        }        public void Delete(IEnumerable<T> entities, string auditnote)        {            DoDelete(entities, auditnote);        }        public void Delete(IEnumerable<Entity> entities, string auditnote)        {            var updates = new List<T>();            foreach (var entity in entities)                updates.Add((T)entity);            DoDelete(updates, auditnote);        }        protected virtual void AfterDelete(T entity)        {        }        #endregion        #region BulkUpdate Functions        public void BulkUpdate(IEnumerable<Entity> entities)        {            BulkUpdate((IEnumerable<T>)entities);        }        public virtual void BulkUpdate(IEnumerable<T> entities)        {            UpdateUserTracking(UserTrackingAction.Write);            Provider.Save(entities);        }        #endregion    }}
 |