• 接口中定义异步的方法


    我定义了如下接口引出错误:

     public interface INewsCategoryRepository    {
            void CreateNewsCategory(NewsCategory newsCategory);
        }

    然后用如下类实现了上述接口

    public async void CreateNewsCategory(NewsCategory newsCategory)
    {
    var db = dbContext as NewsEFCoreContext; await db.Categories.AddAsync(newsCategory); }

    于是问题出现了:

    public class AddNewsCategoryUseCase
        {
            private readonly IRepository repository;
            private readonly INewsCategoryRepository newsCategoryRepository;
            public AddNewsCategoryUseCase(IRepository argRepository, INewsCategoryRepository argNewsCategoryRepository)
            {
                this.repository = argRepository;
                this.newsCategoryRepository = argNewsCategoryRepository;
            }
            /// <summary>
            /// 添加新的新闻
            /// </summary>
            /// <param name="addNewsCategoryDto"></param>
            public  async void AddNewsCategory(AddNewsCategoryDto addNewsCategoryDto)
            {
                NewsCategory newsCategory = addNewsCategoryDto.ToNewsCategory();
                try
                {
                    using (repository)
                    {
                        //newsCategoryRepository 是接口成员 其返回类型是void 没办法使用await--------------------------!!!
                        await newsCategoryRepository.CreateNewsCategory(newsCategory);
                        repository.Commit();
                    }
                }
                catch (Exception ex)
                {
    
                    throw ex;
                }
                
    
            }
        }

    截图如下:

    正确的办法应该是:不能将接口方法定义为void 而应该定义为Task。(没有返回值就用Task,有返回值就用Task<T>)

    public interface INewsCategoryRepository
        {
            Task CreateNewsCategory(NewsCategory newsCategory);
        }

    实现类如下:

    public async Task CreateNewsCategory(NewsCategory newsCategory)
            {
                var db = dbContext as NewsEFCoreContext;
                await db.Categories.AddAsync(newsCategory);
            }

    调用这个方法如下:

     public class AddNewsCategoryUseCase
        {
            private readonly IRepository repository;
            private readonly INewsCategoryRepository newsCategoryRepository;
            public AddNewsCategoryUseCase(IRepository argRepository, INewsCategoryRepository argNewsCategoryRepository)
            {
                this.repository = argRepository;
                this.newsCategoryRepository = argNewsCategoryRepository;
            }
            /// <summary>
            /// 添加新的新闻
            /// </summary>
            /// <param name="addNewsCategoryDto"></param>
            public  async Task AddNewsCategory(AddNewsCategoryDto addNewsCategoryDto)
            {
                NewsCategory newsCategory = addNewsCategoryDto.ToNewsCategory();
                try
                {
                    using (repository)
                    {
                        //没问题------------------!!!
                        await newsCategoryRepository.CreateNewsCategory(newsCategory);
                        repository.Commit();
                    }
                }
                catch (Exception ex)
                {
    
                    throw ex;
                }
                
    
            }
        }

    截图:

     参考文章:异步编程中的最佳做法 https://msdn.microsoft.com/zh-cn/magazine/jj991977.aspx?f=255&MSPPError=-2147217396

  • 相关阅读:
    shi_tomasi特征点,GFTTDetector
    特征点总结(按features2d.hpp源码由上至下总结)
    经过一年时间的沉淀 再次回首 TCP Socket服务器编程 (二)
    构建嵌入式小型Linux系统
    老赵书托(3):深入理解计算机系统
    #define与typedef区别
    Linux下快速静态编译Qt以及Qt动态/静态版本共存
    tcpdump示例
    linux绑定多个ip(转载)
    Linux下eclipse及mysql安装,c++访问mysql数据库
  • 原文地址:https://www.cnblogs.com/wholeworld/p/9458817.html
Copyright © 2020-2023  润新知