dependency-injection – Ninject:构造函数参数
我正在使用Ninject和ASP.NET MVC 4.我正在使用存储库并希望进行构造函数注入以将存储库传递给其中一个控制器.
这是我的Repository界面: public interface IRepository<T> where T : TableServiceEntity { void Add(T item); void Delete(T item); void Update(T item); IEnumerable<T> Find(params Specification<T>[] specifications); IEnumerable<T> RetrieveAll(); void SaveChanges(); } 下面的AzureTableStorageRepository是IRepository< T>的实现: public class AzureTableRepository<T> : IRepository<T> where T : TableServiceEntity { private readonly string _tableName; private readonly TableServiceContext _dataContext; private CloudStorageAccount _storageAccount; private CloudTableClient _tableClient; public AzureTableRepository(string tableName) { // Create an instance of a Windows Azure Storage account _storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString); _tableClient = _storageAccount.CreateCloudTableClient(); _tableClient.CreateTableIfNotExist(tableName); _dataContext = _tableClient.GetDataServiceContext(); _tableName = tableName; } 请注意tableName参数,因为我使用通用表存储库将数据持久保存到Azure. 最后我有以下控制器. public class CategoriesController : ApiController { static IRepository<Category> _repository; public CategoriesController(IRepository<Category> repository) { if (repository == null) { throw new ArgumentNullException("repository"); } _repository = repository; } 现在我想将一个存储库注入控制器.所以我创建了一个包含绑定的模块: /// <summary> /// Ninject module to handle dependency injection of repositories /// </summary> public class RepositoryNinjectModule : NinjectModule { public override void Load() { Bind<IRepository<Category>>().To<AzureTableRepository<Category>>(); } } 模块的加载在NinjectWebCommon.cs中完成 /// <summary> /// Creates the kernel that will manage your application. /// </summary> /// <returns>The created kernel.</returns> private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); RegisterServices(kernel); return kernel; } /// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { // Load the module that contains the binding kernel.Load(new RepositoryNinjectModule()); // Set resolver needed to use Ninject with MVC4 Web API GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel); } DependencyResolver的创建是因为Ninject的DependencyResolver实现了System.Web.Mvc.IDependencyResolver,并且无法将其分配给WebApi应用程序的GlobalConfiguration.Configuration. 所有这一切,Ninject部分实际上是在Controller中注入正确的类型,但是Ninject不能在AzureTableRepository的构造函数中注入tableName参数. 在这种情况下我怎么能这样做?我已经查阅了很多文章和ninject文档,看看我如何使用参数,但我似乎无法让它工作. 任何帮助,将不胜感激. 解决方法
我使用WithConstructorArgument()方法,如…
Bind<IRepository<Category>>().To<AzureTableRepository<Category>>() .WithConstructorArgument("tableName","categories"); 存储库设计的其余部分可能是另一个问题.恕我直言创建一张桌子或在ctor中做任何繁重的工作似乎是一件很大的事. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 在IIS中将WebAPI添加为子/嵌套应用程序
- asp.net – .NET 4.5中的样式包和CSS中的图标
- asp.net – C#Web API模型绑定提供程序应如何工作?
- asp.net – 从aspx访问应用程序设置并添加连接文本
- asp.net-web-api – ASP.NET Web API – 404删除
- entity-framework – 如何将OData查询与DTO映射到EF实体?
- ASP.NET httpHandlers和处理程序
- asp.net阻止表单提交两次
- asp.net – SQL网络接口,错误:26 – 定位服务器/实例指定错
- asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码
- asp.net-mvc – ASP.Net MVC 4.在单独的程序集中
- asp.net-mvc – ASP.NET MVC 3在文章视图中添加注
- 如何在ASP.NET中的主题之间共享.skin主题文件?
- asp.net-mvc – 集成测试ASP.NET MVC应用程序
- asp.net – 无法加载文件或程序集“Oracle.DataA
- asp.net-mvc – 控制器操作无法从JSON读取Guid P
- asp.net-mvc – Razor View语法无法识别HTML属性
- asp.net-core – 将命名空间添加到ASP.NET MVC 6
- asp.net – 在WAP转换期间无法解析.aspx
- asp.net-mvc – 单个控制器站点的ASP.NET MVC路由