使用方法形式泛型类表单接口

时间:2017-07-25 08:26:55

标签: c#

我在这个课程中使用通用服务和写法。

public class EfGenericService<T> : IGenericService<T> where T : class
{
    protected IUnitOfWork Uow;
    protected IDbSet<T> Entites;

    public EfGenericService(IUnitOfWork uow)
    {
        Uow = uow;
        Entites = Uow.Set<T>();
    }

    public virtual ResultStatus Add(T entity)
    {
        ResultStatus rs = new ResultStatus();
        try
        {
            Entites.Add(entity);
            Uow.MarkAsAdded(entity);
            rs.IsSuccessed = true;
            rs.Type = MessageType.Success;
            //rs.Message = .Data_Saved_Successfully;
            return rs;
        }
        catch (Exception e)
        {
            rs.IsSuccessed = false;
            rs.Type = MessageType.Danger;
            rs.ErrorException = e;
            //rs.Message = Messages.Error_In_Saving_Data;
        }
        return rs;
    }

    public virtual ResultStatus Update(T entity)
    {
        ResultStatus rs = new ResultStatus();
        try
        {
            Entites.Attach(entity);
            Uow.MarkAsChanged(entity);
            rs.IsSuccessed = true;
            rs.Type = MessageType.Success;
            //rs.Message = Messages.Data_Edited_Successfully;
            return rs;
        }
        catch (Exception e)
        {
            rs.IsSuccessed = false;
            rs.Type = MessageType.Danger;
            rs.ErrorException = e;
            //rs.Message = Messages.Error_In_Editing_Data;
        }
        return rs;
    }

    public virtual ResultStatus UpdateAll(IQueryable<T> updateEntites, Expression<Func<T, T>> updatExpression)
    {
        ResultStatus rs = new ResultStatus();
        try
        {
            updateEntites.Update(updatExpression);
            rs.IsSuccessed = true;
            rs.Type = MessageType.Success;
            //rs.Message = Messages.Data_Edited_Successfully;
            return rs;
        }
        catch (Exception e)
        {
            rs.IsSuccessed = false;
            rs.Type = MessageType.Danger;
            rs.ErrorException = e;
            //rs.Message = Messages.Error_In_Editing_Data;
        }
        return rs;
    }

    public virtual ResultStatus Delete(T entity)
    {
        ResultStatus rs = new ResultStatus();
        try
        {
            Entites.Remove(entity);
            rs.IsSuccessed = true;
            rs.Type = MessageType.Success;
            //rs.Message = Messages.Data_Removed_Successfully;
            return rs;
        }
        catch (Exception e)
        {
            rs.IsSuccessed = false;
            rs.Type = MessageType.Danger;
            rs.ErrorException = e;
            //rs.Message = Messages.Error_In_Removing_Data;
        }
        return rs;
    }

    public virtual ResultStatus DeleteAll(IQueryable<T> deleteEntites)
    {
        ResultStatus rs = new ResultStatus();
        try
        {
            Uow.Set<T>().RemoveRange(deleteEntites.AsEnumerable());
            rs.IsSuccessed = true;
            rs.Type = MessageType.Success;
            //rs.Message = Messages.Data_Removed_Successfully;
            return rs;
        }
        catch (Exception e)
        {
            rs.IsSuccessed = false;
            rs.Type = MessageType.Danger;
            rs.ErrorException = e;
            //rs.Message = Messages.Error_In_Removing_Data;
        }
        return rs;
    }

    public virtual IEnumerable<ResultStatus> ValidateCreate(T entity)
    {
        return new List<ResultStatus>().AsEnumerable();
    }

    public virtual IEnumerable<ResultStatus> ValidateUpdate(T entity)
    {
        return new List<ResultStatus>().AsEnumerable();
    }

    public T Find(Func<T, bool> predicate)
    {
        return Entites.Where(predicate).FirstOrDefault();
    }

    public IList<T> GetAll(bool asNoTracking = false)
    {
        return asNoTracking ? Entites.AsNoTracking().ToList() : Entites.ToList();
    }

    public IQueryable<T> GetAllAsQueryable(bool asNoTracking = false)
    {
        return asNoTracking ? Entites.AsNoTracking().AsQueryable() : Entites.AsQueryable();
    }

    public IList<T> GetAll(Func<T, bool> predicate, bool asNoTracking = false)
    {
        return asNoTracking ? Entites.AsNoTracking().Where(predicate).ToList() : Entites.Where(predicate).ToList();

    }

    public IQueryable<T> GetAllAsQueryable(Func<T, bool> predicate, bool asNoTracking = false)
    {
        return asNoTracking ? Entites.AsNoTracking().Where(predicate).AsQueryable() : Entites.Where(predicate).AsQueryable();
    }

    public IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<T> query = Uow.Set<T>();
        return includeProperties.Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
    }
    #region IDisposable Members
    public void Dispose()
    {
        throw new NotImplementedException();
    }
    #endregion

}

和这个界面:

public interface IGenericService<T> : IDisposable where T : class
{
    ResultStatus Add(T entity);
    ResultStatus Update(T entity);
    ResultStatus UpdateAll(IQueryable<T> updateEntites, Expression<Func<T, T>> updateExpression);
    ResultStatus Delete(T entity);
    ResultStatus DeleteAll(IQueryable<T> deleteEntites);
    IEnumerable<ResultStatus> ValidateCreate(T entity);
    IEnumerable<ResultStatus> ValidateUpdate(T entity);
    T Find(Func<T, bool> predicate);
    IList<T> GetAll(bool asNoTracking = false);
    IQueryable<T> GetAllAsQueryable(bool asNoTracking = false);
    IList<T> GetAll(Func<T, bool> predicate, bool asNoTracking = false);
    IQueryable<T> GetAllAsQueryable(Func<T, bool> predicate, bool asNoTracking = false);
    IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties);
}

我在User Entite中使用此通用类。

public interface IUser : IGenericService<User>
{

}

课程:

public class EFUserService : EfGenericService<User>, IUser
{
    public EFUserService(IUnitOfWork uow) : base(uow)
    {
    }
}

现在。我需要在Find中使用EfGenericService方法来获取登录代码。

我在班级private readonly IUser _userservice;

中使用此功能

但找不到Find方法。有什么问题 ?我怎么能解决这个问题?

更新

编写此代码以便登录EFUserService

public User FindUser(string username, string password)
        {
            return Find(x => x.Username == username && x.Password == password);
        }
    }

*** EFUserService在其他类库中,我需要在主程序中使用此方法。

现在我在主程序中编写这段代码:

  `private readonly IUser _userservice;`

找不到FindUser

1 个答案:

答案 0 :(得分:0)

随着问题的更新,我的新答案在这里:

.PreserveReferences()不属于FindUser。它仅在IUser中声明。

请在主程序中尝试:

EFUserService

如果您希望将其保留为private readonly EFUserService _userservice; ,则可以执行此操作:

IUser