Property Injection in Asp.Net Core (转载)
问:
I am trying to port an asp.net application to asp.net core. I have property injection (using ninject) on my UnitOfWork implementation like this.
[Inject]
public IOrderRepository OrderRepository { get; set; }
[Inject]
public ICustomerRepository CustomerRepository { get; set; }
Is there a way to achieve the same functionality using build in DI on .net core? Also is it possible to use convention based binding?
答:
No, the built-in DI/IoC container is intentionally kept simple in both usage and features to offer a base for other DI containers to plug-in. (意思就是.NET Core内置的DI包Microsoft.Extensions.DependencyInjection,现在不支持属性注入,只支持构造函数注入)
So there is no built-in support for: Auto-Discovery, Auto-Registrations, Decorators or Injectors, or convention based registrations. There are also no plans to add this to the built-in container yet as far as I know.
You'll have to use a third party container with property injection support.
Please note that property injection is considered bad in 98% of all scenarios, because it hides dependencies and there is no guarantee that the object will be injected when the class is created.
With constructor injection you can enforce this via constructor and check for null and the not create the instance of the class. With property injection this is impossible and during unit tests its not obvious which services/dependencies the class requires when they are not defined in the constructor, so easy to miss and get NullReferenceExceptions.
The only valid reason for Property Injection I ever found was to inject services into proxy classes generated by a third party library, i.e. WCF proxies created from an interface where you have no control about the object creation.
Avoid it everywhere else.
Property Injection in Asp.Net Core (转载)的更多相关文章
- Dependency Injection in ASP.NET Core
Transient – A new instance of the service is created each time it is requested. It can be used for s ...
- 浅谈ASP.NET Core中的DI
DI的一些事 传送门马丁大叔的文章 什么是依赖注入(DI: Dependency Injection)? 依赖注入(DI)是一种面向对象的软件设计模式,主要是帮助开发人员开发出松耦合的应用程序 ...
- DI in ASP.NET Core
.NET-Core Series Server in ASP.NET-Core DI in ASP.NET-Core Routing in ASP.NET-Core Error Handling in ...
- ASP.NET Core MVC 控制器创建与依赖注入
本文翻译自<Controller activation and dependency injection in ASP.NET Core MVC>,由于水平有限,故无法保证翻译完全准确,欢 ...
- Advanced Architecture for ASP.NET Core Web API
转自: https://www.infoq.com/articles/advanced-architecture-aspnet-core ASP.NET Core's new architecture ...
- 关于单元测试的思考--Asp.Net Core单元测试最佳实践
在我们码字过程中,单元测试是必不可少的.但在从业过程中,很多开发者却对单元测试望而却步.有些时候并不是不想写,而是常常会碰到下面这些问题,让开发者放下了码字的脚步: 这个类初始数据太麻烦,你看:new ...
- ASP.NET Core 2 学习笔记(四)依赖注入
ASP.NET Core使用了大量的依赖注入(Dependency Injection, DI),把控制反转(Inversion Of Control, IoC)运用的相当巧妙.DI可算是ASP.NE ...
- ASP.NET Core 中的依赖注入
目录 什么是依赖注入 ASP .NET Core 中使用依赖注入 注册 使用 释放 替换为其它的 Ioc 容器 参考 什么是依赖注入 软件设计原则中有一个依赖倒置原则(DIP),为了更好的解耦,讲究要 ...
- Introducing ASP.NET Core: The New ASP.NET in Town!
The new version of ASP.NET is called ASP.NET Core (a.k.a ASP.NET 5) and it has the most significant ...
随机推荐
- ida不错的插件记录
IDASkins 地址 https://github.com/zyantific/IDASkins 作用 ida黑色皮肤插件 IDAFuzzy 地址 https://github.com/Ga-ryo ...
- 前端 css+js实现返回顶部功能
描述: 本文主要是讲,通过css+js实现网页中的[返回顶部]功能. 实现代码: HTML: <div> <button onclick="returnTop()" ...
- redis 命令clear、set、get、del、rename、keys *、exists、type、expire、expireat、persist、ttl、move、select
清屏 clear 新增/修改set 查询get 删除del 修改key rename old new 查询所有的key keys *查询一个key是否存在 exists,有返回1,没有返回0查询值的类 ...
- 如何让chrome浏览器自动翻译
我用的chrome浏览器,最初用的时候浏览器默认自动翻译英文,我感觉很麻烦.所以我选择了一律不翻译.但是我当我想翻译的的时候又不知道怎么操作.一直郁闷到现在.今天我突然发现了一个方法可以让你的浏览器自 ...
- svn回退到具体的版本
svn回退到具体的版本 找到项目的版本号 命令行中输入相关命令 到指定地点找到项目即可
- json格式转换(json,csjon)(天气预报)
json格式数据默认为string,可以使用eval()函数或者json模块将其转换为dict.标准Json字符串必须使用双引号(")而不能使用单引号('),否则从字符串转换成dict类型会 ...
- Python学习---IO的异步[twisted模块]
安装twisted模块 Linux: pip3 install twisted Window: a. http://www.lfd.uci.edu/~gohlke/pythonlibs/#twiste ...
- 所有文章的测试Demo
Mqtt C++ Client 测试Demo https://pan.baidu.com/s/1Ue00GYv2SUd8aTquhvOW1w
- nodejs API(一)
不要注重版本 URL 官网所在位置:https://nodejs.org/dist/latest-v8.x/docs/api/url.html URL网址解析的好帮手: url有三个可调用的方法:ur ...
- 最强自定义PHP集成环境,系统缺失dll和vc也能正常运行
PHPWAMP支持iis.apache.nginx等web服务器,并全部支持php多版本同时运行,无限自定义mysql.php PHPWAMP支持32和64,支持自定义自动匹配系统所需dll和vc,纯 ...