用于更新导航属性的通用数据访问层

时间:2017-05-25 07:46:45

标签: c# .net entity-framework generics

我有以下通用数据访问类,负责数据库操作。

internal sealed class DataStore<T> : IDataStore<T>
    where T : BaseModel, new()
{
    private readonly DataContext _context;

    public DataStore(DataContext context)
    {
        this._context = context;
    }

    public async Task InsertNew(T obj)
    {
        await Task.Run(async () =>
        {
            _context.Set<T>().Add(obj);
            await _context.SaveChangesAsync();
        });
    }

    public async Task<T> SelectObj(int id, Expression<Func<T, object>> includeExpression = null)
    {
        if (includeExpression != null)
            return
                await _context.Set<T>()
                    .Include<T, object>(includeExpression)
                    .FirstOrDefaultAsync(x => x.ID.Equals(id));

        return await _context.Set<T>().Where(x => x.ID.Equals(id)).FirstOrDefaultAsync();
    }

    public async Task<List<T>> SelectAll(Expression<Func<T, object>> includeExpression = null)
    {
        if (includeExpression != null)
            return await _context.Set<T>().Include<T, object>(includeExpression).ToListAsync();

        return await _context.Set<T>().ToListAsync();
    }

    public async Task Update(T obj)
    {
        await Task.Run(async () =>
        {
             var original = await SelectObj(obj.ID);
             _context.Entry(original).CurrentValues.SetValues(obj);
             await _context.SaveChangesAsync();
         });
    }

    public async Task Delete(T obj)
    {
        await Task.Run(async () =>
        {
            _context.Set<T>().Remove(obj);
            await _context.SaveChangesAsync();
        });
    }

    public async Task Delete(int id, Expression<Func<T, object>> includeExpression = null)
    {
        await Task.Run(async () =>
        {
            var obj = await SelectObj(id, includeExpression);
            await Delete(obj);
        });
    }
}

Update函数的问题是,它只更新传递的T对象,而不是该对象的导航属性。

我已经尝试了以下方法,但我被困住了,不知道如何继续这里。

private void GetNavProperties(T obj)
    {
        var objType = obj.GetType();
        foreach (var prop in objType.GetProperties())
        {
            if (prop.PropertyType.IsClass)
            {
                var values = prop.GetValue(obj);
                //How do I go further here, setting the Entity on the context
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

在玩完之后,我最终找到了适用于我的应用程序的以下解决方案。

以下是解决方案,并附有评论以解释它的作用。

private async Task UpdateNavProperties(T original, T newObject)
    {
        await Task.Run(async () =>
        {
            //get type of object
            var objType = original.GetType();

            //loop through properties on the Type
            foreach (var prop in objType.GetProperties())
            {
                //Check that property is a class/reference type, and not part of the System namespace
                //string would be part of the system namespace, but my custom class not
                if (prop.PropertyType.IsClass && !prop.PropertyType.Namespace.Equals("System"))
                {
                    //get the old value
                    var oldValue = prop.GetValue(original);
                    //get new value
                    var newValue = newObject.GetType().GetProperty(prop.Name).GetValue(newObject);
                    //update the value 
                    _context.Entry(oldValue).CurrentValues.SetValues(newValue);
                    //save changes
                    await _context.SaveChangesAsync();
                }
            }
        });
    }

为了使用它,我做了以下事情:

public async Task Update(T obj, Expression<Func<T, object>> includeExpression = null)
    {
        await Task.Run(async () =>
        {
            var original = await SelectObj(obj.ID, includeExpression);
            _context.Entry(original).CurrentValues.SetValues(obj);
            await _context.SaveChangesAsync();

            await UpdateNavProperties(original, obj);
        });
    }