# Service architecture

The first thing that should happen when your application is loaded is the registration of services' implementations so that they are then accessible from anywhere within the application. This section explains how to bootstrap the required and custom services at the app’s entry-point.

Once you’ve created your application in [_BladeRunnerJS_](http://bladerunnerjs.org/), you’ll have a number of files in the _BladeRunnerJS_ Application directory structure. If you look at the MyApp/default-aspect/**index.html** file, you’ll see it includes the CSS, the JavaScript and then, in a script tag, creates an **App** using code loaded from your project (e.g. `myapp.App`). This code is defined in **_App.js_** in your _MyApp/default-aspect/src/myapp_ directory.

_Libraries_ do not do anything when loaded, it’s up to the person writing the application to create and wire up the parts of the libraries and services you are using.

## Standard Services

Services are used for two things: configuring the implementation of methods relied on by the _BladeRunnerJS Libraries_ (e.g. loading XML or HTML files), or allowing **blades** to provide services to other blades without creating a hard dependency between the blades. Services that many _BladeRunnerJSLibraries_ expect to be defined are:

1. The `[XmlResourceService,opts="nofollow"](https://docs.caplin.com/developer/api/caplin_trader/4/module-br_services_XmlResourceService)`, which provides access to XML bundled by the **XML Bundler**. This is sometimes used to load configuration files.
2. The `[HtmlResourceService,opts="nofollow"](https://docs.caplin.com/developer/api/caplin_trader/4/module-br_services_HtmlResourceService)`, which allows code to get the contents of HTML files that have been bundled together. This is typically used to load templates for **Presenter**.

Because these services are needed by many parts of the _BladeRunnerJSLibraries_, we provide default implementations for them.

## Custom Services

There are a number of other services that the _BladeRunnerJSLibraries_ use, and you can also define your own and use those too. Here’s an example of using one of the other defined services.

Many applications need to know the user name of the currently logged in user. In the following example a message displayed when a user trades should include the user’s name.

The `UserService` interface is defined in the library as having a single function, `getLoginName`, which returns a string. Caplin Trader provides an implementation of this, but you can create your own implementation of the `UserService` and register it. Below is the simplest possible `UserService`, but it’s also possible to create one that gets the login name via an XHR to a protected servlet or uses a streaming connection.

```js
mynamespace.MyUserService = function() {};
mynamespace.MyUserService.prototype = {
    getLoginName: function() {return "Bob";}
};
```

To use your new `UserService` you edit the `aliases.xml` file, in your default aspect’s resources directory. This will re-map the br``.user-service`` alias to point to your implementation. Once you’ve done this, all your code (including the BRJS Trader libraries) will start using your implementation.

```xml
<aliases xmlns="http://schema.br.com/CaplinTrader/aliases">
    <alias name="br.user-service" class="mynamespace.MyUserService"/>
</aliases>
```

This service can then be used in the code that generates the message:

```js
this.userService = br.core.ServiceRegistry.getService("br.user-service");
var buySellMessage = this.m_mTradeDetails['side'] == 'BUY' ? 'bought ' : 'sold ';
var sMessage = this.userService.getLoginName() + ' ' + buySellMessage + ' ' + this.m_mTradeDetails['amount'] + ' ' + this.m_mTradeDetails['dealtCurrency']
    + ' at the rate ' + this.m_mTradeDetails['rate'];
```

There are other services you may want to use, or for which you might want to provide custom implementations. For details on those, see the [Available Services](http://bladerunnerjs.org/docs/concepts/services/) documentation on bladerunnerjs.org.

---

**See also:**

* [Logging](http://bladerunnerjs.org/docs/use/logging/) (BladeRunnerjs.org)
