public void Update(TAppModel entity)
        {
            TDbEntity target = DynamicProxy.CreateDynamicProxy <TDbEntity>();

            EntityMapper.Map <TAppModel, TDbEntity>(entity, target);
            this._context.EntitySet <TDbEntity>().Update(target);
        }
        public void Remove(Action <TAppModel> condition)
        {
            TAppModel local = DynamicProxy.CreateDynamicProxy <TAppModel>();

            condition(local);
            this._context.EntitySet <TDbEntity>().DeleteBy(DynamicProxy.GetModifiedProperties(local));
        }
Exemple #3
0
        public TEntity Create(TEntity entity)
        {
            Dictionary <string, object> modifiedProperties;

            if (DynamicProxy.IsProxy(entity.GetType()))
            {
                modifiedProperties = DynamicProxy.GetModifiedProperties(entity);
            }
            else
            {
                modifiedProperties = this._entityMapper.GetPropertyValues(entity);
            }
            List <string>           values = new List <string>();
            List <string>           list2  = new List <string>();
            List <IDbDataParameter> list3  = new List <IDbDataParameter>();

            foreach (PropertyInfo info in this._properties)
            {
                if (this._autoIncrementProperty != info)
                {
                    values.Add("[" + info.Name + "]");
                    list2.Add("@" + info.Name);
                    IDbDataParameter item = this._helper.CreateParameter(Xinchen.DbEntity.DbHelper.TypeMapper[info.PropertyType], "@" + info.Name, DBNull.Value);
                    if (modifiedProperties.ContainsKey(info.Name))
                    {
                        item.Value = modifiedProperties[info.Name];
                    }
                    if (this._keyProperty == info)
                    {
                        int result = 0;
                        if (int.TryParse(Convert.ToString(item.Value), out result) && (result <= 0))
                        {
                            throw new EntityException("检测到主键值为" + result.ToString() + ",是否忘记给主键赋值?实体名:" + this._entityType.Name);
                        }
                    }
                    if (item.Value == null)
                    {
                        item.Value = DBNull.Value;
                    }
                    list3.Add(item);
                }
            }
            string sql = string.Format("insert into {0} ({1}) values({2});", "[" + this.TableName + "]", string.Join(",", values), string.Join(",", list2));

            if (this._autoIncrementProperty != null && DbHelper.ProviderName != "System.Data.OleDb")
            {
                sql = sql + "SELECT @@IDENTITY;";
                int num2 = this._helper.ExecuteScalarCount(sql, list3.ToArray());
                if (this._autoIncrementProperty != null)
                {
                    this._entityMapper.SetValue(entity, this._autoIncrementProperty.Name, num2);
                }
            }
            else
            {
                this._helper.ExecuteUpdate(sql, list3.ToArray());
            }
            return(DynamicProxy.CreateDynamicProxy <TEntity>(entity));
        }
Exemple #4
0
        public IList <TEntity> GetList(string sql, params IDbDataParameter[] dbParameters)
        {
            DataSet         ds   = this._helper.ExecuteQuery(sql, dbParameters);
            IList <TEntity> list = Mapper.MapList <TEntity>(ds);

            if (list.Count <= 10)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    list[i] = DynamicProxy.CreateDynamicProxy <TEntity>(list[i]);
                }
            }
            return(list);
        }
        public TAppModel Get(Action <TAppModel> condition)
        {
            TAppModel local = DynamicProxy.CreateDynamicProxy <TAppModel>();

            condition(local);
            Dictionary <string, object> modifiedProperties = DynamicProxy.GetModifiedProperties(local);
            TDbEntity source = this._context.EntitySet <TDbEntity>().GetList(modifiedProperties).FirstOrDefault <TDbEntity>();

            if (source == null)
            {
                return(default(TAppModel));
            }
            return(EntityMapper.Map <TDbEntity, TAppModel>(source));
        }
Exemple #6
0
        public int ScalarCount(Action <TEntity> where)
        {
            TEntity local = DynamicProxy.CreateDynamicProxy <TEntity>();

            where (local);
            List <IDbDataParameter> dbParameters = new List <IDbDataParameter>();
            string str = this.ParseConditionFromProxyEntity(local, dbParameters);
            string sql = "select count(1) from " + this.TableName;

            if (!string.IsNullOrEmpty(str))
            {
                sql = sql + " where " + str;
            }
            return(this._helper.ExecuteScalarCount(sql, dbParameters.ToArray()));
        }
        public IList <TAppModel> GetList(Action <TAppModel> condition)
        {
            TAppModel local = DynamicProxy.CreateDynamicProxy <TAppModel>();

            condition(local);
            Dictionary <string, object> modifiedProperties = DynamicProxy.GetModifiedProperties(local);
            IList <TDbEntity>           by    = this._context.EntitySet <TDbEntity>().GetList(modifiedProperties);
            List <TAppModel>            list2 = new List <TAppModel>();

            foreach (TDbEntity local2 in by)
            {
                list2.Add(EntityMapper.Map <TDbEntity, TAppModel>(local2));
            }
            return(list2);
        }
Exemple #8
0
        public void Update(Action <TEntity> where, Action <TEntity> update)
        {
            TEntity whereProxy  = DynamicProxy.CreateDynamicProxy <TEntity>();
            TEntity updateProxy = DynamicProxy.CreateDynamicProxy <TEntity>();

            where (whereProxy);
            update(updateProxy);
            List <IDbDataParameter> dbParameters = new List <IDbDataParameter>();
            List <IDbDataParameter> list2        = new List <IDbDataParameter>();
            string str  = this.ParseConditionFromProxyEntity(whereProxy, dbParameters);
            string str2 = this.ParseSetStringFromProxEntity(updateProxy, list2);
            string sql  = "update [" + this.TableName + "] set " + str2 + " where " + str;

            list2.AddRange(dbParameters);
            this._helper.ExecuteUpdate(sql, list2.ToArray());
        }
        public List <PrivilegeManagement.Model.UserRole> GetList(Action <PrivilegeManagement.Model.UserRole> condition)
        {
            var userRoleRemote = DynamicProxy.CreateDynamicProxy <UserRole>();

            condition(userRoleRemote);
            var dict       = DynamicProxy.GetModifiedProperties(userRoleRemote);
            var listLocal  = _context.UserRoles.GetBy(dict);
            var listRemote = new List <PrivilegeManagement.Model.UserRole>();

            foreach (var userRole in listLocal)
            {
                PrivilegeManagement.Model.UserRole userRemote = new UserRole();
                EntityMapper.Map(userRole, userRemote);
                listRemote.Add(userRemote);
            }
            return(listRemote);
        }
Exemple #10
0
        public void TestCreateProxy()
        {
            var model = DynamicProxy.CreateDynamicProxy(new Model());

            model.Id = 1;
        }
Exemple #11
0
 public TEntity CreateProxy(TEntity entity)
 {
     return(DynamicProxy.CreateDynamicProxy <TEntity>(entity));
 }
        public TResult Execute <TResult>(System.Linq.Expressions.Expression expression)
        {
            var provider = ProviderFactory.CreateProvider(ConfigManager.DataBaseType);
            var parser   = provider.CreateParser();

            //var provider = ProviderFactory.CreateProvider(ConfigManager.DataBase);
            //var parser = provider.CreateParser();
            parser.ElementType = _elementType;
            parser.Parse(expression);
            Type type     = typeof(TResult);
            var  executor = provider.CreateSqlExecutor();

            if (expression.NodeType == ExpressionType.Call && type.IsValueType)
            {
                var     method = ((MethodCallExpression)expression).Method;
                object  r;
                DataSet ds;
                switch (method.Name)
                {
                case "Any":
                    ds = executor.ExecuteDataSet(parser.Result.CommandText, parser.Result.Parameters);
                    r  = ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0;
                    return((TResult)r);

                case "Delete":
                case "Update":
                    r = executor.ExecuteNonQuery(parser.Result.CommandText, parser.Result.Parameters);
                    return((TResult)r);

                case "Average":
                case "Sum":
                case "Count":
                    r = executor.ExecuteScalar(parser.Result.CommandText, parser.Result.Parameters);
                    if (r == DBNull.Value)
                    {
                        return(default(TResult));
                    }
                    return((TResult)Convert.ChangeType(r, type));

                default:
                    throw new Exception();
                }
            }
            else
            {
                var  ds          = executor.ExecuteDataSet(parser.Result.CommandText, parser.Result.Parameters);
                bool isValueType = false;
                if (type.IsGenericType)
                {
                    if (typeof(IEnumerable).IsAssignableFrom(type))
                    {
                        var list = EntityMapper.Map(type.GetGenericArguments()[0], ds);
                        if (_context.IsEnableProxy)
                        {
                            if (list.Count > 10)
                            {
                                return((TResult)list);
                            }
                            if (TableInfoManager.IsEntity(type))
                            {
                                for (int i = 0; i < list.Count; i++)
                                {
                                    list[i] = DynamicProxy.CreateDynamicProxy(list[i]);
                                }
                                var entityOp = _context.GetEntityOperator(type);
                                entityOp.AddEditing(list);
                            }
                        }
                        return((TResult)list);
                    }
                    if (typeof(Nullable <>).IsAssignableFrom(type))
                    {
                        isValueType = true;
                    }
                    if (!isValueType)
                    {
                        throw new Exception();
                    }
                }
                if (type.IsValueType || isValueType)
                {
                    if (ds.Tables[0].Rows.Count <= 0)
                    {
                        return(default(TResult));
                    }
                    var result = ds.Tables[0].Rows[0][0];
                    if (result == DBNull.Value)
                    {
                        return(default(TResult));
                    }
                    return((TResult)Convert.ChangeType(result, type));
                }

                if (TableInfoManager.IsEntity(type))
                {
                    var results = EntityMapper.Map(type, ds);
                    if (results.Count <= 0)
                    {
                        return(default(TResult));
                    }
                    var result = results[0];
                    if (_context.IsEnableProxy)
                    {
                        var entityOp = _context.GetEntityOperator(type);
                        result = DynamicProxy.CreateDynamicProxy(result);
                        entityOp.AddEditing(new ArrayList()
                        {
                            result
                        });
                        return((TResult)result);
                    }
                    return((TResult)result);
                }
                throw new Exception();
            }
        }
Exemple #13
0
 public T CreateProxy <T>(T t)
 {
     return(DynamicProxy.CreateDynamicProxy <T>(t));
 }