我目前遇到了一个问题,试图在每个被拦截的类实例中连接一个拦截器实例.
我正在InterceptorRegistrationStrategy中创建和建议并设置回调来解析内核中的拦截器(它有一个注入构造函数).请注意,我只能在回调中实例化拦截器,因为InterceptorRegistrationStrategy没有引用内核本身.
IAdvice advice = this.AdviceFactory.Create(methodInfo);
advice.Callback = ((context) => context.Kernel.Get<MyInterceptor>());
this.AdviceRegistry.Register(advice);
Run Code Online (Sandbox Code Playgroud)
我正在获得每个方法的拦截器实例.
是否有任何方法可以为每个被拦截的类型实例创建一个拦截器实例?
我在考虑命名范围,但截获的类型和拦截器不会互相引用.
ninject ioc-container interceptor ninject-2 ninject-extensions
我刚刚将Ninject.MVC3 nuget包(v2.2.2.0)添加到我的ASP.NET MVC 3应用程序中.
我似乎以两种不同的方式设置了我的ASP.NET MVC 3应用程序:
NinjectHttpApplicationGlobal.asax.cs 继承NinjectModule于RegisterServices在NinjectMVC3.cs方法而现在我正试图弄清楚这里引用的博客条目.似乎在说还有另一种方法.一些关于.我迷路了.NinjectHttpApplicationModule
我该如何修改NinjectMVC.cs和Global.asax.cs文件?我现在拥有的是粘贴在下面.
NinjectMVC3.cs
[assembly: WebActivator.PreApplicationStartMethod(typeof(TennisClub.App_Start.NinjectMVC3), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(TennisClub.App_Start.NinjectMVC3), "Stop")]
namespace MyApp.App_Start
{
using System.Reflection;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Mvc;
using TennisClub.Configuration;
public static class NinjectMVC3
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// …Run Code Online (Sandbox Code Playgroud) 拥有一个通用的存储库
public class Repository<T>
where T: Entity<T>
{
/*anything else*/
}
Run Code Online (Sandbox Code Playgroud)
应该是每个聚合根的具体存储库
class ProductRepository : Repository<Product>
{
}
class CategoryRepository : Repository<Category>
{
}
Run Code Online (Sandbox Code Playgroud)
被创造?
另外,我如何使用DI(Ninject)与存储库的通用实现.
样品是apreciated!
谢谢!
我可能会遗漏一些明显的东西......
但是当我学会欣赏IoC和ctor注入的荣耀时,我很难将它与对象图序列化协调起来.这两种模式是否兼容?为什么或者为什么不)?
假设有:
public class Foo
{
#region invariants
private readonly List<IBar> _bars;
private readonly Guid _id;
#endregion
public Foo( List<IBar> bars, Guid id )
{
_bars = bars.CannotBeNull("bars");
_id = id.CannotBeNull("id");
}
public List<IBar> Bars { get { return _bars; } }
//some other state I want serialized
}
public static class Ex
{
public static T CannotBeNull<T>( this T obj, string paramName = "" )
{
if ( null == obj ) throw new ArgumentNullException(paramName);
return obj;
} …Run Code Online (Sandbox Code Playgroud) architecture dependency-injection ninject inversion-of-control constructor-injection
我正在使用Ninject模块将不同类型绑定到相应的接口.
注入将在类的构造函数中进行.问题是该类有另一个带有签名的构造函数,包括Func.
Ninject很困惑,并把它扔向我:
使用ClassTest的隐式自绑定激活ClassTest时出错.几个构造函数具有相同的优先级.请使用ToConstructor语法指定构造函数或添加Inject属性.
请参阅下文如何完成绑定以及如何进行注射:
this.Bind<InterfaceA>().To<ClassA>();
...
public class ClassTest
{
public ClassTest(InterfaceA a)
{
}
public ClassTest(Func<ClassB> funcB)
{
}
}
...
var giveMeTest = kernel.Get<ClassTest>(); // exception thrown
}
Run Code Online (Sandbox Code Playgroud)
看来Func是罪魁祸首,你能解释一下为什么Ninject会感到困惑吗?
谢谢
我正在使用MVC4连接Ninject并让它正在努力实际解决依赖关系.但是,我得到以下异常:
找不到方法:'System.Web.Http.Services.DependencyResolver System.Web.Http.HttpConfiguration.get_ServiceResolver()'.
任何人遇到这个并有一个解决方案吗?
我希望Ninject为实现通用接口的特定程序集中的所有类型创建Bindings,而不是在运行时全部指定它们.有点像开放式仿制药在Autofac中的运作方式.
这就是我想出来的......
kernel.Bind(x => x.FromThisAssembly()
.SelectAllClasses()
.Where(t => t.IsAssignableFrom(
typeof(ICommandHandler<>)))
.BindAllInterfaces());
Run Code Online (Sandbox Code Playgroud)
调用下面的方法,我希望实现所有类型的数组,ICommandHandler<T>但它不产生任何...
public void Process<TCommand>(TCommand command)
where TCommand : ICommand
{
var handlers =
_kernel.GetAll<ICommandHandler<TCommand>>();
foreach(var handler in handlers)
{
handler.Handle(command);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有现成的方法来实现这一目标?或者我是否需要使用convention API自行推送?
这似乎是一种相当普遍的模式,并且想知道如果不编写自己的实现就可以实现这一点.
我在Castle Windsor注入了以下接口.我如何在Ninject中做同样的事情?
container.Register(
AllTypes.FromAssemblyNamed("Apps.Web")
.BasedOn(typeof(ICommandHandler<>))
.WithService.FirstInterface());
Run Code Online (Sandbox Code Playgroud)
我试过了:
this.Bind(x => x.FromAssembliesMatching("Apps.Web.dll")
.Select(y => y.Namespace.EndsWith("Handlers"))
.BindSingleInterface());
Run Code Online (Sandbox Code Playgroud)
但是没有将Object引用设置为对象错误的实例.
我正在尝试建立一个新项目,并且我添加了一个新类MembershipService,它需要在它的构造函数中传递HttpContext.
在之前的项目中,我使用了代码
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IMembershipService>()
.To<MembershipService>()
.InRequestScope()
.WithConstructorArgument("context", HttpContext.Current);
....
}
Run Code Online (Sandbox Code Playgroud)
然而,在新项目中,我正在使用Ninject Modules,在对StackOverflow和Google进行一些搜索之后,我提出了以下代码:public class ServiceHandlerModule:NinjectModule {
public override void Load()
{
Bind<IMembershipService>()
.To<MembershipService>()
.WithConstructorArgument("context", ninjectContext=> HttpContext.Current);
this.Kernel.Bind(x =>
{
x.FromAssemblyContaining(typeof(NinjectWebCommon))
.SelectAllClasses()
.Where(t => t != typeof(MembershipService))
.BindDefaultInterface();
});
this.Kernel.Bind(x =>
{
x.FromAssemblyContaining<BrandServiceHandler>()
.SelectAllClasses()
.Where(t => t != typeof(MembershipService))
.BindDefaultInterface();
});
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息:Ninject.ActivationException:激活字符串时出错没有匹配的绑定可用,并且该类型不可自我绑定.激活路径:
5)将依赖字符串注入到HttpRequest类型的构造函数的参数filename中
4)将依赖关系HttpRequest注入到HttpContext类型的构造函数的参数请求中
3)将依赖关系HttpContext注入到MembershipService类型的构造函数的参数httpContext中
2)将依赖关系IMembershipService注入到HomeController类型的构造函数的参数membershipService中
1)请求HomeController
谁能指出我哪里出错了?
谢谢,约翰
我的班级结构是:
public class PhotoService {
private IRepository _repo;
private DTConfig _config;
public PhotoService(IRepository repo, DTConfig config)
{
_repo = repo;
_config = config;
}
}
public class DTConfig
{
private int _accountId;
public DTConfig(int accountId)
{
_accountId = accountId;
}
}
Run Code Online (Sandbox Code Playgroud)
我的 Ninject 绑定是这样的:
var kernel = new StandardKernel();
kernel.Bind<IPhotoService>().To<PhotoService>();
kernel.Bind<IRepository>().To<Repository>();
kernel.Bind<DTConfig>().ToSelf();
Run Code Online (Sandbox Code Playgroud)
现在我想要的是accountId在创建PhotoService实例时将其作为参数传递
var photo = kernel.Get<IPhotoService>();
Run Code Online (Sandbox Code Playgroud)
如何在创建 PhotoService 实例时将参数传递给 DTConfig。accountId 是从服务中获取的,在编译时不可用。
如果需要任何其他信息,请随时发表评论。
ninject ×10
c# ×4
.net ×3
asp.net-mvc ×2
architecture ×1
c#-4.0 ×1
generics ×1
httpcontext ×1
interceptor ×1
ninject-2 ×1
nuget ×1
repository ×1