.net 洋葱模型

洋葱架构

  1. 内层部分比外层更抽象(内层接口,外层实现)。
  2. 外层的代码只能调用内层的代码,内层的代码可以通过依赖注入的形式来间接调用外层的代码

简单的例子,引用依赖图

demo

  1. 接口类库
    EmailInfo
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Intf
{
    public class EmailInfo
    {
        public string Email { get; set; }

        public string Subject { get; set; }

        public string Body { get; set; }

        public EmailInfo(string email,string subject,string body)
        {
            Email = email;  
            Subject = subject;
            Body = body;
        }
    }
}

IGenerateMessage

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Intf
{
    public interface IGenerateMessage
    {
        public IEnumerable<EmailInfo> GenerateEmail();
    }
}

ISendMessage

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Intf
{
    public interface ISendMessage
    {
        public Task SendMessageAsync(EmailInfo emailInfo);
    }
}

Mybusiness 这是实现业务的地方,注入两个接口,一个是获取数据,一个是发送信息,实现均在控制台工程实现

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Intf
{
    public class Mybusiness
    {
        public IGenerateMessage GenerateMessage { get; set; }

        public ISendMessage SendMessage { get; set; }
        public Mybusiness(IGenerateMessage generateMessage, ISendMessage sendMessage)
        {
            GenerateMessage = generateMessage;
            SendMessage = sendMessage;
        }
        public async Task DoIt()
        {
            var items = GenerateMessage.GenerateEmail();

            foreach (var item in items)
            {
                await SendMessage.SendMessageAsync(item);
                await Task.Delay(1000);
            }
        }
    }
}
  1. 控制台工程
    GenerateMessage
csharp 复制代码
using Intf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Onion
{
    internal class GenerateMessage : IGenerateMessage
    {
        public IEnumerable<EmailInfo> GenerateEmail()
        {
            for (int i = 0; i < 5; i++)
            {
                yield return new EmailInfo("1.com", "你好", "你好");
            }
        }
    }
}

MyEmailSender

csharp 复制代码
using Intf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Onion
{
    internal class MyEmailSender : ISendMessage
    {
        public Task SendMessageAsync(EmailInfo emailInfo)
        {
            Console.WriteLine($"{emailInfo.Email} {emailInfo.Subject} {emailInfo.Body}");
            return Task.CompletedTask;
        }
    }
}

program

csharp 复制代码
//依赖注入
using Intf; //类库名称
using Microsoft.Extensions.DependencyInjection;//
using Onion;//控制台工程名称

ServiceCollection service =  new ServiceCollection();

service.AddScoped<IGenerateMessage, GenerateMessage>();

service.AddScoped<ISendMessage, MyEmailSender>();

service.AddScoped<Mybusiness>();
var sp =  service.BuildServiceProvider();

Mybusiness business = sp.GetRequiredService<Mybusiness>();

business.DoIt();


Console.WriteLine("Hello, World!");
相关推荐
WineMonk16 小时前
.NET WPF CommunityToolkit.Mvvm框架
.net·wpf·mvvm
界面开发小八哥16 小时前
界面控件DevExpress WPF中文教程:Data Grid——卡片视图设置
.net·wpf·界面控件·devexpress·ui开发
九鼎科技-Leo1 天前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo1 天前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发1 天前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
九鼎科技-Leo1 天前
在 C# 中,ICollection 和 IList 接口有什么区别?
windows·c#·.net
时光追逐者1 天前
C#/.NET/.NET Core学习路线集合,学习不迷路!
开发语言·学习·c#·asp.net·.net·.netcore·微软技术
Crazy Struggle2 天前
.NET 全功能流媒体管理控制接口平台
.net·开源项目·流媒体