gpt4 book ai didi

BLToolkit - 是否能够进行异步数据库操作?

转载 作者:行者123 更新时间:2023-12-02 22:55:31 25 4
gpt4 key购买 nike

我想避免由于缓慢的数据库读取而阻塞执行线程,并且我非常喜欢BLToolkit DataAccessor

public abstract class PersonAccessor : DataAccessor
{
[SqlText(@"SELECT * FROM Person WHERE FirstName = @firstName")]
public abstract List<Person> GetPersonListByFirstName(string @firstName);

[SprocName("sp_GetPersonListByLastName")]
public abstract List<Person> GetPersonListByLastName(string @lastName);
}

BLToolkit DataAccessor 是否可以使用异步操作?

希望它能够返回Task<T>我可以使用await从 C# 5.0 开始

兄弟。

最佳答案

您可以组合Async属性提供开始结束模式和 turn it into Task像这样(未测试):

public abstract class PersonAccessor : DataAccessor
{
[SqlText(@"SELECT * FROM Person WHERE FirstName = @firstName")]
public abstract List<Person> GetPersonListByFirstName(string @firstName);

[Async]
public abstract IAsyncResult BeginGetPersonListByFirstName(string @firstName, AsyncCallback callback, object state);

[Async]
public abstract List<Person> EndGetPersonListByFirstName(IAsyncResult asyncResult);

public Task<List<Person>> GetPersonListByFirstNameAsync(string @firstName)
{
return Task.Factory.FromAsync(
BeginGetPersonListByFirstName(),
EndGetPersonListByFirstName,
@firstName,
null);
}
}

public class TestClass
{
public List<Person> AwaitTest(PersonAccessor personAccessor, string @firstName)
{
return await personAccessor.GetPersonListByFirstNameAsync(@firstName);
}
}

关于BLToolkit - 是否能够进行异步数据库操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18272900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com