聪明的C#和ASP.NET Core小功能让我感到高兴

Visual Studio

I recently needed to refactor my podcast site which is written in ASP.NET Core 2.2 and running in Azure. The Simplecast backed API changed in a few major ways from their v1 to a new redesigned v2, so there was a big backend change and that was a chance to tighten up the whole site.

最近,我需要重构用ASP.NET Core 2.2编写并在Azure中运行的播客站点。 从Simplecast支持的API在几个主要方面进行了更改,从他们的v1更改为新的重新设计的v2,因此后端发生了很大的变化,这是一个使整个站点更加紧凑的机会。

As I was refactoring I made a few small notes of things that I liked about the site. A few were C# features that I'd forgotten about! C# is on version 8 but there were little happinesses in 6.0 and 7.0 that I hadn't incorporated into my own idiomatic view of the language.

在重构时,我做了一些关于我喜欢该站点的事情的小笔记。 我忘了一些C#功能! C#在版本8上,但是在6.0和7.0中几乎没有我没有融入到我自己的惯用语言中的快乐。

This post is collecting a few things for myself, and you, if you like.

如果您愿意,这篇文章将为我自己和您收集一些东西。

I've got a mapping between two collections of objects. There's a list of all Sponsors, ever. Then there's a mapping of shows where a show might have n sponsors.

我有两个对象集合之间的映射。 有所有赞助商的清单。 然后是节目的映射,其中一个节目可能有n个赞助商。

Out Var (Out Var )

I have to "TryGetValue" because I can't be sure if there's a value for a show's ID. I wish there was a more compact way to do this (a language shortcut for TryGetValue, but that's another post).

我必须“ TryGetValue”,因为我不能确定节目ID是否有值。 我希望有一个更紧凑的方法来实现(TryGetValue的语言快捷方式,但这是另一篇文章)。

Shows2Sponsor map = null;
shows2Sponsors.TryGetValue(showId, out map); if (map != null) { var retVal = sponsors.Where(o => map.Sponsors.Contains(o.Id)).ToList(); return retVal; } return null;

I forgot that in C# 7.0 they added "out var" parameters, so I don't need to declare the map or its type. Tighten it up a little and I've got this. The LINQ query there returns a List of sponsor details from the main list, using the IDs returned from the TryGetValue.

我忘记了在C#7.0中他们添加了“ out var”参数,因此我不需要声明映射或其类型。 稍微拧紧就可以了。 那里的LINQ查询使用从TryGetValue返回的ID从主列表返回赞助者详细信息列表。

if (shows2Sponsors.TryGetValue(showId, out var map))
    return sponsors.Where(o => map.Sponsors.Contains(o.Id)).ToList();
return null;

类型别名 (Type aliases)

I found myself building JSON types in C# that were using the "Newtonsoft.Json.JsonPropertyAttribute" but the name is too long. So I can do this:

我发现自己在C#中使用“ Newtonsoft.Json.JsonPropertyAttribute”构建JSON类型,但名称太长。 所以我可以这样做:

using J = Newtonsoft.Json.JsonPropertyAttribute;

Which means I can do this:

这意味着我可以这样做:

[J("description")] 
public string Description { get; set; }

[J("long_description")] public string LongDescription { get; set; }

惰性缓存 (LazyCache)

I blogged about LazyCache before, and its challenges but I'm loving it. Here I have a GetShows() method that returns a List of Shows. It checks a cache first, and if it's empty, then it will call the Func that returns a List of Shows, and that Func is the thing that does the work of populating the cache. The cache lasts for about 8 hours. Works great.

之前写过关于LazyCache的博客,以及它的挑战,但我很喜欢它。 在这里,我有一个GetShows()方法,它返回一个节目列表。 它首先检查一个缓存,如果它是空的,那么它将调用Func返回一个显示列表,而Func是负责填充缓存的东西。 缓存持续约8个小时。 效果很好。

public async Task<List<Show>> GetShows()
{
Func<Task<List<Show>>> showObjectFactory = () => PopulateShowsCache();
return await _cache.GetOrAddAsync("shows", showObjectFactory, DateTimeOffset.Now.AddHours(8));
}
private async Task<List<Show>> PopulateShowsCache()
{
List<Show> shows = shows = await _simpleCastClient.GetShows();
_logger.LogInformation($"Loaded {shows.Count} shows");
return shows.Where(c => c.Published == true && c.PublishedAt < DateTime.UtcNow).ToList();
}

What are some little things you're enjoying?

您喜欢什么小东西?

Sponsor: Manage GitHub Pull Requests right from the IDE with the latest JetBrains Rider. An integrated performance profiler on Windows comes to the rescue as well.

赞助商:使用最新的JetBrains Rider从IDE直接管理GitHub Pull Requests。 Windows上的集成性能分析器也可以解决。

翻译自: https://www.hanselman.com/blog/clever-little-c-and-aspnet-core-features-that-make-me-happy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值