我有一个非常大的方法,我想使异步访问。这些方法很复杂,有时很长。我能想到的方法是复制所有现有的方法并使其异步。但是当我必须做一些改变时,我必须编辑2种方法。代码在一个地方有更好的方法吗?使1方法异步以及在C#中同步
正如你所看到的代码基本相同。 是否可以将这两种方法合并为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", "");
}
如果您希望我们为您提供帮助,您需要发布一个小而重复的问题。在没有任何实际例子的情况下提出冗长的问题并没有帮助。 – 2014-11-21 08:17:32
也许你可以从异步包装器中调用你的方法而不用改变这个方法 – Fedor 2014-11-21 08:21:08
正确的答案取决于这些方法实际上在做什么。在某些情况下,你可能想用一些'Task'来包装一个同步方法。在其他情况下,您可能想要封装异步方法,直到完成为止。即使您的方法目前都是同步的,但并不排除它们可能更好地实现为异步。当然,每种方法的具体细节因每种方法所做工作的确切性质而异。 – 2014-11-21 08:29:21