如何将泛型类型对象传递给 class 构造函数
How to pass a generic type object to a class constructor
我有一个通用的 class 将扮演存储库的角色,我有第二个 class,第二个 class 应该从构造函数中的通用存储库接收一个对象.
通用 class(存储库):
public sealed class Repo<TContext> : IRepo<TContext>, IDisposable where TContext : DbContext, IDbContextFactory<TContext>, new()
{
#region properties
/// <summary>
/// Private DBContext property
/// </summary>
private DbContext _Context { get; } = null;
/// <summary>
/// Determine if Lazy Loading either activate or not
/// </summary>
private bool _LazyLoaded { get; set; }
#endregion
#region Construcors
public Repo(bool LazyLoaded)
{
_Context = new TContext();
_LazyLoaded = LazyLoaded;
_Context.ChangeTracker.LazyLoadingEnabled = LazyLoaded;
}
public Repo(DbContext context,bool LazyLoaded)
{
_Context = context;
_LazyLoaded = LazyLoaded;
_Context.ChangeTracker.LazyLoadingEnabled = LazyLoaded;
}
public Repo(IConfiguration configuration, string connectionString,bool LazyLoaded)
{
_Context = new TContext().GetInstance(configuration, connectionString);
_LazyLoaded = LazyLoaded;
_Context.ChangeTracker.LazyLoadingEnabled = LazyLoaded;
}
#endregion
}
我在第二个 class 中尝试的方法:
class UOW:IUOW
{
public UOW(Repo<DbContext> repo)
{
}
public void Commit()
{
}
public void RollBack()
{
}
}
但是我得到了这两个错误:
CS0311 The type 'Microsoft.EntityFrameworkCore.DbContext' cannot be used as type parameter 'TContext' in the generic type or method 'Repo'. There is no implicit reference conversion from 'Microsoft.EntityFrameworkCore.DbContext' to 'Microsoft.EntityFrameworkCore.IDbContextFactory<Microsoft.EntityFrameworkCore.DbContext>'.
CS0310 'DbContext' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TContext' in the generic type or method 'Repo'
我如何配置我的第二个 class 的构造函数以从 repo 泛型 class 接收对象?
您的代码存在多个问题,但要解决您在此处关注的错误,DbContext
不是 非抽象类型,因此您不能使用它与 new()
通用约束。它也没有实现 IDbContextFactory
。您可以做几件事。首先是使 UOW
通用并匹配相同的通用约束,如下所示:
class UOW<TContext> : IUOW
where TContext : DbContext, IDbContextFactory<TContext>, new()
{
public UOW(Repo<TContext> repo)
{
}
}
第二种方法是使用适用于所有这些约束的具体 class,例如:
class MyContext : DbContext, IDbContextFactory<MyContext>
{
public MyContext CreateDbContext()
{
throw new NotImplementedException();
}
}
class UOW : IUOW
{
public UOW(Repo<MyContext> repo)
// ^^^^^^^^^ Use the new object here
{
}
}
我有一个通用的 class 将扮演存储库的角色,我有第二个 class,第二个 class 应该从构造函数中的通用存储库接收一个对象.
通用 class(存储库):
public sealed class Repo<TContext> : IRepo<TContext>, IDisposable where TContext : DbContext, IDbContextFactory<TContext>, new()
{
#region properties
/// <summary>
/// Private DBContext property
/// </summary>
private DbContext _Context { get; } = null;
/// <summary>
/// Determine if Lazy Loading either activate or not
/// </summary>
private bool _LazyLoaded { get; set; }
#endregion
#region Construcors
public Repo(bool LazyLoaded)
{
_Context = new TContext();
_LazyLoaded = LazyLoaded;
_Context.ChangeTracker.LazyLoadingEnabled = LazyLoaded;
}
public Repo(DbContext context,bool LazyLoaded)
{
_Context = context;
_LazyLoaded = LazyLoaded;
_Context.ChangeTracker.LazyLoadingEnabled = LazyLoaded;
}
public Repo(IConfiguration configuration, string connectionString,bool LazyLoaded)
{
_Context = new TContext().GetInstance(configuration, connectionString);
_LazyLoaded = LazyLoaded;
_Context.ChangeTracker.LazyLoadingEnabled = LazyLoaded;
}
#endregion
}
我在第二个 class 中尝试的方法:
class UOW:IUOW
{
public UOW(Repo<DbContext> repo)
{
}
public void Commit()
{
}
public void RollBack()
{
}
}
但是我得到了这两个错误:
CS0311 The type 'Microsoft.EntityFrameworkCore.DbContext' cannot be used as type parameter 'TContext' in the generic type or method 'Repo'. There is no implicit reference conversion from 'Microsoft.EntityFrameworkCore.DbContext' to 'Microsoft.EntityFrameworkCore.IDbContextFactory<Microsoft.EntityFrameworkCore.DbContext>'.
CS0310 'DbContext' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TContext' in the generic type or method 'Repo'
我如何配置我的第二个 class 的构造函数以从 repo 泛型 class 接收对象?
您的代码存在多个问题,但要解决您在此处关注的错误,DbContext
不是 非抽象类型,因此您不能使用它与 new()
通用约束。它也没有实现 IDbContextFactory
。您可以做几件事。首先是使 UOW
通用并匹配相同的通用约束,如下所示:
class UOW<TContext> : IUOW
where TContext : DbContext, IDbContextFactory<TContext>, new()
{
public UOW(Repo<TContext> repo)
{
}
}
第二种方法是使用适用于所有这些约束的具体 class,例如:
class MyContext : DbContext, IDbContextFactory<MyContext>
{
public MyContext CreateDbContext()
{
throw new NotImplementedException();
}
}
class UOW : IUOW
{
public UOW(Repo<MyContext> repo)
// ^^^^^^^^^ Use the new object here
{
}
}