代码之家  ›  专栏  ›  技术社区  ›  Andrus

如何用异步方法实现IStringLocalizer索引器

  •  0
  • Andrus  · 技术社区  · 4 年前

    IStringLocalizer 接口。

    修饰符“async”对此项无效

    public async LocalizedString this[string name] => new LocalizedString(name, await I(name));
    

    如何实施 IString定位器 使用EF核心异步数据访问的接口?

    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Localization;
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    /// <summary>
    /// Translation service.
    /// Retrieves localized strings from database
    /// </summary>
    public sealed class Localizer : IStringLocalizer
    {
        readonly EevaContext ctx;
    
        public Localizer(EevaContext ctx)
        {
            this.ctx = ctx;
        }
    
        async Task<string> I(string str)
            {   
                return await ctx.Strings
                                .Where((s) => s.Est == estString.)
                                .SingleOrDefaultAsync(); 
            }
    
        #region interface implementation
        public async LocalizedString this[string name] => new LocalizedString(name, await I(name));
        public LocalizedString this[string name, params object[] arguments] => throw new NotImplementedException();
    }
    

    回答 async Indexer in C#

    0 回复  |  直到 4 年前