前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >造轮子之属性注入配合懒加载构建服务抽象基类

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

作者头像
饭勺oO
发布2023-10-18 20:03:35
1630
发布2023-10-18 20:03:35
举报

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

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

上代码:

代码语言:javascript
复制
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;
        }
    }
}
代码语言:javascript
复制
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()。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-10-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档