breeze odata and normal web api - angularjs

I need to have two entityManagers in an angular app using breese so that I can get data from two different services. One service is an OData API and the other is a standard breeze web api (asp.net web api using breeze controllers).
My OData context works fine but when I create the standard one the uriBuilder property is still odata and when this manager trys to get metedata is is prefixing the $metadata value onto the end of the url.
breeze.config.initializeAdapterInstance('modelLibrary', 'backingStore', true);
breeze.NamingConvention.camelCase.setAsDefault();
var serviceName = common.testApiUrl;
var metadataStore = createMetadataStore();
does anyone know if its even possible to have the two setup?

I have managed to get this working and the trick is to use the DataService to contruct the entitymanagers. This means that they're separate objects with their own configuration and you can indeed use OData and WebApi together.
Here is how I did it:
Create a data service object
var dataService = new breeze.DataService({
serviceName: myConfig.testApiUrl,
hasServerMetadata: true,
adapterName: 'WebApi'
});
Instantiate an entity manager using this data service
function newManager() {
var mgr = new breeze.EntityManager({
dataService: dataService
});
return mgr;
}
You can then use this same pattern for however many you need and just change the adapter name. There is some extra config but all standard breeze stuff so I haven't included it all here.
I then create a DataContext for each EntityManager to encapsulate them and I can then just inject the context that I need as and when I need it.

Related

Best way to use database connection for every get/post request in .net core web api service

I have .net core web api service. Should i open new connection and close for every get/post request? Or, is there a performance way to use db connection like a global connection variable?
Yes. You use the AddDbContext extension method to configure your DbContext and it'll automatically create a Scoped instance of the context which is created and disposed with each request:
var connection = #"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.AspNetCore.NewDb;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<BloggingContext>
(options => options.UseSqlServer(connection));
For a full example visit: https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db?tabs=visual-studio

Blocking / Initialization service with angular.js

My apps are using many web services on the intranet, and url-s for those depend on the server environment.
My apps are hosted on IIS, which adds an HTTP response header like this: Environment: DEV, so every web app knows in which server environment it is running, and thus which intranet servers it must use to call all the services.
Each of my angular apps uses a service that issues a simple GET against the app's own root just to get any response with the environment name in it, and set configuration accordingly.
Question:
How should an angular app implement such a service that would execute as the very first thing in the application, and make sure that while it is getting that first response, nothing in the app tries to execute an HTTP request against other services, or even try to use any configuration provided by my environment service?
Is there a way to implement such a service in angular that could block every other service / factory in the application till it is done initializing itself?
I have many other services in the app, and none of them really know what to do till my environment service has finished its initialization.
UPDATE
Looking at it from another angle.... is it possible to implement such an interceptor in angular that could do the following?:
execute an HTTP request and block the app's execution till it gets a response
make information from the response available throughout the app as a service/factory/config.
Angular lifecycle could be one solution. Using the angular.config() phase you could peek at the headers of the HTTP service.
Create a factory called 'httpInterceptor'
function httpInterceptors(siteConfig, $q, $injector) {
return {
response: function(data, status, headers) {
siteConfig.setEnvironment(headers['Environment']);
return data;
}
};
)
Then in angular.config()
$httpProvider.interceptors.push('httpInterceptor');
If you truly want to block the other option is to use UI router resolve property to block routes loading until the request has been made https://github.com/angular-ui/ui-router/wiki you can add the resolve method to the root state.
Resolve
You can use resolve to provide your controller with content or data that > is custom to the state. resolve is an optional map of dependencies which > should be injected into the controller.
If any of these dependencies are promises, they will be resolved and converted to a value before the controller is instantiated and the $stateChangeSuccess event is fired.

Accessing Session values in Angular.js

I am unable to access the session values which is set by node.js in Angular.js controller. I am using the Express framework. How to resolve it? Here is my code.
app.use(express.cookieParser());
app.use(express.session({
secret: '1234567890QWERTY',
cookie: { httpOnly: false }
}));
//setting the values
cookies.set('username',username);
req.session.username=username;
Presumably you want to do something like show the username in your angular app. As I mentioned in this answer, the hard part to Angular is not thinking about what data the server has, but thinking about data the server should provide to the browser via API.
My general model is to have the angular page start up without any data and have a low-level controller invoke a service (say AuthenticationService or IdentityService) that requests relevant data from the server, e.g. HTTP GET /api/identity. That will return a block of JSON that the page can then store in that low-level controller. Any deeper controller can then access the identity (or whatever) data loaded in that first request.

The best way to set up app variables when the app is initializing

Before I start, I am using a .Net back end.
From the server I want to send app specific details such as api end points to the angular app to use to populate the page with data. Thses end points will be sent to the client side angular app.
I have an api and the api locations may change from time to time so I want this to be handled in the back end. If these are changed I don't want to have to change my front end code.
So before I can hit these apis I need to know the end points.
What is the best way to bootstrap my app to have these available before I query the api.
Create an angular service for holding configuration items such as endpoints.
Creating an Angular Service
Poll the server to get the endpoints while initialising the service (the endpoint for this call needs to be in the front-end at least e.g. "/api/endpoints" unless you are generating a global on the serverside somehow).
You can then inject the service to any module you'd like; to get the endpoints.
You can have a static endpoint in your backend's API that would return all the end points and then use them on your angular app
Alternatively you can have a script tag on your body:
<script type="text/javascript">
//add your variables
var backendVariable1 = #(myVar1); //razor syntax?
var backendVariable2 = #(myVar2);
</script>
And then, somewhere inside your angular app (it could be in a service for example):
angular.module('myApp').factory('BackendVariables',
function() {
this.variables.variable1 = window.backendVariable1;
this.variables.variable2 = window.backendVariable2;
return this.variables;
}
);

Asyc Web Service/HTTPS call not working in Titanium mobile?

We are using more than one Web Service( HTTPS call) to get data from server.
We are uploading a file to server in a Web Service and at same time we are making another Web Service call to get/send data to/from the same server.
But we are not getting response from 2nd Web service till first Web service (upload receipt) succeed or failed.
So our doubt is how Titanium send Web service call when more than one Web service send from the Titanium Application.
Is the web service call from the Titanium app queued (Sync)or is it called in parallel to other Web service (Async call)?
In our Titanium app we are creating a new HTTPClient object for each Web service call, using the following code to create a new HTTPClient object:
function runService(){
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function() {
var r = this.responseText;
}
xhr.open("GET", URI);
xhr.send();
}
I think you need to pass true for async
xhr.open("GET", URI, true);

Resources