Exemple #1
0
        /// <summary>
        /// Extract the information from the supplied exception
        /// and stores the information as a new ErrorLog
        /// </summary>
        public static ErrorLog Logg <T>(Exception e, bool suppressMessage = false)
        {
            if (ExceptionHandler.HasCaught(e))
            {
                return(ExceptionHandler.GetErrorLog(e) as ErrorLog);
            }

            var error = new ErrorLog
            {
                UserName   = DwarfContext <T> .GetConfiguration().UserService.CurrentUser != null ? DwarfContext <T> .GetConfiguration().UserService.CurrentUser.UserName : "",
                TimeStamp  = DateTime.Now,
                Message    = e.Message,
                Type       = e.GetType().ToString(),
                StackTrace = e.StackTrace,
                Exception  = e,
            };

            if (e.InnerException != null)
            {
                error.InnerException = Logg <T>(e.InnerException, suppressMessage);
            }

            DwarfContext <T> .GetDatabase().Insert <T, ErrorLog>(error);

            if (!suppressMessage)
            {
                ExceptionHandler.Logg(error);
            }

            return(error);
        }
Exemple #2
0
        /// <summary>
        /// See base
        /// </summary>
        public void Delete()
        {
            if (!IsSaved)
            {
                return;
            }

            try
            {
                DbContextHelper <T> .BeginOperation();

                OnBeforeDeleteInternal();
                OnBeforeDelete();
                DwarfContext <T> .GetDatabase().Delete(this);

                DbContextHelper <T> .FinalizeOperation(false);

                OnAfterDeleteInternal();
                OnAfterDelete();
            }
            catch (Exception e)
            {
                DbContextHelper <T> .FinalizeOperation(true);

                DwarfContext <T> .GetConfiguration().ErrorLogService.Logg(e);

                throw;
            }
            finally
            {
                DbContextHelper <T> .EndOperation();
            }
        }
Exemple #3
0
        /// <summary>
        /// Returns an object of the type T with the supplied Id
        /// </summary>
        protected static T Load(params WhereCondition <T>[] conditions)
        {
            if (!typeof(T).Implements <ICompositeId>())
            {
                throw new InvalidOperationException("This method may only be used for ICompositeId types");
            }

            return(DwarfContext <T> .GetDatabase().Select(conditions));
        }
Exemple #4
0
        /// <summary>
        /// Returns an object of the type T with the supplied Id
        /// </summary>
        public static T Load(Guid id)
        {
            if (typeof(T).Implements <ICompositeId>())
            {
                throw new InvalidOperationException("This method may not be used for ICompositeId types");
            }

            return(DwarfContext <T> .GetDatabase().Select <T>(id));
        }
Exemple #5
0
 /// <summary>
 /// Returns all AuditLog events for the supplied object
 /// </summary>
 public static List <AuditLog> GetAllEvents <T>(Guid id)
 {
     return(DwarfContext <T> .GetDatabase().SelectReferencing <T, AuditLog>
            (
                new WhereCondition <AuditLog> {
         Column = x => x.ClassType, Value = typeof(T).Name
     },
                new WhereCondition <AuditLog> {
         Column = x => x.ObjectId, Value = id
     }
            ));
 }
Exemple #6
0
 /// <summary>
 /// Returns the "Created" AuditLog event for the supplied object
 /// </summary>
 public static AuditLog GetCreatedEvent <T>(T obj) where T : Dwarf <T>, new()
 {
     return(DwarfContext <T> .GetDatabase().SelectReferencing <T, AuditLog>
            (
                new WhereCondition <AuditLog> {
         Column = x => x.ClassType, Value = obj.GetType().Name
     },
                new WhereCondition <AuditLog> {
         Column = x => x.AuditLogType, Value = AuditLogTypes.Created
     },
                new WhereCondition <AuditLog> {
         Column = x => x.ObjectId, Value = obj.Id
     }
            ).FirstOrDefault());
 }
Exemple #7
0
        /// <summary>
        /// Restores the object's properties to their original values
        /// </summary>
        public void Refresh()
        {
            //Fetch the original values from the database (bypass the cache...)
            var originalObject = DwarfContext <T> .GetDatabase().SelectReferencing <T>(new QueryBuilder().Select <T>().From <T>().Where(this, Cfg.PKProperties[DwarfHelper.DeProxyfy(this)]), false, true).FirstOrDefault();

            if (originalObject != null)
            {
                foreach (var ep in DwarfHelper.GetDBProperties(GetType()))
                {
                    SetOriginalValue(ep.Name, ep.GetValue(originalObject));
                }
            }

            //Else should we throw an exception since the object has been deleted from the DB?

            Reset();
        }
Exemple #8
0
        /// <summary>
        /// Stores an AuditLog object for the ongoing transaction
        /// </summary>
        public static IAuditLog Logg <T>(IDwarf obj, AuditLogTypes auditLogType, params AuditLogEventTrace[] auditUpdateEvents)
        {
            var type = DwarfHelper.DeProxyfy(obj);

            if (type.Implements <IAuditLogless>() || type.Implements <IAuditLog>())
            {
                return(null);
            }

            var al = new AuditLog
            {
                ClassType    = type.Name,
                AuditLogType = auditLogType,
                UserName     = DwarfContext <T> .GetConfiguration().UserService.CurrentUser != null ? DwarfContext <T> .GetConfiguration().UserService.CurrentUser.UserName : string.Empty,
                TimeStamp    = DateTime.Now,
                ObjectValue  = obj.ToString(),
            };

            if (!type.Implements <ICompositeId>())
            {
                al.ObjectId = obj.Id.ToString();
            }
            else
            {
                foreach (var ep in DwarfHelper.GetPKProperties(type))
                {
                    al.ObjectId += string.Format("[{0}: {1}]", ep.Name, ep.GetValue(obj));
                }
            }

            if (auditLogType != AuditLogTypes.Created)
            {
                al.AuditDetails = "<?xml version=\"1.0\"?><Properties>";

                foreach (var auditUpdateEvent in auditUpdateEvents)
                {
                    al.AuditDetails += auditUpdateEvent.ToXml().ToString();
                }

                al.AuditDetails += "</Properties>";
            }

            DwarfContext <T> .GetDatabase().Insert <T, AuditLog>(al);

            return(al);
        }
Exemple #9
0
        /// <summary>
        /// Deletes a ManyToMany relationship
        /// </summary>
        private static void DeleteManyToMany(IDwarf owner, IDwarf child, string alternateTableName = null)
        {
            try
            {
                DbContextHelper <T> .BeginOperation();

                DwarfContext <T> .GetDatabase().DeleteManyToMany <T>(owner, child, alternateTableName);

                DbContextHelper <T> .FinalizeOperation(false);
            }
            catch (Exception e)
            {
                DbContextHelper <T> .FinalizeOperation(true);

                DwarfContext <T> .GetConfiguration().ErrorLogService.Logg(e);

                throw;
            }
            finally
            {
                DbContextHelper <T> .EndOperation();
            }
        }
Exemple #10
0
        /// <summary>
        /// A quick way to insert a lot of objects at the same time. No audit logs are created though
        /// </summary>
        protected static void BulkInsert(IEnumerable <T> objects)
        {
            try
            {
                DbContextHelper <T> .BeginOperation();

                DwarfContext <T> .GetDatabase().BulkInsert(objects);

                DbContextHelper <T> .FinalizeOperation(false);
            }
            catch (Exception e)
            {
                DbContextHelper <T> .FinalizeOperation(true);

                DwarfContext <T> .GetConfiguration().ErrorLogService.Logg(e);

                throw;
            }
            finally
            {
                DbContextHelper <T> .EndOperation();
            }
        }
Exemple #11
0
        internal QueryBuilderLight Where <T>(string column, object value)
        {
            where.Append(string.Format("{0} [{1}].[{2}] = {3} ", (where.Length == 0 ? "WHERE " : "AND "), typeof(T).Name, column, DwarfContext <T> .GetDatabase().ValueToSqlString(value)));

            return(this);
        }
Exemple #12
0
        internal QueryBuilderLight Where(string column, object value, Type type)
        {
            where.Append(string.Format("{0} {1} = {2} ", (where.Length == 0 ? "WHERE " : "AND "), column, DwarfContext.GetDatabase(type).ValueToSqlString(value)));

            return(this);
        }
Exemple #13
0
 /// <summary>
 /// Executes a SQL statement against a connection object.
 /// </summary>
 public void ExecuteNonQuery(string query)
 {
     DwarfContext <T> .GetDatabase().ExecuteNonQuery <T>(query);
 }
Exemple #14
0
 /// <summary>
 /// Executes the query and returns the first column of the first row in the result set returned by the query. All other columns and rows are ignored.
 /// </summary>
 public TY ExecuteScalar <TY>(string query)
 {
     return(DwarfContext <T> .GetDatabase().ExecuteScalar <T, TY>(query));
 }
Exemple #15
0
 /// <summary>
 /// Executes a SQL statement against a connection object and returns a list of dynamic objects
 /// </summary>
 public List <dynamic> ExecuteQuery(string query)
 {
     return(DwarfContext <T> .GetDatabase().ExecuteCustomQuery <T>(query));
 }
Exemple #16
0
 /// <summary>
 /// Executes a SQL statement against a connection object.
 /// </summary>
 public void ExecuteNonQuery(QueryBuilder queryBuilder)
 {
     DwarfContext <T> .GetDatabase().ExecuteNonQuery <T>(queryBuilder);
 }
Exemple #17
0
 /// <summary>
 /// See base
 /// </summary>
 public static List <AuditLog> LoadAll <T>()
 {
     return(DwarfContext <T> .GetDatabase().SelectReferencing <T, AuditLog>());
 }
Exemple #18
0
 /// <summary>
 /// Returns an object collection of the type T where
 /// the object's value(s) matches the supplied value(s)
 /// </summary>
 protected static List <TY> LoadReferencing <TY>(params WhereCondition <TY>[] conditions) where TY : Dwarf <TY>, new()
 {
     return(DwarfContext <T> .GetDatabase().SelectReferencing <T, TY>(conditions));
 }
Exemple #19
0
 /// <summary>
 /// Returns an object collection of the type T where
 /// the object's value(s) matches the supplied value(s)
 /// </summary>
 protected static List <TY> LoadReferencing <TY>(QueryBuilder queryBuilder, bool overrideSelect = true) where TY : Dwarf <TY>, new()
 {
     return(DwarfContext <T> .GetDatabase().SelectReferencing <TY>(queryBuilder, overrideSelect));
 }
Exemple #20
0
 /// <summary>
 ///Returns an object collection of the type T where
 /// the supplied value matches the the target values via a common many to many table
 /// </summary>
 private static List <TY> LoadManyToManyRelation <TY>(IDwarf ownerObject, string alternateTableName = null) where TY : Dwarf <TY>, new()
 {
     return(DwarfContext <T> .GetDatabase().SelectManyToMany <TY>(ownerObject, alternateTableName));
 }
Exemple #21
0
 /// <summary>
 /// Returns an object collection of the type T where
 /// the object's value(s) matches the supplied value(s)
 /// </summary>
 protected static List <TY> LoadReferencing <TY>(QueryMergers queryMerger, params QueryBuilder[] queryBuilders) where TY : Dwarf <TY>, new()
 {
     return(DwarfContext <T> .GetDatabase().SelectReferencing <TY>(queryMerger, queryBuilders));
 }
Exemple #22
0
 /// <summary>
 /// Returns an object collection of the type T where
 /// the object's value(s) matches the supplied value(s)
 /// </summary>
 public static List <T> LoadReferencing(QueryBuilder queryBuilder)
 {
     return(DwarfContext <T> .GetDatabase().SelectReferencing <T>(queryBuilder));
 }
Exemple #23
0
 /// <summary>
 /// See base
 /// </summary>
 public static AuditLog Load <T>(Guid id)
 {
     return(DwarfContext <T> .GetDatabase().Select <T, AuditLog>(id));
 }
Exemple #24
0
 /// <summary>
 /// Executes a SQL statement against a connection object and returns a list of dynamic objects
 /// </summary>
 public List <dynamic> ExecuteQuery(QueryBuilder queryBuilder)
 {
     return(DwarfContext <T> .GetDatabase().ExecuteCustomQuery <T>(queryBuilder));
 }
Exemple #25
0
 /// <summary>
 /// Executes the query and returns the first column of the first row in the result set returned by the query. All other columns and rows are ignored.
 /// </summary>
 public TY ExecuteScalar <TY>(QueryBuilder queryBuilder)
 {
     return(DwarfContext <T> .GetDatabase().ExecuteScalar <T, TY>(queryBuilder));
 }
Exemple #26
0
        /// <summary>
        /// See base
        /// </summary>
        public void Save()
        {
            if (isDeleted)
            {
                return;
            }

            try
            {
                DbContextHelper <T> .BeginOperation();

                if (!Id.HasValue)
                {
                    Id = internallyProvidedCustomId.HasValue ? internallyProvidedCustomId : Guid.NewGuid();
                }

                OnBeforeSave();

                var faultyForeignKeys = FaultyForeignKeys(this).ToList();

                if (faultyForeignKeys.Count > 0)
                {
                    DbContextHelper <T> .RegisterInvalidObject(this, faultyForeignKeys);

                    DbContextHelper <T> .FinalizeOperation(false);

                    return;
                }
                else
                {
                    DbContextHelper <T> .UnRegisterInvalidObject(this);
                }

                var auditLogType = AuditLogTypes.Updated;

                var traces = CreateTraceEventsForProperties();

                if (IsSaved)
                {
                    if (traces.Length > 0) //IsDirty
                    {
                        DwarfContext <T> .GetDatabase().Update(this, traces.Select(x => PropertyHelper.GetProperty <T>(x.PropertyName)));
                    }
                }
                else
                {
                    auditLogType = AuditLogTypes.Created;
                    DwarfContext <T> .GetDatabase().Insert <T, T>(this, Id);
                }

                CreateAuditLog(auditLogType, traces);
                PersistOneToManyCollections();
                PersistManyToManyCollections();

                OnAfterSave();
                DbContextHelper <T> .FinalizeOperation(false);

                OnAfterSaveInternal();
            }
            catch (Exception e)
            {
                DbContextHelper <T> .FinalizeOperation(true);

                DwarfContext <T> .GetConfiguration().ErrorLogService.Logg(e);

                throw;
            }
            finally
            {
                DbContextHelper <T> .EndOperation();
            }
        }