[英]Make 1 method async as well as sync in C#
我有很多方法想使异步访问。 这些方法很复杂,有时很长。 我可以考虑的方法是复制所有现有方法并使它们异步。 但是,当我必须进行一些更改时,我必须编辑2种方法。 将代码放在一个地方是否有更好的方法?
如您所见,代码基本相同。 可以将这两种方法合并为1吗?
public async Task ManufacturersToWebshopAsync(HttpContext httpContext, ManufacturerNopServiceClient manufacturerNopServiceClient, bool onlyChanged = false, bool includeNight = false)
{
Log.Verbose("ManufacturersToWebshop", "Start", "");
// client
if (manufacturerNopServiceClient == null)
{
var host = httpContext.Request.Url.Host;
manufacturerNopServiceClient = GetManufacturerNopServiceClient(host);
}
var manufacturers = _manufacturerService.GetAllManufacturers();
if (onlyChanged && includeNight)
{
manufacturers = manufacturers.Where(x => x.State == State.Changed || x.State == State.Night).ToList();
}
else
{
if (onlyChanged)
{
manufacturers = manufacturers.Where(x => x.State == State.Changed).ToList();
}
if (includeNight)
{
manufacturers = manufacturers.Where(x => x.State == State.Night).ToList();
}
}
var tasks = new List<Task>();
var total = manufacturers.Count();
var count = 1;
foreach (var manufacturer in manufacturers)
{
Log.Information("ManufacturersToWebshop", "Manufacturer " + count + " van de " + total, "");
//tasks.Add(ManufacturerToWebshop(httpContext, manufacturer, manufacturerNopServiceClient));
await ManufacturerToWebshopAsync(httpContext, manufacturer, manufacturerNopServiceClient);
count++;
}
//await Task.WhenAll(tasks);
Log.Verbose("ManufacturersToWebshop", "End", "");
}
public void ManufacturersToWebshop(HttpContext httpContext, ManufacturerNopServiceClient manufacturerNopServiceClient, bool onlyChanged = false, bool includeNight = false)
{
Log.Verbose("ManufacturersToWebshop", "Start", "");
// client
if (manufacturerNopServiceClient == null)
{
var host = httpContext.Request.Url.Host;
manufacturerNopServiceClient = GetManufacturerNopServiceClient(host);
}
var manufacturers = _manufacturerService.GetAllManufacturers();
if (onlyChanged && includeNight)
{
manufacturers = manufacturers.Where(x => x.State == State.Changed || x.State == State.Night).ToList();
}
else
{
if (onlyChanged)
{
manufacturers = manufacturers.Where(x => x.State == State.Changed).ToList();
}
if (includeNight)
{
manufacturers = manufacturers.Where(x => x.State == State.Night).ToList();
}
}
var total = manufacturers.Count();
var count = 1;
foreach (var manufacturer in manufacturers)
{
Log.Information("ManufacturersToWebshop", "Manufacturer " + count + " van de " + total, "");
ManufacturerToWebshop(httpContext, manufacturer, manufacturerNopServiceClient);
count++;
}
Log.Verbose("ManufacturersToWebshop", "End", "");
}
可以将这两种方法合并为1吗?
并非以一种适用于所有情况的好方法。 有黑客编写异步方法同步包装 ,以及其他黑客编写同步方法异步包装 -但没有的选项在所有情况下工作。 没有通用的通用解决方案可以解决此问题。
我建议您考虑您的方法在做什么,并确定它是否应该异步。 例如,如果它正在执行I / O,则它应该是异步的。 然后,如果该方法应该异步,则只需使其异步(没有同步版本)即可。
如果要更新代码,则将需要更新所有调用新异步方法的代码(以及调用这些方法的代码,等等)。 如果此更新花费的时间太长而无法在整个系统中应用,那么建议使用(临时)重复代码。 但是,如果您不控制调用代码,则可能必须考虑链接文章中的一些技巧。 只要确保仔细考虑每一个的分支,然后选择适合您特定代码的分支即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.