Loading

造轮子之属性注入配合懒加载构建服务抽象基类

在前面实现管理API的时候,可以看到我们用的挺多功能是没有通过构造函数注入的。比如缓存DistributedCache,MemoryCache,对象映射Mapper,多语言L,当前用户CurrentUser等等。
这些全都初始化在WheelServiceBase以及WheelControllerBase中,可以通过属性注入完成这个操作,同时为了避免注入太多影响性能,可以配合懒加载实现除IServiceProvider以外的服务注入。

这样做的好处是可以很方便把我们常用的一些工具型服务打包到基类调用,不需要每个业务服务都需要重复注入,同时减少了我们业务服务构造器因为注入越来越臃肿的情况。

上代码:

namespace Wheel.Services
{
    public abstract class WheelServiceBase
    {
        public IServiceProvider ServiceProvider { get; set; }
        public SnowflakeIdGenerator SnowflakeIdGenerator => LazyGetService<SnowflakeIdGenerator>();
        public GuidGenerator GuidGenerator => LazyGetService<GuidGenerator>();
        public IHttpContextAccessor HttpContextAccessor => LazyGetService<IHttpContextAccessor>();

        public IUnitOfWork UnitOfWork => LazyGetService<IUnitOfWork>();

        public IMapper Mapper =>  LazyGetService<IMapper>();
        public IMemoryCache MemoryCache => LazyGetService<IMemoryCache>();

        public IDistributedCache DistributedCache => LazyGetService<IDistributedCache>();
        public ILocalEventBus LocalEventBus => LazyGetService<ILocalEventBus>();
        public IDistributedEventBus DistributedEventBus => LazyGetService<IDistributedEventBus>();
        public ICurrentUser CurrentUser => LazyGetService<ICurrentUser>();
        public IStringLocalizerFactory LocalizerFactory => LazyGetService<IStringLocalizerFactory>();

        private IStringLocalizer _stringLocalizer = null;

        public IStringLocalizer L { 
            get
            {
                if (_stringLocalizer == null)
                    _stringLocalizer = LocalizerFactory.Create(null);
                return _stringLocalizer;
            }
        }

        public T LazyGetService<T>() where T : notnull
        {
            return new Lazy<T>(ServiceProvider.GetRequiredService<T>).Value;
        }
    }
}


namespace Wheel.Controllers
{
    [Authorize("Permission")]
    public abstract class WheelControllerBase : ControllerBase
    {
        public IServiceProvider ServiceProvider { get; set; }
        public SnowflakeIdGenerator SnowflakeIdGenerator => LazyGetService<SnowflakeIdGenerator>();
        public GuidGenerator GuidGenerator => LazyGetService<GuidGenerator>();
        public IUnitOfWork UnitOfWork => LazyGetService<IUnitOfWork>();
        public IMapper Mapper => LazyGetService<IMapper>();
        public IMemoryCache MemoryCache => LazyGetService<IMemoryCache>();

        public IDistributedCache DistributedCache => LazyGetService<IDistributedCache>();
        public ILocalEventBus LocalEventBus => LazyGetService<ILocalEventBus>();
        public IDistributedEventBus DistributedEventBus => LazyGetService<IDistributedEventBus>();
        public ICurrentUser CurrentUser => LazyGetService<ICurrentUser>();
        public IStringLocalizerFactory LocalizerFactory => LazyGetService<IStringLocalizerFactory>();


        private IStringLocalizer _stringLocalizer = null;

        public IStringLocalizer L
        {
            get
            {
                if (_stringLocalizer == null)
                    _stringLocalizer = LocalizerFactory.Create(null);
                return _stringLocalizer;
            }
        }

        public T LazyGetService<T>() where T : notnull
        {
            return new Lazy<T>(ServiceProvider.GetRequiredService<T>).Value;
        }
    }
}

可以看到,只有public IServiceProvider ServiceProvider { get; set; }是通过属性注入的,别的服务都是通过LazyGetService方法获得实例。
LazyGetService则是通过懒加载的方法,调用ServiceProvider.GetRequiredService去获取服务。只有在使用到对应服务时,才会从依赖注入获取对应的服务。

注意,原生依赖注入是不支持使用属性注入功能的,需要第三方依赖注入组件支持,我们使用autofac的时候,若需要属性注入功能,则在注册注入时需要调用PropertiesAutowired()。

轮子仓库地址https://github.com/Wheel-Framework/Wheel
欢迎进群催更。

image.png

posted @ 2023-10-12 12:15  饭勺oO  阅读(187)  评论(0编辑  收藏  举报