Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 同时使用Repository factory和Repository factor设计模式_C#_Generics_Repository_Factory - Fatal编程技术网

C# 同时使用Repository factory和Repository factor设计模式

C# 同时使用Repository factory和Repository factor设计模式,c#,generics,repository,factory,C#,Generics,Repository,Factory,我的DbRepository类如下所示: public class DbRepository<TEntity, TDbContext> : IRepository<TEntity> where TEntity : class, IEntity where TDbContext : DbContext { protected readonly TDbContext dbContext; /// <summary> ///

我的DbRepository类如下所示:

public class DbRepository<TEntity, TDbContext> : IRepository<TEntity>
    where TEntity : class, IEntity
    where TDbContext : DbContext
{
    protected readonly TDbContext dbContext;

    /// <summary>
    ///     Initializes a new instance of the <see cref="DbRepository{TEntity}" /> class.
    /// </summary>
    public DbRepository(TDbContext dbContext)
    {
        this.dbContext = dbContext;
    }

    /// <inheritdoc/>
    public async Task<TEntity> AddAsync(TEntity entity)
    {
        ObjectCheck.EntityCheck(entity, $"{nameof(TEntity)} missing.");
        await dbContext.Set<TEntity>().AddAsync(entity);
        await dbContext.SaveChangesAsync();
        return entity;
    }

    /// <inheritdoc/>
    public virtual async Task<TEntity> GetAsync(object primaryKey)
    {
        ObjectCheck.PrimaryKeyCheck(primaryKey, $"primaryKey <= 0 in {nameof(IRepository<TEntity>)}");
        return await dbContext.Set<TEntity>().FindAsync(primaryKey);
    }

    /// <inheritdoc/>
    public virtual IQueryable<TEntity> GetAll(Expression<Func<TEntity, bool>> filter = null)
    {
        return (filter != null ? dbContext.Set<TEntity>().Where(filter) : dbContext.Set<TEntity>());
    }

    /// <inheritdoc/>
    public virtual async Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> filter)
    {
        return await dbContext.Set<TEntity>().SingleOrDefaultAsync(filter);
    }

    /// <inheritdoc/>
    public virtual async Task<TEntity> UpdateAsync(TEntity entity)
    {
        ObjectCheck.EntityCheck(entity, $"{nameof(TEntity)} missing.");
        dbContext.Set<TEntity>().Update(entity);
        await dbContext.SaveChangesAsync();
        return entity;
    }

    /// <inheritdoc/>
    public virtual async Task DeleteAsync(object primaryKey)
    {
        var entityToBeDeleted = await GetAsync(primaryKey);
        ObjectCheck.EntityCheck(entityToBeDeleted, $"{nameof(TEntity)} missing.");

        dbContext.Set<TEntity>().Remove(entityToBeDeleted);
        await dbContext.SaveChangesAsync();
    }

}
   public interface IRepository<TEntity>
      where TEntity : class, IEntity
   {
    Task<TEntity> GetAsync(object primaryKey);

    IQueryable<TEntity> GetAll(Expression<Func<TEntity, bool>> filter = null);

    Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> filter);

    Task<TEntity> AddAsync(TEntity entity);

    Task<TEntity> UpdateAsync(TEntity entity);

    Task DeleteAsync(object primaryKey);
   }
    
公共类数据库存储库:IRepository
其中tenty:类,tenty
其中TDbContext:DbContext
{
受保护的只读TDbContext dbContext;
/// 
///初始化类的新实例。
/// 
公共数据库存储库(TDbContext dbContext)
{
this.dbContext=dbContext;
}
/// 
公共异步任务AddAsync(TEntity实体)
{
EntityCheck(entity,$“{nameof(tenty)}缺失”);
等待dbContext.Set().AddAsync(实体);
等待dbContext.saveChangesSync();
返回实体;
}
/// 
公共虚拟异步任务GetAsync(对象主密钥)
{

PrimaryKeyCheck(primaryKey,$”primaryKey它给出了该错误,因为在IRepository接口中,您告诉T必须是类(引用类型),并且它必须实现IEntity接口

在工厂里,你没有说一个也没有

也许你必须写一些这样的东西:

public class RepositoryFactory
{
    public static TRepository GetRepositoryInstance<T, TRepository>()
          where TRepository : IRepository<T>, new()
          where T : class, IEntity
    {
        return new TRepository();
    }
}
公共类存储工厂
{
公共静态TRepository GetRepositoryInstance()
其中TRepository:IRepository,new()
式中T:类,属性
{
返回新的TRepository();
}
}

谢谢。我是否将此通用存储库注册为作用域,以及在启动时如何注册?我通常会执行类似于services.AddScoped()的操作;在这种情况下我该怎么办?DI是创建实例的。当您想使用factory创建时,最好注册factory。在您的情况下,我会在DI中注册RepositoryFactory,方法是将静态方法转换为实例方法,并在启动时注册:services.AddSingleton(new RepositoryFactory());Singleton,因为Factory模式是创造性模式,您使用Factory来创建回购协议,而不是DI。另一种注册方法是将Factory放在一边并这样做:services.AddScoped
public class RepositoryFactory
{
    public static TRepository GetRepositoryInstance<T, TRepository>()
          where TRepository : IRepository<T>, new()
          where T : class, IEntity
    {
        return new TRepository();
    }
}