我正在尝试使用ninject注入一个属性。考虑到下面ninject模块中的两个绑定,我希望将ConcreteDependency注入到B中。
然而,WhenInjectedInto似乎并没有考虑注入的类型,只考虑了目标的声明类型(本例中是属性)。
有没有办法实现我所期望的行为?
static void Main(string[] args)
{
var kernel = new StandardKernel(new TestModule());
var b = kernel.Get<B>();
var c = kernel.Get<C>();
}
我在自定义角色提供程序中使用属性注入,如下所示:
public class MyRoleProvider : RoleProvider
{
[Inject]
public IRoleRepository RoleRepository { get; set; }
...
}
我的ninject模块:
public class MyNinjectModule : NinjectModule
{
public override void Load()
{
Bind<IRoleRepository>().To<RoleRepos
我有一些代码,如下所示。我刚接触IoC和Ninject,不知道如何将IOtherClass注入到SomeService中,这样Parallel.ForEach才能正常工作。我唯一的另一个猜测是将Parallel.ForEach移到DoWork方法中?
谢谢你的帮助。
public class SomeService
{
Parallel.Foreach(items, item =>
new OtherClass.DoWork(item);
}
public class OtherClass:IOtherClass
{
pub
我有一个带有构造函数的控制器,如下所示:
Public Sub New(Service As ICategoryService)
服务的构造函数如下所示:
Public Sub New(Repository As IRepository(Of Category), IValidationDictionary)
我有一个实现IValidationDictionary的名为ModelStateWrapper的类:
Public Class ModelStateWrapper
Implements IValidationDictionary
Private ModelState A
我要进入尼尼特了。简化我的真实场景,我有一个带有属性注入的A类:
public class NinjectBindings : NinjectModule
{
public override void Load()
{
Bind<IMasterViewModel>().To<IQMasterViewModel>();
}
}
public class A
{
[Inject]
public IMasterViewModel _viewModel
{
get;
set;
我正在使用Ninject从内核中检索我的DataContext,我想知道Ninject是否会自动处理DataContext,或者他是如何处理dispose()行为的。根据我自己的经验,我知道处理数据上下文是相当重要的,并且每当您创建一个datacontext的直接对象(如: new DataContext())时,您应该使用一个using()块。
因此,我的问题是:当我从内核检索我的DataContext时,我应该仍然使用using()块吗?或者是Ninject帮我解决这个问题?
在MVC Web应用程序中,我没有遇到注入业务逻辑类的任何问题。但是我想将数据访问类注入到业务逻辑类的构造函数中。逻辑层是一个类库,没有Program.cs类或入口点。
数据访问类
public class DataAccessClass
{
public void Insert(Product product)
{
new SqlObj().Insert(Product);
}
}
数据访问类接口
public Interface IDataAccessClass()
{
void Insert(Product product);
}
编辑:我忘了在这里将内核移到一个非泛型父类中,并提供了一个虚拟方法来访问它。我确实意识到,下面的示例将创建过多的内核实例。
在过去的一周里,我刚刚学习了如何做注入,下面是我目前如何设置的:
using Ninject;
using System.Reflection;
namespace Infrastructure
{
public static class Inject<T>
{
static bool b = Bootstrap();
static IKernel kernel;
static bool Bo
我的应用程序正在使用MEF从外部程序集中导出一些类。这些类是为构造函数注入而设置的。我面临的问题是,当我试图访问这些类时,MEF会尝试实例化它们。有没有一种方法可以让Ninject负责类的实例化?
IEnumerable<Lazy<IMyInterface>> controllers =
mefContainer.GetExports<IMyInterface>();
// The following line throws an error because MEF is
// trying to instantiate a class tha
我们正在尝试使用WebAPI管道的Owin中的Ninject。根据,我们已经完成了所有的设置,但是我们不能让InRequestScope()工作。
以下是startup.cs的重要部分
public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
// Web API routes
config.MapHttpAttributeRoutes();
我正在开发一个应用程序,该应用程序被拆分为多个程序集。每个程序集都向外部世界提供接口,实例是通过基于Ninject的工厂生成的.
啊,好吧,让有代码。这是来自执行程序集的。
public class IsolationTestModule : NinjectModule
{
public override void Load()
{
ServiceFactory sf = new ServiceFactory();
Bind<IService>().ToMethod(context=>sf.CreatService()).InS
我目前使用的代码如下:
public class MyProvider
{
public MyProvider()
{
}
public void Fetch()
{
using (PopClient popClient = new PopClient())
{
....
}
}
}
因为我希望能够对Fetch方法进行单元测试,并且不能模拟PopClient,所以我创建了一个调用PopClient的接口和包装类。我更新的代码看起来像这样:
public class MyPr
我正在尝试使用MVC4项目的MVC4中的依赖解析器来获取对Ninject.Extensions.Logging.ILogger的引用,但它正在抛出一个NullReferenceException。我在项目的其他地方使用构造函数注入来获取ILogger的实例,这是完美无缺的。
protected void Application_EndRequest()
{
if (Context.Response.StatusCode == 404)
{
Response.Clear();
var rd = new RouteData();
r
对于使用ASP.Net包进行依赖注入的ASP.Net MVC3应用程序,我使用Ninject MVC3(版本3.0.0.0)进行安装。
下面是Global.asax的更改:
public class MvcApplication : NinjectHttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static v