惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
LINUX DO - 热门话题
Blog — PlanetScale
Blog — PlanetScale
博客园 - Franky
J
Java Code Geeks
腾讯CDC
博客园 - 聂微东
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
Last Week in AI
Last Week in AI
量子位
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Schneier on Security
C
CERT Recently Published Vulnerability Notes
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
有赞技术团队
有赞技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Securelist
AWS News Blog
AWS News Blog
GbyAI
GbyAI
L
LINUX DO - 最新话题
大猫的无限游戏
大猫的无限游戏
Forbes - Security
Forbes - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Attack and Defense Labs
Attack and Defense Labs
C
CXSECURITY Database RSS Feed - CXSecurity.com
Y
Y Combinator Blog
W
WeLiveSecurity
T
Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Proofpoint News Feed
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
V
V2EX
N
News and Events Feed by Topic
Google DeepMind News
Google DeepMind News
D
Docker
The Hacker News
The Hacker News
A
About on SuperTechFans
Security Latest
Security Latest
NISL@THU
NISL@THU
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
博客园_首页
H
Hacker News: Front Page

博客园 - 青玄鸟

.NET 中优雅处理 Server-Sent Events 请求取消 vue3.0 + ts 实现上传工厂(oss与cos) Dapr 订阅者参数无法正确反序列化问题 .NET 代码整洁手册 Blazor项目通过docker和nginx部署为静态站点的步骤 Moq mock 方法返回null空指针异常 HttpClient with Stream HttpClient partial update HttpClient 基本使用 值对象的封装 只读集合类型属性实现 适配器模式 模板模式 最少知识原则 单例模式 抽象工厂 简单工厂、工厂方法、抽象工厂 工厂方法模式 使用 Visual Studio Code创建和执行T-SQL
基于接口隔离原则的依赖注入实现
青玄鸟 · 2020-07-21 · via 博客园 - 青玄鸟

接口隔离原则

不强迫接口的使用者依赖其不需要的接口

接口隔离原则的一般实现

    public interface IFoo
    {
        void DoSomeOperation();
    }

    public interface IBar
    {
        void DoAnotherOperation();
    }

    public class Qux : IFoo, IBar
    {
        public Qux()
        {
            Console.WriteLine("Qux instanced");
        }

        public void DoAnotherOperation()
        {
            Console.WriteLine("I am IBar");
        }

        public void DoSomeOperation()
        {
            Console.WriteLine("Just IFoo");
        }
    }

ASP.NET Core 依赖注入方式注入接口的实现类

为了使IFoo和IBar接口使用同一个Qux实例,可以使用以下方式注入所需服务

        static void Main(string[] args)
        {
            var root = new ServiceCollection()
                .AddScoped<Qux>()
                .AddScoped<IFoo>(provider=>provider.GetService<Qux>())
                .AddScoped<IBar>(provider => provider.GetService<Qux>())
                .BuildServiceProvider();

            using (var scope = root.CreateScope())
            {
                var provider = scope.ServiceProvider;
                provider.GetRequiredService<IFoo>();
                provider.GetRequiredService<IBar>();
            }

            Console.ReadLine();
        }

运行效果如下