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 ...
随机推荐
- Docker for Windows(一)下载与安装
一.下载Docker for Windows 下载地址:Docker for Windows 下载完是一个安装程序,双击运行即可.注:如果您的系统不符合运行Docker for Windows的要求, ...
- 当div元素内的内容超出其宽度时,自动隐藏超出的内容
word-break:keep-all;/* 不换行 */ white-space:nowrap;/* 不换行 */ overflow:hidden;/* 内容超出宽度时隐藏超出部分的内容 */ te ...
- 自定义圆角ImageView控件
这个就当工具类用吧,因为直接是继承的ImageView.所以也具备了ImageView所有的特点,不同的是,可以自动裁剪成圆角图片.看效果吧. 效果还是不错的.使用方式: 直接在配置中添加依赖 com ...
- 破解 jeb 2.3.7 demo
前言 使用的技术和上文的一样. mips 版本的修改版 修改版: https://gitee.com/hac425/jeb-mips 正文 安卓版 jeb-2.3.7.201710262129-JEB ...
- 树莓派发射FM波——搭建私人小电台
树莓派的应用十分广泛,有很多奇思妙想的应用非常有趣,在这里我们想实现一个小电台的功能,但是在这里需要说明,私人架设电台是违法行为,所以本案只作为自我娱乐所用,不能发射大功率的信号干扰正常的FM频段. ...
- maven 依赖和坐标
1.maven 坐标由groupId.artifactId.packaging.version.classifier定义.2.classifier 用来帮助定义构建输出的一些附属构件.如,*javad ...
- oracle 中的角色
conn sys as sysdba;create role testrole;grant create session, create table, unlimited tablespace to ...
- node和iisnode express手工安装
一.安装node.js的x86版本: 这样,node.js会安装在C:\Program Files (x86)\nodejs,这符合iisnode express7版本的期待. 二.安装iisnode ...
- Fatal error: Can't use function return value in write context
这个的出错原因很简单,先贴出错代码: <?php $contact = array("id"=>1, "姓名"=>"老高" ...
- Ssh 证书验证 续篇
今天下午正好有外面的人要登录服务器,想了想,普通用户密码就是不想给,然后我就这样做了. useradd alex ---创建账户和密码 passwd alex mkdir /home/alex/.ss ...