Masa的依赖注入系统是基于Microsoft的依赖注入扩展库(Microsoft.Extensions.DependencyInjection nuget包)开发的,因此,它的文档在Masa中也是有效的。
Masa引入了按照约定进行服务注册,依据约定大于配置,开发者无需做任何事,框架会自动完成注册
示例:
``` C#public class StorageOptions : ITransientDependency{ }```忽略注入,用于排除不被自动注入
示例:
``` C#public class BaseService : ISingletonDependency{ public static int Count { get; set; } = 0; public BaseService() { Count++; } public BaseService(bool isChildren) { }}[IgnoreInjection]public class GoodsBaseService : BaseService{ public GoodsBaseService() : base(true) { }}public class GoodsService : GoodsBaseService{ public static int GoodsCount { get; set; } = 0; public GoodsService() { GoodsCount++; }}```效果等同于:services.AddSingleton<
BaseService>(); services.AddSingleton<GoodsService>();
配合ISingletonDependency、IScopedDependency、ITransientDependency使用,实现服务仅被注册一次
示例:
``` C#public interface ICache : ISingletonDependency{ void Set(string key, string value);}[Dependency(TryRegister = true)]public class EmptyCache : ICache{ public void Set(string key, string value) { throw new NotSupportedException($"暂不支持{nameof(Set)}方法"); }}public class MemoryCache : ICache{ private readonly ConcurrentDictionary<string, Lazy<string>> _dicCache = new(); public void Set(string key, string value) { _ = _dicCache.AddOrUpdate ( key, k => new Lazy<string>(() => value, LazyThreadSafetyMode.ExecutionAndPublication), (_, _) => new Lazy<string>(() => value, LazyThreadSafetyMode.ExecutionAndPublication) ).Value; }}```效果等同于:services.AddSingleton<
ICache,MemoryCache>();
示例:
``` C#public interface IEncryptionService : ISingletonDependency{ string MethodName { get; }}[Dependency(ReplaceServices = true)]public class Sha1EncryptionService : IEncryptionService{ public string MethodName => "Sha1";}public class Md5EncryptionService : IEncryptionService{ public string MethodName => "Md5";}```效果等同于:services.AddSingleton<
IEncryptionService,Sha1EncryptionService>();
新建单元测试项目Assignment.DependencyInjection,选择MSTest,并安装Masa.Utils.Extensions.DependencyInjection
dotnet new xunit -o Assignment.DependencyInjectioncd Assignment.DependencyInjectiondotnet add package Masa.Utils.Extensions.DependencyInjection --version 0.5.0-preview.2新建类StorageOptions
public class StorageOptions : ITransientDependency{ }新建类DITest
[TestClass]public class DITest{ private IServiceCollection _services; [TestInitialize] public void Init() { _services = new ServiceCollection(); _services.AddAutoInject();//执行自动注入 } [TestMethod] public void TestAutoInject() { Assert.IsTrue(_services.Any<StorageOptions>(ServiceLifetime.Transient));//判断StorageOptions注册成功,且生命周期为Transient } private IServiceProvider ServiceProvider => _services.BuildServiceProvider();}如果需要使用按照约定自动注册服务,则请记住
ISingletonDependency 单例(项目启动后仅初始化一次)IScopedDependency 请求 (每次请求仅初始化一次)ITransientDependency 瞬时(每次获取都会被初始化)IRepository,且希望接口以及对应的默认实现类RepositoryBase会被自动注册生命周期为Scoped,则接口IRepository应该继承IScopedDependencyRepositoryBase可以被用户定义的类替换,则应该在RepositoryBase上方增加[Dependency(TryRegister = true)],那用户仅需要实现IRepository即可RepositoryBase,用户实现IRepository后,并在新的实现类上方增加[Dependency(ReplaceServices = true)]Assignment07
https://github.com/zhenlei520/MasaFramework.Practice
MASA.BuildingBlocks:https://github.com/masastack/MASA.BuildingBlocks
MASA.Contrib:https://github.com/masastack/MASA.Contrib
MASA.Utils:https://github.com/masastack/MASA.Utils
MASA.EShop:https://github.com/masalabs/MASA.EShop
MASA.Blazor:https://github.com/BlazorComponent/MASA.Blazor
如果你对我们的 MASA Framework 感兴趣,无论是代码贡献、使用、提 Issue,欢迎联系我们

本文来自博客园,作者:磊_磊,转载请注明原文链接:https://www.cnblogs.com/zhenlei520/p/16497621.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利