Пример #1
0
        public virtual int GetNewId()
        {
            var query = QueryBuilder <T> .GetNewId();

            return(_dbContext.SqlConnection.Query <int>(query).FirstOrDefault());
        }
Пример #2
0
        public virtual IEnumerable <T> AllIncluding(params Expression <Func <T, object> >[] includeProperties)
        {
            var query = QueryBuilder <T> .Select();

            return(_dbContext.SqlConnection.Query <T>(query, includeProperties).ToList());
        }
Пример #3
0
        public virtual IEnumerable <T> GetAll()
        {
            var query = QueryBuilder <T> .Select();

            return(_dbContext.SqlConnection.Query <T>(query).ToList());
        }
Пример #4
0
        public virtual T Get(T entity)
        {
            var query = QueryBuilder <T> .SelectByPrimaryKey();

            return(_dbContext.SqlConnection.Query <T>(query, entity).FirstOrDefault());
        }
Пример #5
0
        public virtual int Delete(T entity)
        {
            var query = QueryBuilder <T> .Delete();

            return(_dbContext.SqlConnection.Query <int>(query, entity).FirstOrDefault());
        }
Пример #6
0
        public virtual T GetWithNavigationProperty(T entity)
        {
            var query = QueryBuilder <T> .SelectByPrimaryKey();

            var foreignKeyList = new List <string>();
            var data           = _dbContext.SqlConnection.Query <T>(query, entity).FirstOrDefault();

            if (data != null)
            {
                var virtualAttributes = typeof(T).GetProperties().Where(p => p.GetMethod.IsVirtual);
                var foreignAttribute  = typeof(T).GetProperties().Where(prop => prop.IsDefined(typeof(ForeignKeyAttribute), false));
                var enumerable        = foreignAttribute as PropertyInfo[] ?? foreignAttribute.ToArray();
                if (enumerable.Any())
                {
                    foreignKeyList.AddRange(enumerable.Select(atr => atr.Name));
                }
                var propertyInfos = virtualAttributes as PropertyInfo[] ?? virtualAttributes.ToArray();
                if (propertyInfos.Any())
                {
                    foreach (var attributes in propertyInfos)
                    {
                        var    type = attributes.PropertyType;
                        string name;
                        string primaryKey = string.Empty;
                        var    isList     = false;

                        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                        {
                            var itemType = type.GetGenericArguments()[0];
                            name   = itemType.FullName + "," + itemType.Assembly.FullName;
                            isList = true;
                        }
                        else
                        {
                            name = type.FullName + "," + type.Assembly.FullName;
                        }

                        var classType    = Type.GetType(name, true);
                        var o            = (Activator.CreateInstance(classType));
                        var keyAttribute = Type.GetType(name, true).GetProperties().FirstOrDefault(prop => prop.IsDefined(typeof(KeyAttribute), false));
                        if (keyAttribute != null)
                        {
                            primaryKey = keyAttribute.Name;
                        }

                        var tableAttribute = o.GetType().GetCustomAttributes(true).FirstOrDefault(a => a.GetType().Name == "TableAttribute");

                        if (tableAttribute != null)
                        {
                            var innerQuery = string.Empty;
                            var attribute  = (TableAttribute)tableAttribute;
                            if (!string.IsNullOrEmpty(primaryKey))
                            {
                                if (foreignKeyList.Count > 0 && foreignKeyList.Contains(primaryKey))
                                {
                                    innerQuery = "SELECT * FROM " + attribute.Schema + ".[" + attribute.Name +
                                                 "] WHERE " + primaryKey + "=@" + primaryKey;
                                }
                                else
                                {
                                    innerQuery = "SELECT * FROM " + attribute.Schema + ".[" + attribute.Name +
                                                 "] " + QueryBuilder <T> .GetWhereClause();
                                }

                                var innerValue = _dbContext.SqlConnection.Query <dynamic>(innerQuery, data).FirstOrDefault();

                                if (innerValue != null)
                                {
                                    PropertyInfo propertyInfo = data.GetType().GetProperty(Convert.ToString(classType.Name));
                                    if (propertyInfo != null)
                                    {
                                        propertyInfo.SetValue(data, Convert.ChangeType((QuestionCategory)innerValue, propertyInfo.PropertyType), null);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(data);
        }