我想避免由于缓慢的数据库读取而阻塞执行线程,我非常喜欢BLToolkitDataAccessor
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数据访问器是否可以使用异步操作?
希望它能返回Task<T>
,我可以使用C#5.0 中的await
Br。
您可以组合提供Begin-End模式的Async属性,并将其转换为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);
}
}