我刚刚开始使用Ninject 2.0和ASP.NET MVC 2.因此,我有一个接口IMongoRepository和类MongoRepository.
MongoRepository接收参数字符串集合.
根据我想要使用的集合,我在MongoRepository的参数中传入一个不同的值.我希望我正确地说这个,但是我如何根据我使用的控制器映射不同的参数?
例如,在Article控制器中我会调用:
_articlesRepository = new MongoRepository("Articles");
Run Code Online (Sandbox Code Playgroud)
在PageController中我会调用:
_pagesController = new MongoRepository("Pages");
Run Code Online (Sandbox Code Playgroud)
我想做的只是做构造函数注入,然后传入IMongoRepository.任何想法或建议?
顺便说一句,我只是在学习IOC/DI; 所以,我愿意接受IOC忍者的任何提示!谢谢!
dependency-injection ninject constructor-injection ninject-2 asp.net-mvc-2
我有一个使用Asp.net MVC2的Web应用程序.我将它升级到MVC 3,现在我发现OutputCache功能不再起作用了.我创建了一个简单的Test动作,如下所示.
[OutputCache(Duration = 1000000, VaryByParam = "none")]
public virtual ActionResult CacheTest(string name)
{
string message = string.Format("{0}: Time is {1}", name, DateTime.Now.ToLongTimeString());
ViewData.Add("Message", message);
return View();
}
Run Code Online (Sandbox Code Playgroud)
这总是给出当前时间,表明它没有被缓存.我在这里错过了什么吗?
更多信息:如果我创建一个新的Mvc3应用程序,它工作正常.它只在升级的应用程序中,我有这个问题.
更新:我也在使用Ninject.如果我停止使用Ninject OutputCache开始工作.
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}.aspx/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } …Run Code Online (Sandbox Code Playgroud) 我一直在我的ASP.NET MVC3门户中使用Ninject IoC容器.每当我DbContext在PerThread范围内注入实体框架时,我的数据都不一致,在我对实体进行更改等后的一段时间内,更改不会显示.
在我切换IoC配置以解决DbContext每个请求(PerRequestScope())的实例的新副本之后,所有问题都消失了.
那么在MVC3应用程序中使用PerRequest注入策略是绝对必须的吗?
我是使用ninject和Dependency Injection的新手,并且在使用它时遇到了问题.
我尝试在我的类库中使用Ninject,并构建集成测试.现在,我在很多例子中看到,使用ninject只是指定了DI模块,如下所示:
Public Class DIModule : NinjectModule
public override void Load()
{
Bind<IUSAServices>().To<USAServices>();
}
Run Code Online (Sandbox Code Playgroud)
然后在我的测试类中,我尝试调用我的依赖是这样的:
[TestClass]
public class USAIntegrationTests
{
private readonly IUSAServices _usaService;
public USAIntegrationTests(IUSAServices usaServices)
{
_usaService = usaServices;
}
[TestMethod]
public void ValidateUserTests()
{
Assert.IsTrue(_usaService.ValidateUser("username1", "password1"));
}
}
Run Code Online (Sandbox Code Playgroud)
并得到此错误:
Unable to get default constructor for class USATests.IntegrationTests.USAIntegrationTests.
Run Code Online (Sandbox Code Playgroud)
但是,我阅读文档并尝试这样:
[TestClass]
public class USAIntegrationTests
{
private readonly IUSAServices _usaService;
public USAIntegrationTests()
{
using (IKernel kernel = new StandardKernel(new DIModule()))
{
_usaService = kernel.Get<IUSAServices>();
}
}
[TestMethod]
public void …Run Code Online (Sandbox Code Playgroud) 我对Ninject不太熟悉,所以我可能在这里有一个完全错误的概念,但这就是我想要做的.我有一个多租户Web应用程序,并希望注入一个不同的类对象,具体取决于用于访问我的网站的URL.
虽然也许我可以在绑定中使用.When(),但你得到了这个想法:
private static void RegisterServices(IKernel kernel)
{
var currentTenant = TenantLookup.LookupByDomain(HttpContext.Current.Request.Url.Host.ToLower());
if (currentTenant.Foldername == "insideeu")
{ kernel.Bind<ICustomerRepository>().To<AXCustomerRepository>(); }
else
{ kernel.Bind<ICustomerRepository>().To<CustomerRepository>(); }
...
Run Code Online (Sandbox Code Playgroud)
问题是此时HttpContext.Current为null.所以我的问题是如何在NinjectWebCommon.RegisterServices中获取HttpContext数据.我对Ninject可能出错的任何方向都会非常感激.
谢谢
任何人都可以提供帮助,我在使用Ninject和NSubstitute之间可用的自动模拟时遇到问题,实际上这个包是一个ninject打包调用Ninject.MockingKernel.NSubstitute,应该允许我使用Ninject创建模拟并返回注入模拟的 实例.
Moq和Rhinomocks似乎有一些例子,但我没有看到NSubstitute.
到目前为止我所拥有的是什么
this.kernel = new NSubstituteMockingKernel();
var summaryService = this.kernel.GetMock<IMyService>(); // GetMock not available
Run Code Online (Sandbox Code Playgroud)
有人用吗?
我有一个名为的接口IXMLModelsRepository,我有一个具体的实现XMLModelsRepository,只需从XML文件中读取.
但是,我想改进功能,我想暂时将元素缓存到Dictionary<>列表中.
我不想修改现有的XMLModelsRepository,但我想创建一个新的类,在顶部添加缓存功能.
如何使用Ninject接口绑定到两个具体实现?
// the interface i am working with
public interface IXMLModelsRepository
{
Product GetProduct(Guid entity_Id);
}
// concrete implementation that reads from XML document
public class XMLModelsRepository : IXMLModelsRepository
{
private readonly XDocument _xDoc = LoadXMLDocument();
public Product GetProduct(Guid entity_Id)
{
return _xDoc.Element("root").Elements("Product").Where(p => p.Attribute("Entity_Id").Value == entity_Id.ToString();
}
}
// concrete implementation that is only responsable of caching the results
// this is the class that i …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Adam Freeman的"Pro ASP.NET MVC 5"一书来学习ASP.NET MVC.不幸的是,所有使用Ninject的项目都会抛出相同的错误
Ninject.dll中出现"System.IO.FileLoadException"类型的异常,但未在用户代码中处理
附加信息:无法加载文件或程序集'System.Web.Mvc,> Version = 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'或其中一个>依赖项.定位程序集的清单定义与程序集>引用不匹配.(HRESULT异常:0x80131040)
这与此主题中讨论的问题完全相同,
在mvc 5项目中安装ninject mvc 3后出现的问题
但提供的解决方案对我不起作用.
我已经尝试过目标平台4.5和4.5.1,Ninject MVC3和MVC5.我也插入了这个片段
<runtime>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</runtime>
Run Code Online (Sandbox Code Playgroud)
在Web.config文件中,没有任何影响.
问题必须在Ninject MVC3和MVC5包中.无论何时安装这些软件包,任何调用Ninject.StandardKernel()抛出异常,无论Global.asax是从新的NinjectWebCommon.cs还是从任何其他代码片段开始的(当然,这对于这种异常无关紧要,但在此线程中使用Ninject时出错)使用ASP.NET V4,有人建议该错误可能与使用Global.asaxNinject连接到应用程序有关.
我已经没想完了.有人可以帮忙吗?
到目前为止,我一直在我的项目存储库模式和依赖注入中实现.我为ORM使用Entity Framework,并且还有一个管理我的存储库的服务层.
我有点想做一些对手头的问题不那么重要的事情.我不太可能更改我的数据库或ORM.对于小型项目,我不需要实施测试.
但我喜欢每次获取数据时都不向数据库发送请求的想法,如果它已经在内存中从那里获取数据.
我的问题是:如果我在Asp.Net MVC应用程序中使用Entiry Framework,而不是使用存储库模式,但仍然在EF的DBContext上使用DI,我可以通过阻止往返数据库服务器读取来获得性能的好处吗?
通过在DbContext上实现DI,我的意思是Ninject示例:
kernel.Bind<MyDBContext>().ToSelf().InRequestScope();
Run Code Online (Sandbox Code Playgroud)
谢谢
asp.net-mvc entity-framework dependency-injection ninject ninject.web.mvc
我有一个习惯IDependencyResolver:
internal class NinjectResolver : IDependencyResolver
{
private IKernel _kernel;
internal NinjectResolver(params ApplicationModule[] modules)
{
_kernel = new StandardKernel(modules);
}
public IDependencyScope BeginScope()
{
return this;
}
public object GetService(Type serviceType)
{
return _kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _kernel.GetAll(serviceType);
}
protected void Dispose(bool disposing)
{
if(disposing)
{
if(_kernel != null && !_kernel.IsDisposed)
{
_kernel.Dispose();
_kernel = null;
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppresFinalize(this);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在这样注册:
public static void RegisterModules(HttpConfiguration configuration, params …Run Code Online (Sandbox Code Playgroud) ninject ×10
asp.net-mvc ×4
asp.net ×3
c# ×3
automocking ×1
c#-4.0 ×1
caching ×1
mocking ×1
ninject-2 ×1
nsubstitute ×1
owin ×1