ABP 领域服务层学习记录

在ABP框架中有一个约定,所有的领域服务都应该继承并实现IDomainService接口,在领域层Core创建某一个实体的领域服务类,继承并实现IDomainService接口。在ABP框架中,领域服务类的命名习惯一般时xxxManager。
namespace MyTestProject.ShoppingGoods
{
    /// <summary>
    /// 领域服务接口
    /// </summary>
    public interface IGoodsManager:IDomainService
    {
        //书写接口
    }
}
namespace MyTestProject.ShoppingGoods
{
    /// <summary>
    /// 领域服务
    /// </summary>
    public class GoodsManager : DomainService, IGoodsManager
    {
        public GoodsManager()
        {
        }

        //实现接口
    }
}
在AppService中将IGoodsManager 注入进去
namespace MyTestProject.ShoppingGoods
{
    public class GoodssAppService : MyTestProjectAppServiceBase, IApplicationService
    {
        public readonly IGoodsManager _goodsManager;

        public GoodssAppService(IGoodsManager goodsManager)
        {
            _goodsManager = goodsManager;
        }
    }
}

上面的这种方式为,当在某个情况下,领域服务不仅要给appservice提供服务,也要向外部提供服务时,需要与上面的方式书写。当只是给appservice提供服务,使用下面的方式就OK了

namespace MyTestProject.ShoppingGoods
{
    /// <summary>
    /// 领域服务
    /// </summary>
    public class GoodsManager : DomainService
    {
        private readonly IRepository<Goods, Guid> _goodsRepository;

        public GoodsManager(IRepository<Goods, Guid> goodsRepository)
        {
            _goodsRepository = goodsRepository;
        }

        //实现
        public async Task<Guid> CreateAsync(Goods goods)
        {
            return await _goodsRepository.InsertAndGetIdAsync(goods);
        }
    }
}
在AppService中将GoodsManager 注入进去
namespace MyTestProject.ShoppingGoods
{
    public class GoodssAppService : MyTestProjectAppServiceBase, IApplicationService
    {
        //public readonly IGoodsManager _goodsManager;
        public readonly GoodsManager _goodsManager;

        public GoodssAppService(GoodsManager goodsManager)
        {
            _goodsManager = goodsManager;
        }
    }
}

posted @ 2021-08-04 11:05  悟自省  阅读(70)  评论(0编辑  收藏  举报