.net 洋葱模型

57 篇文章 1 订阅

洋葱架构

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

简单的例子,引用依赖图
在这里插入图片描述

demo

  1. 接口类库
    EmailInfo
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

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

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 这是实现业务的地方,注入两个接口,一个是获取数据,一个是发送信息,实现均在控制台工程实现

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
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

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

//依赖注入
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!");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值