使用接口将数据库上下文注入 类
Inject database context into classes using interface
我希望将数据库上下文注入所有实现我的接口 similar to this post 的 classes。
我有什么
public abstract class Service
{
public Service(Context context)
{
Context = context;
}
public Context Context { get; }
}
每个服务 class 都会有一个带有方法的接口
interface IRecipeTypeIndexService
{
IEnumerable<RecipeType> GetAll();
}
所有服务 classes 将继承抽象 Service
class,所以我现在的具体 class 看起来像
public class RecipeTypesIndexService : Service, IRecipeTypeIndexService
{
public RecipeTypesIndexService(Context context) : base(context)
{
}
public IEnumerable<RecipeType> GetAll()
{
return Context.RecipeTypes.AsEnumerable();
}
}
我的 ninject 绑定看起来像
Kernel.Bind<DbContext>().ToSelf().InRequestScope();
Kernel.Bind<Service>().ToSelf().InRequestScope();
我想做的是让我的接口 IRecipeTypeIndexService
和我创建的其他服务接口继承另一个接口 IService
,即 Ninject 绑定到抽象 Service
class,所以所有实现 IWhateverService
的具体 class 必须有一个构造函数将数据库上下文注入基础 class,所以我的具体 class 看起来像这样:
public class RecipeTypesIndexService : IRecipeTypeIndexService
{
public RecipeTypesIndexService(Context context) : base(context)
{
}
public IEnumerable<RecipeType> GetAll()
{
return Context.RecipeTypes.AsEnumerable();
}
}
这可能吗?这是我第一次使用 Ninject 并且我是使用依赖注入的新手。
更新
事后我意识到这是不可能的。
因为我已经设置了 Ninject 以便在构造函数中具有上下文的任何地方都将具有已经初始化的上下文,所以我不需要抽象 class.
我的服务 class 将如下所示:
public class RecipeTypesIndexService : IRecipeTypeIndexService
{
private Context context { get; }
public RecipeTypesIndexService(Context context) : base(context)
{
this.context = context;
}
public IEnumerable<RecipeType> GetAll()
{
return context.RecipeTypes.AsEnumerable();
}
}
我根本不需要抽象基础class。
我希望将数据库上下文注入所有实现我的接口 similar to this post 的 classes。
我有什么
public abstract class Service
{
public Service(Context context)
{
Context = context;
}
public Context Context { get; }
}
每个服务 class 都会有一个带有方法的接口
interface IRecipeTypeIndexService
{
IEnumerable<RecipeType> GetAll();
}
所有服务 classes 将继承抽象 Service
class,所以我现在的具体 class 看起来像
public class RecipeTypesIndexService : Service, IRecipeTypeIndexService
{
public RecipeTypesIndexService(Context context) : base(context)
{
}
public IEnumerable<RecipeType> GetAll()
{
return Context.RecipeTypes.AsEnumerable();
}
}
我的 ninject 绑定看起来像
Kernel.Bind<DbContext>().ToSelf().InRequestScope();
Kernel.Bind<Service>().ToSelf().InRequestScope();
我想做的是让我的接口 IRecipeTypeIndexService
和我创建的其他服务接口继承另一个接口 IService
,即 Ninject 绑定到抽象 Service
class,所以所有实现 IWhateverService
的具体 class 必须有一个构造函数将数据库上下文注入基础 class,所以我的具体 class 看起来像这样:
public class RecipeTypesIndexService : IRecipeTypeIndexService
{
public RecipeTypesIndexService(Context context) : base(context)
{
}
public IEnumerable<RecipeType> GetAll()
{
return Context.RecipeTypes.AsEnumerable();
}
}
这可能吗?这是我第一次使用 Ninject 并且我是使用依赖注入的新手。
更新
事后我意识到这是不可能的。
因为我已经设置了 Ninject 以便在构造函数中具有上下文的任何地方都将具有已经初始化的上下文,所以我不需要抽象 class.
我的服务 class 将如下所示:
public class RecipeTypesIndexService : IRecipeTypeIndexService
{
private Context context { get; }
public RecipeTypesIndexService(Context context) : base(context)
{
this.context = context;
}
public IEnumerable<RecipeType> GetAll()
{
return context.RecipeTypes.AsEnumerable();
}
}
我根本不需要抽象基础class。