懒惰的程序员经常用别人开发的组件,也经常到处寻觅好用的组件,实在找不到才自己去写一个,有没有想过把自己写过的好用的功能可以分享给别人使用呢?
我们动手尝试一下看看, 先学习别人的,然后再做一个简单的练习范例
以这位大哥的Logdashboard组件使用为例: https://doc.logdashboard.net/logdashboard/abp-vnext-shi-yong-logdashboard
常见的组件引入及使用方法有以下几个步骤
1. Nuget package安装
2. 添加引用并注入, 以及参数配置
context.Services.AddLogDashboard(opt => opt.SetRootPath(hostingEnvironment.ContentRootPath));
3. 有UI的再补一句启用其自带的UI
app.UseLogDashboard();
4. 无需UI的例如Serilog等组件只要注入到需要的地方即可,例如
public TodosController(TodoContext context, ILogger<TodosController> logger)
{
//将注入的context和logger保存到当前上下文变量
//就可以用了
Log.Information("Starting web request...");
}
这样功能就可以用了
这中间涉及几个技能点:
1.Nuget打包
2.静态方法扩展
3.依赖注入
4.配置文件注入与读取
Nuget打包,此处就不废话,按官网介绍操作,参考如下指南也可,打包好才能像npm或maven那样被方便安装
https://docs.microsoft.com/zh-cn/nuget/create-packages/creating-a-package
https://www.cnblogs.com/h82258652/p/4898983.html
静态方法扩展, 可以对已有的别人的组件扩展自己的方法, 典型范例Linq的查询扩展, 让代码可读性更好,感觉更面向对象,浑然一体
上文范例中扩展了string,增加了字数统计方法
namespace ExtensionMethods{
public static class MyExtensions {
publicstaticintWordCount(thisString str) {
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}
这样在使用时,可以对字符串统计字数, 好用又好理解吧, 以后你就理解为何别人的string有你所不知道的方法了.
不好的地方是你经常找不到哪里实现的,因为是静态扩展的
string s = "Hello Extension Methods";
int i = s.WordCount();
这样也可以
int i = MyExtensions.WordCount(s);
依赖注入, 用习惯后会觉得没有依赖注入.net就没有灵魂了, 把需要的服务往工具箱一丢,谁想用就去取就好了,至于其如何初始化,是谁提供的服务等细节不用关注太多,也正是这样,我们引入组件才很方便,在需要的地方注入就可以
https://docs.microsoft.com/zh-cn/dotnet/core/extensions/dependency-injection
下面的service从工具箱中取用了2个工具ILooger, IOptions.
public class ExampleService{
public ExampleService() { }
public ExampleService( ILogger<ExampleService> logger, IOptions<ExampleService> options)
{ // omitted for brevity
}
}
配置文件读取与注入
https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/options?view=aspnetcore-6.0
appsettings.json中的配置项
"Position": {
"Title": "Editor",
"Name": "Joe Smith"
}
定义加载配置项的class
public class PositionOptions{
public const string Position = "Position";
public string Title { get; set; }
public string Name { get; set; }
}
注入并使用Options
public class Test22Model : PageModel{
private readonly IConfiguration Configuration;
publicTest22Model(IConfiguration configuration) {
Configuration = configuration;
}
publicContentResultOnGet() {
var positionOptions = new PositionOptions();
Configuration.GetSection(PositionOptions.Position).Bind(positionOptions);
return Content($"Title:{positionOptions.Title}\n" +
$"Name:{positionOptions.Name}");
}
}
有了以上基本技能, 我们来扩展一个email发送的组件示范一下
此处忽略Nuget打包过程
定义一个Email Send 接口并实现它, 作为公共组件发布之用
using System.Threading.Tasks;
namespace My.Extensions
{
public interface IEmailSender
{
public Task SendEmailAsync(string email, string subject, string message);
}
}
功能实现
using MailKit.Net.Smtp;
using Microsoft.Extensions.Options;
using MimeKit;
using MimeKit.Text;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace My.Extensions
{
public class MailKitEmailSender : IEmailSender
{
public MailKitEmailSender(IOptions<MailKitOptions> options)
{
this.Options = options.Value;
}
private static SmtpClient smtp = new SmtpClient();
private static readonly Object lockObject = new Object();
private readonly MailKitOptions Options;
public Task SendEmailAsync(string email, string subject, string message)
{
return Execute(email, subject, message);
}
private Task Execute(string to, string subject, string message)
{
var email = new MimeMessage
{
Sender = MailboxAddress.Parse(Options.DefaultFromAddress)
};
if (!string.IsNullOrEmpty(Options.DefaultFromDisplayName))
email.Sender.Name = Options.DefaultFromDisplayName;
email.From.Add(email.Sender);
email.To.Add(MailboxAddress.Parse(to));
email.Subject = subject;
email.Body = new TextPart(TextFormat.Html) { Text = message };
// send email
using (var smtp = new SmtpClient())
{
lock (lockObject)
{
var username = string.IsNullOrEmpty(Options.Domain) ? Options.Username : Options.Domain + "\\" + Options.Username;
smtp.Connect(Options.Host, Options.Port, Options.SecureSocketOptions);
smtp.Authenticate($"{username}", Options.Password);
smtp.Send(email);
smtp.Disconnect(true);
}
}
return Task.FromResult(true);
}
}
}
定义一个Class来装载配置项
using MailKit.Security;
namespace My.Extensions
{
public class MailKitOptions
{
public MailKitOptions()
{
SecureSocketOptions = SecureSocketOptions.Auto;
}
public string Host { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Domain { get; set; }
public string Password { get; set; }
public SecureSocketOptions SecureSocketOptions { get; set; }
public string DefaultFromAddress { get; set; }
public string DefaultFromDisplayName { get; set; }
public bool AuthenticationEabled { get; set; } = true;
}
}
在Appsettings.json中加入配置项
"MailKit": {
"Mail.Smtp.Host": "smtp.163.com",
"Mail.Smtp.Port": "587",
"Mail.Smtp.UserName": "your id",
"Mail.Smtp.Password": "your psw",
"Mail.Smtp.Domain": "如果企业内部可以用domain",
"Mail.Smtp.EnableSsl": false,
"Mail.Smtp.UseDefaultCredentials": false,
"Mail.Smtp.AuthenticationEabled": true,
"Mail.DefaultFromAddress": "xxx-sendor@163.com",
"Mail.DefaultFromDisplayName": "你的email地址显示名称"
}
对IServiceCollection 扩展, 注入Options,以及mail组件, 这样就可以在需要的地方随意使用了.
using System;
using MailKit.Security;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace My.Extensions
{
public static class EmailExtension
{
public static void AddEmailExtension(this IServiceCollection services,Action<MailKitOptions> options)
{
services.Configure<MailKitOptions>(options);
services.AddSingleton<IEmailSender, MailKitEmailSender>();
}
}
}
注入并使用之
public class AccountController : Controller
{
private IEmailSender EmailSender { get; }
public AccountController(
IEmailSender emailSender)
{
EmailSender = emailSender;
}
public void Notify()
{
this.MailSender.SendEmailAsync("send to address@XXX.com", "subject ", "mail msg .....");
}
}
结束, 记录之分享给需要的人.