Return observable from .net web api - sql-server

Specifically my question is about how to return a true observable from a .netcore web api controller, using efcore. This is so datasets can be returned as a stream so the front end can start building the page at once using the data that it receives in the first iteration and keep on until all data has been received.
So I have seen observable collections. I have also seen that EFCore now streams rather than buffers.
Can anyone point me at the documentation, or an example so I can do more reading?
Let's say I am returning 20 records, from my web api and returning them to a reactjs project. Reactjs supports observables using rxjs. What do I need to do in the web api app to support this observable flow from sql server all the way up to the controller level?

I am not exactly sure what are you asking for , but the normal process is create a service which return a DTO, in your case an array of DTOs or a DTO which contains an array of DTOs.
And then you can send this info trough the controller.

I'm not sure if WebAPI is able to stream IObservable out-of-the-box (most probably not), so there are 2 options:
you can use SinglarR sockets to stream objects to frontend
You can return IAsyncEnumerable from controller. This way JSON serializer will stream the data element by element and frontend needs to start deserializing them before getting whole result
Personally, I'd use approach 2, unless you already use websockets in your project. In the controller it's mostly a matter of using ToAsyncEnumerable (here's a source), but you need to verify, if your frontend libraries support that

Related

cakephp 4 - model without database

In a cakephp 4 project in need to read data from a third party api.
I found this doc for cakephp 2, but no equivalent for cakephp 4.
https://book.cakephp.org/2/en/models/datasources.html
Where is it ?
Thanks
Read data from a third part api direct in your controller using HttpClient or other libs.
https://book.cakephp.org/4/en/core-libraries/httpclient.html
In CakePHP 4 the ORM is structured quite differently, data is retrieved via a Datasource, so you essentially need an HTTP-backed Datasource.
While CakePHP doesn't naively supply an HTTP source, there are a few of plugins that do, such as:
https://github.com/imsamurai/cakephp-httpsource-datasource
https://github.com/CakePHP-Copula/Copula
It looks like these may have some drawbacks and limitations - and in general be fairly advanced setups requiring some creative problem solving. So in the end, as the other poster mentioned, it may in the end be easier to just make HTTP requests directly if you don't require any of the ORM features (validation, Entity classes, virtual fields, Event hooks, etc.)
If the API communicate using POST/GET requests and responds using JSON format (which is usually the case), you can use the Request & Response Objects to ask and get data from the API

Breeze modify data without ajax call; using signalR

I'm working on a project which uses Angular, Breeze and signalR. I want to send data changes using SignalR instead of ajax calls from client side. How can I save changes in the breeze data?
are you planning to send the entire object graph or just individual objects?
In case of individual objects it should be as easy as attaching them to the DBContext.
Object Graph will be another story.. don't even know how that will work with SignalR.

Why does Sinatra need to rewind the request body?

I made a POST request to a Sinatra app. I noticed that the parameters arrive in the server as a StringIO. It can be read using request.body.read. However, it can only be read once. To read it again, I need to run request.body.rewind (haha, Sinatra).
Why is it designed this way? I can see this being useful in streaming data but are there other applications?
Parameters are available within Sinatra via the params hash. request.body.read and request.body.rewind are part of Rack, they are not actually implemented within Sinatra. The most common way I have used this in the past is when I'm using Sinatra strictly as a web API and serializing/de-serializing my payload.

breeze with asp.net web api, reporitories and UoW

I just started using breeze.js, it looks wonderful, but there is something that makes me confused. I am a newbie, so I am sorry if my question is dumb, or does not make sense:)
In server side, I have some repositories and UoW, I am trying to create REST Service. But I want to consume this service from mobile devices like Android, IOS and also from my SPA (Thanks to John Papa for HotTowel).
My confusion is if I arrange my UoW according to Breeze like using EFContextProvider or saving changes by using
public SaveResult SaveChanges(JObject saveBundle)
{
return _contextProvider.SaveChanges(saveBundle);
}
instead of using
public void Commit()
{
//System.Diagnostics.Debug.WriteLine("Committed");
DbContext.SaveChanges();
}
1)Am I still going to be able to use my UoW methods (like savechanges) from my non-breeze controllers?
and
2)Am I still going to be able to use the Rest service from native applications in mobile devices(that does not use breezejs).
1) No you cant use breeze contextProvider.SaveChanges() in your other controllers.
contextProvider.SaveChanges(JObject saveBundle)
breeze server side expects saveBundle to be a json data created by breeze client side js.
2) Create any other webapi controller for REST with its own repository. You can't work with breeze's repository saveChanges without breeze client side js.
UPDATE:
Breeze is raising so fast, they now have a BreezeSharp project. It can be integrated into your .net application. See Jay Traband answer saving changes to breeze web api from c#
#Didar is correct to observe that the JObject saveBundle is specific to the POST body sent by a Breeze client using the Web API out-of-the-box data service adapters.
I want to let you know that a Breeze client saveChanges method call can update a conventional RESTy service with separate PUT/POST/DELETE/PATCH methods if that's what you want to do. You'll have to change the client-side "data service adapter" to one that understands your server API ... or more likely write one that matches the peculiarities of your API.
Writing a custom data service adapter not a newbie task to be sure. We'll be showing how to do it soon ... but it won't be a newbie task ... more an intermediate's task.
My point is that it is there to be done, it isn't hard, and you can take comfort that it will be within your capacity to write by the time you need it.
FWIW, none of the code you're showing actually conforms to repository or UOW patterns IMO. You're showing perfectly serviceable starter code ... code that gets you up and running with a minimum of fuss.
Once you get going, you'd refactor so that references to contextProvider are no longer in your controllers. They'd be wrapped in a repository or unit-of-work component of some sort.
That's a story for another day.

simulating HTTP POST handling using Angular Tutorial web-server script

I'm trying to develop AngularJS applicatino using the Angular tutorial web-server script.
Is it possible or smart to use it for development only scenario ?
I want to be able to develop and test my Angular application without relying on the real server and real database, that's the reason I'm asking this.
I don't know much about the tutorial web-server script.
When it comes to your situation, though, your best bet is to abstract away your data managing processes. In other words, you can make a set of services that take care of loading and saving your data. You could have methods like book.save() or book.fetch().
Then in save() and fetch() you can return or insert an object literal or call for a JSON file.
Assuming that your product will be running on JSON data, you should be able to write another set of model services that call JSON data from the server rather any that you've hard written in the code or in a *.json file.

Resources