Blazor, BusinessBase and async #2623
-
I'm using blazor and async calls to load BizObjects. Should I be declaring fetch as async. The fetch in the book and project tracker isn't declared as async. private async Task Fetch(SqlCommand cmd)
{
using (var dr = new SafeDataReader(await cmd.ExecuteReaderAsync()))
{
if (dr.Read())
{
Fetch(dr);
}
}
} Thanks, Mike |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
CSLA supports both [Fetch]
private void Fetch() and [Fetch]
private async Task FetchAsync() You can use either, and the data portal will work as expected. So the real question is whether, inside your method, you need to call |
Beta Was this translation helpful? Give feedback.
-
This doesn't directly answer the question you asked, but since I tripped over the information, I thought I'd add a bit more detail on this discussion, for completeness. Blazor WebAssembly, accessing a data portal server using the HttpProxy, can only use async on the client. This isn't a limitation of Csla, but a limitation imposed on us by the runtime. HttpClient only supports async, and WebClient, which Csla uses under the covers to do synchronous HTTP calls, isn't implemented in the runtime used to host Blazor WebAssembly. For this reason, I'd recommend making use of data portal's FetchAsync method in your Blazor UI code. That means it will work with either Blazor WebAssembly or Blazor Server, and you can change between them without issue (or use different hosting models in different sites using the same objects.) This also reduces the risk of sync over async being introduced into your system without you being aware. Csla is capable of accepting a sync request via DataPortal.Fetch and running an async data access method, to help you out. However, if you are using LocalProxy - client and server methods running in the same process - then it has to do so by using sync over async. In some scenarios, sync over async will cause your system to deadlock. This is something you should avoid. But wait, there's more. Things are made more exciting by Csla's FieldManager class, and how it interacts with child objects. FieldManager currently only exposes a sync UpdateChildren method, so if you write async code in any child objects, you almost always suffer sync over async. So there is a lot to answering how best to do your data access. My general recommendations are as follows:
The world is going async. There are already some data storage providers that only offer async. When they go async only, they force everyone else to do the same. Therefore, what I do is design for async from the outset. Here are my rules:
Once we've found time to address issue #2393 then the last rule will change so that all data access methods in child objects are async in that scenario. |
Beta Was this translation helpful? Give feedback.
-
For completeness, FieldManager.UpdateChildrenAsync was added to CSLA 6.0. That means I need to update my recommendations listed above. My general recommendations are now as follows: If you are going to need to use async/await in your data access method - either for better scalability or because it might call something that only offers async access to your data store - then you should also use async for the call into DataPortal on the client. The world is going async. There are already some data storage providers that only offer async. When they go async only, they force everyone else to do the same. Therefore, what I do is design for async from the outset. Here are my rules:
Remember that the client calls I am referring to include saving the object. That means I prefer to use SaveAsync or SaveAndMergeAsync over Save or SaveAndMerge to fulfil the recommendation of "async all the way." |
Beta Was this translation helpful? Give feedback.
CSLA supports both
and
You can use either, and the data portal will work as expected. So the real question is whether, inside your method, you need to call
await
or not.