コード例 #1
0
ファイル: ErrorLog.cs プロジェクト: GreenfieldVentures/Dwarf
        /// <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);
        }
コード例 #2
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
        /// <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();
            }
        }
コード例 #3
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
        /// <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));
        }
コード例 #4
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
        /// <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));
        }
コード例 #5
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
        private void OnBeforeDeleteInternal()
        {
            //We want to persist all the Inverse OneToManyCollection upon deletion. Let's remove all lists that doesn't
            //need persistance first (all objects therein will be deleted in the database via delete cascades anyways)
            foreach (var pi in DwarfHelper.GetOneToManyProperties(this))
            {
                var propertyAtt = OneToManyAttribute.GetAttribute(pi.ContainedProperty);

                if (propertyAtt == null)
                {
                    throw new NullReferenceException(pi.Name + " is missing the OneToMany attribute...");
                }

                if (!propertyAtt.IsInverse)
                {
                    continue;
                }

                var obj = (IDwarfList)pi.GetValue(this);

                var owningProp = oneToManyAlternateKeys.ContainsKey(pi.Name) ? oneToManyAlternateKeys[pi.Name] : GetType().Name;

                obj.Cast <IDwarf>().ForEachX(x => PropertyHelper.SetValue(x, owningProp, null));
                obj.SaveAllInternal <T>();
            }

            if (DbContextHelper <T> .DbContext.IsAuditLoggingSuspended || DwarfContext <T> .GetConfiguration().AuditLogService == null)
            {
                return;
            }

            var traces = (from ep in DwarfHelper.GetDBProperties(GetType()).Where(x => !x.Name.Equals("Id"))
                          let oldValue = originalValues[ep.Name]
                                         where oldValue != null && (oldValue is string? !string.IsNullOrEmpty(oldValue.ToString()) : true)
                                         select new AuditLogEventTrace {
                PropertyName = ep.Name, OriginalValue = oldValue
            }).ToArray();

            var collectionTraces = (from ep in DwarfHelper.GetGemListProperties(GetType())
                                    let oldValue = (IGemList)ep.GetValue(this)
                                                   where oldValue != null && oldValue.Count > 0
                                                   select new AuditLogEventTrace {
                PropertyName = ep.Name, OriginalValue = oldValue
            }).ToArray();

            var many2ManyTraces = (from ep in DwarfHelper.GetManyToManyProperties(GetType())
                                   let oldValue = ep.GetValue(this)
                                                  where oldValue != null && ((IList)oldValue).Count > 0
                                                  select new AuditLogEventTrace {
                PropertyName = ep.Name, OriginalValue = oldValue
            }).ToArray();

            DwarfContext <T> .GetConfiguration().AuditLogService.Logg(this, AuditLogTypes.Deleted, traces.Union(collectionTraces).Union(many2ManyTraces).ToArray());
        }
コード例 #6
0
ファイル: AuditLog.cs プロジェクト: GreenfieldVentures/Dwarf
 /// <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
     }
            ));
 }
コード例 #7
0
ファイル: AuditLog.cs プロジェクト: GreenfieldVentures/Dwarf
 /// <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());
 }
コード例 #8
0
ファイル: AuditLog.cs プロジェクト: GreenfieldVentures/Dwarf
        /// <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);
        }
コード例 #9
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
        /// <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();
        }
コード例 #10
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
        private void CreateAuditLog(AuditLogTypes auditLogType, IEnumerable <AuditLogEventTrace> propertyTraces)
        {
            if (DbContextHelper <T> .DbContext.IsAuditLoggingSuspended || DwarfContext <T> .GetConfiguration().AuditLogService == null)
            {
                return;
            }

            if (auditLogType == AuditLogTypes.Updated)
            {
                var traces = propertyTraces.Union(CreateTraceEventsForManyToMany()).ToArray();

                if (traces.Length > 0)
                {
                    DwarfContext <T> .GetConfiguration().AuditLogService.Logg(this, auditLogType, traces);
                }
            }
            else if (auditLogType == AuditLogTypes.Created)
            {
                DwarfContext <T> .GetConfiguration().AuditLogService.Logg(this, auditLogType);
            }
        }
コード例 #11
0
        internal static IEnumerable <Type> GetValidTypes <T>()
        {
            var baseType = DeProxyfy(typeof(T));

            if (baseType != typeof(T))
            {
                throw new InvalidOperationException("T is a proxy class... Looks like you need to specify the complete namespace");
            }

            var typeList = baseType.Assembly.GetTypes().Where(type => type.Implements <IDwarf>() && !type.IsAbstract).ToList();

            if (DwarfContext <T> .GetConfiguration().AuditLogService is AuditLogService <T> )
            {
                typeList.Add(typeof(AuditLog));
            }

            if (DwarfContext <T> .GetConfiguration().ErrorLogService is ErrorLogService <T> )
            {
                typeList.Add(typeof(ErrorLog));
            }

            return(typeList);
        }
コード例 #12
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
        /// <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();
            }
        }
コード例 #13
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
        /// <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();
            }
        }
コード例 #14
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);
        }
コード例 #15
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);
        }
コード例 #16
0
 public static IDwarfConfiguration GetConfiguration <T>()
 {
     return(DwarfContext <T> .GetConfiguration());
 }
コード例 #17
0
 /// <summary>
 /// Executes a SQL statement against a connection object.
 /// </summary>
 public void ExecuteNonQuery(string query)
 {
     DwarfContext <T> .GetDatabase().ExecuteNonQuery <T>(query);
 }
コード例 #18
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));
 }
コード例 #19
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
 /// <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));
 }
コード例 #20
0
 /// <summary>
 /// Executes a SQL statement against a connection object.
 /// </summary>
 public void ExecuteNonQuery(QueryBuilder queryBuilder)
 {
     DwarfContext <T> .GetDatabase().ExecuteNonQuery <T>(queryBuilder);
 }
コード例 #21
0
ファイル: AuditLog.cs プロジェクト: GreenfieldVentures/Dwarf
 /// <summary>
 /// See base
 /// </summary>
 public static List <AuditLog> LoadAll <T>()
 {
     return(DwarfContext <T> .GetDatabase().SelectReferencing <T, AuditLog>());
 }
コード例 #22
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
 /// <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));
 }
コード例 #23
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
 /// <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));
 }
コード例 #24
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
 /// <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));
 }
コード例 #25
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));
 }
コード例 #26
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
 /// <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));
 }
コード例 #27
0
ファイル: AuditLog.cs プロジェクト: GreenfieldVentures/Dwarf
 /// <summary>
 /// See base
 /// </summary>
 public static AuditLog Load <T>(Guid id)
 {
     return(DwarfContext <T> .GetDatabase().Select <T, AuditLog>(id));
 }
コード例 #28
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));
 }
コード例 #29
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));
 }
コード例 #30
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
        /// <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();
            }
        }