Example #1
0
        /// <summary>
        /// 更新实体记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(BaseEntity entity)
        {
            return(this.ExecuteTransaction(() =>
            {
                #region 更新前 Plugin
                var context = new PersistBrokerPluginContext()
                {
                    Broker = this, Entity = entity, EntityName = entity.EntityName, Action = EntityAction.PreUpdate
                };
                ServiceContainer.ResolveAll <IPersistBrokerBeforeCreateOrUpdate>()?
                .Each(item => item.Execute(context));

                ServiceContainer.ResolveAll <IPersistBrokerPlugin>(item => item.StartsWith(entity.EntityName.Replace("_", ""), StringComparison.OrdinalIgnoreCase))
                .Each(item => item.Execute(context));
                #endregion

                var sql = @"
UPDATE {0} SET {1} WHERE {2} = @id;
";
                var paramList = new Dictionary <string, object>()
                {
                    { "@id", entity.Id }
                };

                #region 处理属性
                var attributes = new List <string>();
                int count = 0;
                foreach (var item in entity.GetAttributes())
                {
                    if (item.Key != "Id" && item.Key != entity.EntityName + "Id")
                    {
                        var keyValue = ParseSqlUtil.GetSpecialValue($"@param{count}", item.Value);
                        paramList.Add($"@param{count}", keyValue.value);
                        attributes.Add($"{ item.Key} = {keyValue.name}");
                        count++;
                    }
                }
                #endregion
                sql = string.Format(sql, entity.EntityName, string.Join(",", attributes), entity.MainKeyName);
                var result = this.Execute(sql, paramList);

                #region 更新后 Plugin
                context.Action = EntityAction.PostUpdate;
                ServiceContainer.ResolveAll <IPersistBrokerPlugin>(item => item.StartsWith(entity.EntityName.Replace("_", ""), StringComparison.OrdinalIgnoreCase))
                .Each(item => item.Execute(context));
                #endregion
                return result;
            }));
        }
Example #2
0
        /// <summary>
        /// 创建实体记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public string Create(BaseEntity entity, bool usePlugin = true)
        {
            return(this.ExecuteTransaction(() =>
            {
                #region 创建前 Plugin
                var context = new PersistBrokerPluginContext()
                {
                    Entity = entity, Broker = this, Action = EntityAction.PreCreate, EntityName = entity.EntityName
                };
                ServiceContainer.ResolveAll <IPersistBrokerBeforeCreateOrUpdate>()?
                .Each(item => item.Execute(context));
                if (usePlugin)
                {
                    ServiceContainer.ResolveAll <IPersistBrokerPlugin>(item => item.StartsWith(entity.EntityName.Replace("_", ""), StringComparison.OrdinalIgnoreCase))
                    .Each(item => item.Execute(context));
                }
                #endregion

                var sql = "INSERT INTO {0}({1}) Values({2})";
                var attrs = new List <string>();
                var values = new List <object>();
                var paramList = new Dictionary <string, object>();
                foreach (var attr in entity.GetAttributes())
                {
                    var attrName = attr.Key == "Id" ? entity.MainKeyName : attr.Key;
                    var keyValue = ParseSqlUtil.GetSpecialValue($"@{attrName}", attr.Value);
                    attrs.Add(attrName);
                    values.Add(keyValue.name);
                    paramList.Add(attrName, keyValue.value);
                }
                sql = string.Format(sql, entity.EntityName, string.Join(",", attrs), string.Join(",", values));
                this.Execute(sql, paramList);

                #region 创建后 Plugin
                if (usePlugin)
                {
                    context.Action = EntityAction.PostCreate;
                    ServiceContainer.ResolveAll <IPersistBrokerPlugin>(item => item.StartsWith(entity.EntityName.Replace("_", ""), StringComparison.OrdinalIgnoreCase))
                    .Each(item => item.Execute(context));
                }
                #endregion

                return entity.Id;
            }));
        }
        public void Execute(PersistBrokerPluginContext context)
        {
            var entity = context.Entity;
            var broker = context.Broker;

            var user = CallContext <CurrentUserModel> .GetData(CallContextType.User);

            switch (context.Action)
            {
            case EntityAction.PreCreate:
            {
                if ((!entity.GetAttributes().ContainsKey("createdBy") || entity.GetAttributeValue("createdBy") == null) && entity.GetType().GetProperty("createdBy") != null)
                {
                    entity.SetAttributeValue("createdBy", user.Id);
                    entity.SetAttributeValue("createdByName", user.Name);
                }
                if ((!entity.GetAttributes().ContainsKey("createdOn") || entity.GetAttributeValue("createdOn") == null) && entity.GetType().GetProperty("createdOn") != null)
                {
                    entity.SetAttributeValue("createdOn", DateTime.Now);
                }
                entity.SetAttributeValue("modifiedBy", user.Id);
                entity.SetAttributeValue("modifiedByName", user.Name);
                entity.SetAttributeValue("modifiedOn", DateTime.Now);

                SetBooleanName(entity);
                CheckDuplicate(entity, broker);
            }
            break;

            case EntityAction.PreUpdate:
            {
                entity.SetAttributeValue("modifiedBy", user.Id);
                entity.SetAttributeValue("modifiedByName", user.Name);
                entity.SetAttributeValue("modifiedOn", DateTime.Now);

                SetBooleanName(entity);
                CheckDuplicate(entity, broker);
            }
            break;

            default:
                break;
            }
        }