gpt4 book ai didi

c# - 通用存储库添加自定义方法

转载 作者:太空狗 更新时间:2023-10-29 23:38:32 26 4
gpt4 key购买 nike

我正在尝试创建一个带有通用存储库的库,供以后在我的 MVC 应用程序中使用。代码如下...

public abstract class BaseEntity
{
}

public interface IEntity<T>
{
T Id { get; set; }
}

public abstract class Entity<T> : BaseEntity, IEntity<T>
{
public virtual T Id { get; set; }
}

public interface IAuditableEntity
{
int? UsuarioId { get; set; }
DateTime CreatedDate { get; set; }
string CreatedBy { get; set; }
DateTime UpdatedDate { get; set; }
string UpdatedBy { get; set; }
}

public abstract class AuditableEntity<T> : Entity<T>, IAuditableEntity
{
public int? UsuarioId { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public DateTime UpdatedDate { get; set; }
public string UpdatedBy { get; set; }
}

public interface IGenericRepository<T> where T : BaseEntity
{
IEnumerable<T> GetAll();
IEnumerable<T> GetByUsuarioId(int usuarioId);
T GetById(int id);
T Add(T entity);
T Delete(T entity);
void Edit(T entity);
void Save();
}

public class GenericRepository<T> : IGenericRepository<T>
where T : BaseEntity
{
protected DbContext _entities;
protected readonly IDbSet<T> _dbset;

public GenericRepository(DbContext context)
{
_entities = context;
_dbset = context.Set<T>();
}

public virtual IEnumerable<T> GetAll()
{
return _dbset.AsEnumerable<T>();
}

public IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate)
{
IEnumerable<T> query = _dbset.Where(predicate).AsEnumerable();
return query;
}


public virtual IEnumerable<T> GetByUsuarioId(int usuarioId)
{
// NO SÉ COMO IMPLEMENTAR ESTE METODO!!!!!
return null;
// NO SÉ COMO IMPLEMENTAR ESTE METODO!!!!!
}

public virtual T GetById(int id)
{
return _dbset.Find(id);
}

public virtual T Add(T entity)
{
return _dbset.Add(entity);
}

public virtual T Delete(T entity)
{
return _dbset.Remove(entity);
}

public virtual void Edit(T entity)
{
_entities.Entry(entity).State = EntityState.Modified;
}

public virtual void Save()
{
_entities.SaveChanges();
}
}

这些是我的一些类(class) POCO...

   public class Documento : AuditableEntity<int>
{
public string Descripcion { get; set; }
public string Foto { get; set; }

public virtual Usuario Usuario { get; set; }
}

public class Gasto : AuditableEntity<int>
{
public int? TaxiId { get; set; }
public int TipoGastoId { get; set; }
public DateTime Fecha { get; set; }
public double Importe { get; set; }
public int Kilometros { get; set; }
public string Descripcion { get; set; }
public string Foto { get; set; }

public virtual Usuario Usuario { get; set; }
public virtual Taxi Taxi { get; set; }
public virtual TipoGasto TipoGasto { get; set; }
public virtual PartidaTarjetas PartidaTarjetas { get; set; }

public virtual ICollection<Tarea> Tareas { get; set; }

public int? PartidaTarjetasId { get; set; }

public Gasto()
{
Tareas = new List<Tarea>();
}
}

public class Nivel : Entity<int>
{
public string Descripcion { get; set; }
public string PaginaInicio { get; set; }

public virtual ICollection<Usuario> Usuarios { get; set; }

public Nivel()
{
Usuarios = new List<Usuario>();
}
}

我的问题是它不会实现方法...

    public virtual IEnumerable<T> GetByUsuarioId(int usuarioId)
{
// NO SÉ COMO IMPLEMENTAR ESTE METODO!!!!!
return null;
// NO SÉ COMO IMPLEMENTAR ESTE METODO!!!!!
}

这是一个通用方法,它应该只在 IAuditable 类型时返回结果...,也必须是某种东西...

    public virtual IEnumerable<T> GetByUsuarioId(int usuarioId)
{
return _dbset.FindBy(c => c.UsuarioId == usuarioId);
}

你能帮帮我吗?,谢谢。

你好 C 鲍尔 ...

注意...

public abstract class BaseEntity
{
}

public interface IEntity<T>
{
T Id { get; set; }
}

public abstract class Entity<T> : BaseEntity, IEntity<T>
{
public virtual T Id { get; set; }
}

public interface IAuditableEntity
{
int? UsuarioId { get; set; }
DateTime CreatedDate { get; set; }
string CreatedBy { get; set; }
DateTime UpdatedDate { get; set; }
string UpdatedBy { get; set; }
}

public abstract class AuditableEntity<T> : Entity<T>, IAuditableEntity
{
public int? UsuarioId { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public DateTime UpdatedDate { get; set; }
public string UpdatedBy { get; set; }
}

我的 GenericRepositry 是..

public interface IGenericRepository<T> where T : BaseEntity

那么现在 ¿?

public interface IGenericRepository<T> where T : ¿¿ ??

最佳答案

您可以实现一个类型化的存储库扩展类,这样它就只适用于 IRepository<IAuditable> :

    public static class Extensions
{
public static IEnumerable<IAuditable> GetUsuarioById(this IRepository<IAuditable> repository, int id)
{
return repository.FindBy(audible => audible.Id == id);
}
}

编辑:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace StackOverflowPlayground
{
class JesusPlayground
{
public JesusPlayground()
{
var auditableRepo = new AuditableRepo();
auditableRepo.GetUsuarioById(1);

var otherRepo = new OtherRepo();
//otherRepo. (does not have GetUsuarioById


var auditableRepoNotUsingTheActualClass = new GenericRepository<IAuditable>();
auditableRepoNotUsingTheActualClass.GetUsuarioById(1); //still works!
}
}


public static class Extensions
{
public static IEnumerable<IAuditable> GetUsuarioById(this IRepository<IAuditable> repository, int id)
{
return repository.FindBy(audible => audible.Id == id);
}
}



public class OtherRepo : IRepository<OtherType>
{
public IEnumerable<OtherType> FindBy(Expression<Func<OtherType, bool>> expr)
{
throw new NotImplementedException();
}
}

public class OtherType
{
}

public interface IAuditable
{
int Id { get; set; }
}

public interface IRepository<T>
{
IEnumerable<T> FindBy(Expression<Func<T, bool>> expr);
}

public class GenericRepository<T> : IRepository<T>
{
public IEnumerable<T> FindBy(Expression<Func<T, bool>> expr)
{
throw new NotImplementedException();
}
}
class AuditableRepo : IRepository<IAuditable>
{
public IEnumerable<IAuditable> FindBy(Expression<Func<IAuditable, bool>> expr)
{
throw new NotImplementedException();
}
}
}

关于c# - 通用存储库添加自定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27490984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com