BreezeJs with standard REST and Json Patch - breeze

we currently have a project where we will use a standard RESTful Interface implemented in WebApi. So there will not be a single Breeze-Controller with a single SaveChanges method to handle all CUD request. Instead, there will be a Controller by type Web Api implementation where the CRUD Actions will be handled by each Controller. Additionally, we think of using JSON Patch to just send the needed data to the backend.
Is there any Adapter already implemented for BreezeJs? There is a abstractrest implementation in breeze labs which sounds like the right base class. But this should be something other already encountered. Is there an example or implementation we could use for our needs?
Thank you for your help!
Marc

You are far from the only person who could benefit from such an adapter. I've been meaning to write about this topic for ... well I think it is years now. It's strange that not enough people have asked to push me over the hump.
You're on the right path with the AbstractRestDataServiceAdapter in Breeze Labs.
I recently wrote a derived adapter to do pretty much what you have in mind ... although I was targeting the Web API (aka MVC6) in ASP.NET 5. Take a look at this "mvc6-unicorn" adapter for inspiration.
It uses POST for modified entities but you could use PATCH or MERGE as in this fragment from the "OData" adapter in b00_breeze.dataService.odata.js.
else if (aspect.entityState.isModified()) {
...
request.method = "MERGE";
request.data = helper.unwrapChangedValues(entity, entityManager.metadataStore, transformValue);
} ...
Please note also that the client-side interpretation of the JSON data within the JsonResultsAdapter is greatly facilitated by a small amount of server-side Json.NET reconfiguration along the lines of what you see in this ApiConfiguration class here (that's the MVC6 equivalent to WebApiConfig).
Sadly, I have to leave you here with only these clues to craft a solution that works for you.
The full story ... which I do hope to write "soon" ... belongs in our Breeze documentation.
Please feel free to return if you get stuck on specific points.

Related

Using Breeze for arbitrary server response

Have all a structure for creating complex queries and obtaining data on the client side using Breeze and webapi IQueryable<T>.
I would use this structure on the client side to call another webapi controller, intercept the result of the query, and use this to make an Excel file returned by HttpResponseMessage.
See: Returning binary file from controller in ASP.NET Web API
How can I use the executeQuery without getting return data in standard Breeze JSON and without interfering with the data on the client side cache to have the 'octet-stream'.
The goal is to create an 'Export to Excel' without existing frontend paging for a large volume of data.
If you don't want to track changes, call EntityQuery.noTracking() before calling executeQuery(). This will return raw javascript objects without breeze tracking capabilities.
You can't make executeQuery() return binary 'octet-stream' data. But you can use breeze ajax implementation:
var ajaxImpl = breeze.config.getAdapterInstance("ajax");
ajaxImpl.ajax() // by default it is a wrapper to jQuery.ajax
Look http://www.breezejs.com/documentation/customizing-ajax
Create a custom "data service adapter" and specify it when you create an EntityManager for this special purpose. It can be quite simple because you disable the most difficult parts to implement, the metadata and saveChanges methods.
You don't want to cache the results. Therefore, you make sure the manager's metadata store is empty and you should add the " no caching" QueryOption. [exact names escape me as I write this on my phone].
Make sure these steps are really adding tangible value
Specialized server operations often can be performed more simply with native AJAX components.
p.s. I just saw #didar 's answer which is consistent with mine. Blend these thoughts into your solution.

What benefit do I get when I use Breeze JS with MS MVC pattern?

I need some help in understanding the benefit of using Breeze JS in a Visual Studio MVC project.
With MVC, using razor, I can bind my model properties to controls on the page and have any changes user makes sent back to the server without me writing any code. So, I am not really understanding if in this situation use of Breeze can be beneficial.
Certainly, I am not looking at the whole picture. And that is the reason I am asking this question here. Can anyone explain.
TIA.
kr
breeze is very helpful when you use ORM like NH\EF (also good for nosql and mongo)
if you do stuff like TPT\TPH and circular ref data it's hard to serialize by yourself
breeze takes care of that part for you
you can get your entities on the client side and keep change tracking
also no need to write sql\sp\functions to query data you can do all that on the client side
breeze supports client side cache with ease
breeze also works great with angularjs and knockout, if you bind data to the scope and then use breeze manager to delete or add more data no need to push or splice it breeze does that for you
breeze can do so much more so please check out the docs and features and find out what breeze can do for your project
I am using MVC Pattern and breeze will help us to integrate our typeScript interface generated from T4Ts taking the advantage of have strongly typed entities.
export interface Library extends breeze.Entity {
Category: string;
Name: string;
}
var manager = new breeze.EntityManager('api/library');
var query = new breeze.EntityQuery()
.from("Library");
manager.executeQuery(query).then(data => {
var employees = <Library []>data.results;
}).fail(e => {
alert(e);
});

EF CTP4 when to call context.SaveChanges

I'm trying to replicate many of the practices we use with NHibernate in EF CTP4.
Currently we have a generic repository interface (CTP4 actually makes this quite easy with DbContext.Set).
We are passing in the context and using StructureMap to scope it per HttpRequest.
With NHibernate we pass in ISession (again created using StructureMap) but use an ActionFilter (UnitOfWork) to commit the transaction at the end of the request.
Do we have equivalent transactions in EF CTP4? If not, should I use the same filter to call context.SaveChanges() or just do this inside the repository.
On a side note, what's the easiest way of testing to see whether an entity is new or not. With NH we have the luxury of just calling ISession.SaveOrUpdate. Note that all my entities use a Guid identifier. Perhaps a check for default(Guid)?
Thanks.
I know this is a late answer that you will have figured out by now but i guess its useful for people searching. Rob conery has a great post on using actionfilter and Ef to create transactions per request scope.
Rob C Article
Oh and yes I would create an UpdateInsert() method on a generic repository that test for default(Guid).

SubSonic 2.x StoredProcedure parameter data types?

From what I can see, SubSonic 2.x stored procedure parameter data types can only be of type System.Data.DbType.
Is there a quick way to add System.Data.SqlDbType so we can use System.Data.SqlDbType.Structured to pass tables - ultimately to TVP's?
In this particular project, all data caller functions are accessed through StoredProcedures.cs. If we switched to 3.x, does it support SqlDbType.Structured, and if so, is it a seamless integration to retain the current StoredProcedures.cs methods?
You should take a look here. Some guy with the same username and an identical avatar image just implemented that ;)
So, I created my own solution for this - SubSonic 2.x now supports TVP's - SqlDbType.Structure / DataTables for SQL Server 2008
I'm somewhat surprised at the lack of response to TVP's, and seemingly low traction TVP's have in online communities.

asp.net mvc tutorial of how to http post an xml file sought

I have been trying to find some step by step guidance on how to take a user's inputs (e.g.) name address, etc) via the view, to write that to xml, and then to submit that set of data to a third party interface (e.g UK Companies house xml gateway)
I have some aspects in hand (i.e. GET side of view to collect the data), but can't seem to get my head around using the controller and POST to submit the actual thing!
If anyone can steer me to a "For dumboids" tutorial, I'd highly appreciate it
Thanks
Paul
Side stepping the issue of whether you should be posting from your controller, you can use code like the following to post xml to a url. In the example below "xml" is your xml string and "url" is the url you want to post to. (Note: WebClient is built-into the .NET framework)
// create a client object
using(System.Net.WebClient client = new System.Net.WebClient()) {
// performs an HTTP POST
client.UploadString(url, xml);
}
I would question whether the controller should be posting directly to the XML gateway - this sounds like the job of a domain/business object sitting behind the controller, ideally referenced by an interface.
This was you can test your controller without requiring it to hit the XML gateway, and simulate the success/failure conditions.
If you can get the data already, I'd consider:
IGatewayPoster poster = ...
poster.Submit(dataSentFromView);
In the GatewayPoster class do the actual work of communicating with the XML gateway.

Resources