我做了我的研究,但是我找不到任何我想做的具体例子.
我成功地将Ninject实现到了我的MVC项目中.一切都很完美.但是,我想做最后一步.
到现在为止,我一直在这样工作(正常的DI模式):
public class myController : Controller
{
private iMyInterface myRepository;
public myController(iMyInterface myRepository)
{
this.myRepository = myRepository;
}
public ActionResult list(){
return view(myRepository.getMyList())
}
// rest o the code ...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是; 有办法做这样的事情吗?(存储库"Generator")
public class myController : Controller
{
private iMyInterface myRepository = specialClass.GetMyRepository();
public ActionResult list(){
return view(myRepository.getMyList()) }
// rest o the code ...
}
Run Code Online (Sandbox Code Playgroud)
我知道我正在写一个无意义的代码,但我的想法是能够做类似的事情.
有什么建议?
在ASP.NET MVC3应用程序中,我通过a初始化Ninject IoC容器
[assembly : WebActivator.PreApplicationStartMethod( typeof (NinjectMVC3), "Start" )]
Run Code Online (Sandbox Code Playgroud)
一个类NinjectMVC3
负责我的IoC容器Kernel
初始化.
在调用它之后,所有通过构造函数变量声明可解析依赖关系的控制器都可以很好地解析它们.
但我需要在Global.asax
Application_Start
方法中使用已解析的依赖项,将其提供给我的一些自定义全局过滤器?如何Application_Start
在我的场景中解决依赖关系?
我真的不知道为什么Ninject抛出这个错误,因为我已经使用它一段时间了.
它说我没有实现一种方法,但它确实存在.
从程序集'FollowUp.Repository.Impl,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'类型'FollowUp.Repository.Impl.ApplicationLogRepository'中的方法'GetApplicationLogEntries'没有实现.
我在https://www.sugarsync.com/pf/D6486369_1701716_00156上制作了最小的复制品
有ninject经验的人可以帮我吗?非常感激.
亲切的问候,汤姆
Lazy有几个构造函数,通过它们可以控制Lazy实例的线程行为.Ninject使用什么构造函数来创建注入的Lazy实例?我如何指定Ninject必须使用哪个构造函数?如果可能的话.
我有 WPF 中的用户控制代码(如下)。我使用 nInject 作为 IocContainer。我在 App 类的 OnStartup 事件中初始化 ioc。
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var iocContainer = IocContainer.Get();
iocContainer.Bind<CreateRemindPopup>().To<CreateRemindPopup>();
iocContainer.Bind<MainWindow>().To<MainWindow>();
Current.MainWindow = iocContainer.Get<MainWindow>();
Current.MainWindow.Show();
}
Run Code Online (Sandbox Code Playgroud)
如果我删除无参数构造函数,则在应显示控件时会出现 NullReferenceException 异常。当存在无参数构造函数时,不会执行显示内容的代码。
我的问题是如何强制 WPF 使用参数执行构造函数?我不想删除无参数构造函数,因为这样我就在 VisualStudio 中丢失了设计器。
public partial class RemindersListing : UserControl
{
private readonly IReminderReadLogic _reminderReadLogic;
public ObservableCollection<Reminder> Reminders { get; set; }
public RemindersListing()
{
}
public RemindersListing(IReminderReadLogic reminderReadLogic)
{
_reminderReadLogic = reminderReadLogic;
InitializeComponent();
var list = _reminderReadLogic.Get();
Reminders = new ObservableCollection<Reminder>(list);
}
}
Run Code Online (Sandbox Code Playgroud) 我在公共库中使用依赖解析器接口作为抽象.这使我可以灵活地切换容器.鉴于下面的接口和静态IoC类,我需要使用不带IKernel
全局asax 的工厂初始化IoC .
请参阅下面我如何使用NinjectHttpApplication
base创建Ninject内核.
这是问题所在; 该IKernel
物业CtsDependencyFactory
始终null
.
Ninject不会像这样注射自己吗?我应该使用不同的方法传递IKernel
给工厂吗?我找不到像静态类ObjectFactory
中StructureMap
的Ninject得到一个参考解析器.
任何帮助表示赞赏.
public interface IResolveDependency
{
T Resolve<T>();
T Resolve<T>(params object[] parameters);
T Resolve<T>(string name);
T Resolve<T>(string name, params object[] parameters);
object Resolve(Type type);
IEnumerable<T> ResolveAll<T>();
void Clear();
}
public interface IResolveDependencyFactory
{
IResolveDependency CreateInstance();
}
public class CtsDependencyResolver : IResolveDependency
{
private readonly IKernel m_kernel;
public CtsDependencyResolver(IKernel kernel)
{
m_kernel = kernel;
}
#region Implementation of IResolveDependency …
Run Code Online (Sandbox Code Playgroud) 我的项目错误与global.asax Application_Start调用:ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
namespace SportsStore.WebUI.Infrastructure
{
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
// put additional bindingers here
}
}
Run Code Online (Sandbox Code Playgroud)
错误堆栈:
Locating source for 'c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Multimap.cs'.
Checksum: MD5 {de 1d cc 43 b7 22 44 a5 8d 8b 50 ed 23 dc 4 28}
The file 'c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Multimap.cs' does …
Run Code Online (Sandbox Code Playgroud)