ExecutorService based multi-threading in Java

A threading feature within Java is the ExecutorService. This allows us to, in essence, create our own threadpools. This is useful because it allows us to create a limited number of threads that our code may run on, ensuring we do not end up with thread starvation or the number of threads getting out of hand.

By writing our code to use the ExecutorService we can also limit our methods to only use a single thread if we want.

Here’s a simple example of using the ExectorService

public static void main(String[] args) throws 
   InterruptedException, 
ExecutionException {

   System.out.println("Main Thread - " + Thread.currentThread().getId());

   //ExecutorService executorService = Executors.newSingleThreadExecutor();
   ExecutorService executorService = 
      Executors.newFixedThreadPool(
         Runtime.getRuntime().availableProcessors());

   Future<?> f1 = executorService.submit(() 
      -> outputInformation(1));
   Future<?> f2 = executorService.submit(() 
      -> outputInformation(2));
   Future<?> f3 = executorService.submit(() 
      -> outputInformation(3));

   // the equivalent of waiting on the threads
   f1.get();
   f2.get();
   f3.get();

   executorService.shutdown();
}

private static void outputInformation(int id) {
   try {
      // some arbitrary time wasting
      Thread.sleep(2000);
      System.out.println(id + " - " + Thread.currentThread().getId());
   }catch (Exception e) {
   }
}

In the code above, we are creating our own threadpool (using Executors.newFixedThreadPool) with the size based upon the number of available processors. Hence in this example (assuming you have more than 1 processor on your machine) the resulting output is indeterminant, i.e. we might see 2 followed by 1, followed by 3 and all on different threads.

By simply changing to use Executors.newSingleThreadExecutor() the output will be in the correct order as only a single thread is used for each call.

The ExecutorService is a useful abstraction for containing and controlling multiple threads.

Spring boot and GraphQL

A previous post Writing a GraphQL service using graphql-dotnet and ASP.NET core covered implementing an ASP.NET application with a GraphQL service.

As GraphQL is language/platform agnostic I wanted to see how to implement a similar little service using Java frameworks.

I’m using JetBrains’ IntelliJ (I think you need the ultimate edition for this) to create a project, so I’ll list a few steps regarding this process. In IntelliJ

  • Create a new Project
  • Select Spring Initializr
  • Leave the defaults as https://start.spring.io and just press the Next button
  • Supply a name for your project then press the Next button
  • At the next page you can select dependencies, just press the Next button
  • Finally give your project a name then press the Finish button

Now let’s update the pom.xml with the following

<!-- Add the following for GraphQL -->
<dependency>
   <groupId>com.graphql-java</groupId>
   <artifactId>graphql-spring-boot-starter</artifactId>
   <version>5.0.2</version>
</dependency>
<dependency>
   <groupId>com.graphql-java</groupId>
   <artifactId>graphql-java-tools</artifactId>
   <version>5.2.4</version>
</dependency>

<!-- Add this for the GraphQL interactive tool -->
<dependency>
   <groupId>com.graphql-java</groupId>
   <artifactId>graphiql-spring-boot-starter</artifactId>
   <version>4.0.0</version>
</dependency>

As shown with the comments, the last dependency will allow us to include the GraphQL interactive web application graphiql, obviously remove this if you do not need it.

Now let’s write some code…

Schema and Queries

Unlike the previous C# example, we’re going to use the GraphQL schema definition language to declare our schema (in C# we used code to implement this).

In your package folder add Person.java with the following code

public class Person {
    private String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

It’s basic but this is our starting point (and matches what we did in the C# example).

We now need to create a QueryResolver, so add a file named Query.java to the package with the following code

import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class Query implements GraphQLQueryResolver {
    List<Person> people = new ArrayList<>();

    public Query() {
        people.add(new Person("Scooby"));
        people.add(new Person("Shaggy"));
        people.add(new Person("Daphne"));
        people.add(new Person("Thelma"));
        people.add(new Person("Fred"));
    }

    public List<Person> people() {
        return people;
    }
}

That’s it for the Java code for now – Spring Boot’s dark magic will wire everything up for us, but before then we need to add a .graphqls file. Within the resources folder lets add a schema file named schema.graphqls (IntelliJ comes with a plugin for recognising this file type).

Note: the name of the file need not be schema, any name will suffice for this example.

Within the schema.graphqls file we have the following

type Query {
    people : [Person]
}

type Person {
    name: String!
}

That’s it!

Running the application will result in a Tomcat embedded server running on http://localhost:8080/graphiql (assuming you include graphiql in the Maven file). If we now execute the following query within graphiql

{
  people {
    name
  } 
}

we should see the result

{
  "data": {
    "people": [
      {
        "name": "Scooby"
      },
      {
        "name": "Shaggy"
      },
      {
        "name": "Daphne"
      },
      {
        "name": "Thelma"
      },
      {
        "name": "Fred"
      }
    ]
  }
}

Let’s now add an operation find to allow us to locate a Person by their name. In the Query class, add the following

public Person find(String input) {
   return people.stream()
      .filter(p -> p.getName().equalsIgnoreCase(input))
      .findFirst()
      .orElse(null);
}

This code acts a little like a Linq query in C#, returning an Optional, we’ll basically return a null if no item was found hence unwrapping our return from the Optional.

We need to add this operation to the schema.graphql Query type, so it now looks like this

type Query {
    people: [Person]
    find(input: String!) : Person
}

Running the application we can now write the following in graphiql

{
   find(input : "Scooby") {
      name
  }
}

Mutations

Next up, let’s write a mutation to add a person. We’re not going to add a Person to the list of people as this would require some refactoring of the query data, but it simply demonstrates the changes required to our project to implement mutations.

Let’s jump into the schema file and add mutation to the schema definition, so it looks like this

schema {
    query: Query
    mutation: Mutation
}

and now add the mutation type into the file, which should look like this

type Mutation {
    addPerson(input: String) : Person
}

Now we’ll create the code. Add a file (mine’s called Mutation.java) for our mutation code, here’s a the very simple example

import com.coxautodev.graphql.tools.GraphQLMutationResolver;
import org.springframework.stereotype.Component;

@Component
public class Mutation implements GraphQLMutationResolver {
    public Person addPerson(String input) {
        return new Person(input);
    }
}

That’s all there is to it, run the application and using grapiql, write the following in your query window

mutation addPerson($input : String) {
   addPerson(input : $input)
   {
      name
   }
}

and in the Query variables window add the following

{
  "input" : "Scooby Doo"
}

Subscriptions

Finally let’s write some subscription code. Change the schema.graphqls schema to look like this

schema {
    query: Query
    mutation: Mutation
    subscription : Subscription
}

and add the following type

type Subscription {
    personAdded : Person
}

The Subscription.java file will look like this

import com.coxautodev.graphql.tools.GraphQLSubscriptionResolver;
import org.reactivestreams.Publisher;
import org.springframework.stereotype.Component;

@Component
public class Subscription implements GraphQLSubscriptionResolver {

    private PersonPublisher publisher;

    public Subscription(PersonPublisher publisher) {
        this.publisher = publisher;
    }

    public Publisher<Person> personAdded() {
        return publisher.getPublisher();
    }
}

This implements our Subscription resolver, but we’ll separate out the publisher code to look like this. Here’s my PersonPublisher.java file

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.observables.ConnectableObservable;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

@Component
public class PersonPublisher {

    private final Flowable<Person> publisher;

    public PersonPublisher() {
        Observable<Person> o = Observable.interval(1, 1, TimeUnit.SECONDS)
                .map(l -> new Person(l.toString()));
        ConnectableObservable<Person> co = o.share().publish();

        publisher = co.toFlowable(BackpressureStrategy.BUFFER);
    }

    public Flowable<Person> getPublisher() {
        return publisher;
    }
}

Note: I’ve based the code for the Subcription and PersonPublisher on the Subscription.java and the StockTickerPublisher.java sample code.

Code

Code is available on GitHub.

Disclaimer

graphiql does not display subscription events at this time. So I’ve not tested this subscription code. Hence beware, it may or may not work. I’ll try to update when I get chance to test it fully.

Git aliases

In a previous post on git (Using GIT from the CLI), I listed a fair few commands which are very useful. For the most part they’re fairly simple commands/options, but occasionally they become less easy to remember.

For example to check for merge conflicts on a branch that you may wish to merge master to, you can type

git merge master --no-ff --no-commit

Luckily I have this blog post allowing me to copy and paste this command, an alternate might be to create an alias to this command.

Adding an alias

Creating the alias is simply a way to store a command name within git’s global config which runs another command. Whether we want to simply shorten a name, like using co and ci in place of checkout and commit or shorten a command such as the merge above with check-conflicts.

To create an alias we use the following

git config --global alias.check-conflicts 'merge master --no-ff --no-commit'

In this example we’re telling git to add configuration using –global option (so the configuration is across all repositories or –local for just this repository). We create the alias using the alias option followed by a period/full stop and then the name we want for our alias. This is then followed by the git command and options (within single or double quotes if there are multiple options).

Now we can execute

git check-conflicts

Deleting an Alias

To delete an alias we can either remove it from the config file (for example .gitconfig) or run the following command line

git config --global --unset alias.check-conflicts

Listing Aliases

We can look in the .gitconfig or run the following command line to list the available aliases

git config --get-regexp '^alias\.'

Creating an Alias to list Aliases

To round this up nicely, let’s now create an alias to list aliases, for example

git config --global alias.alias "config --get-regexp ^alias\."

Writing a GraphQL service using graphql-dotnet and ASP.NET core

Introduction

GraphQL is classified as a query language for querying your data.

I’m not going to go into a full description of it’s capabilities/uses, for that go to the GraphQL website, but one thing which has always interested me, is an API where the client application can tell the server API what data to return. Thus allow the client to reduce bandwidth as well as reduce the amount of data being deserialised to exactly what the client application requires – obviously particularly useful for mobile applications.

Using graphiql-dotnet and ASP.NET core

We’re going to be using graphql-dotnet and ASP.NET core to implement a basic

  • Create an ASP.NET Core Web Application, mine’s named GraphQLService
  • Select an Empty project
  • I’m going to uncheck “Configure for HTTPS”
  • Now add the following packages via NuGet
    • GraphQL.Server.Transports.AspNetCore
    • GraphQL.Server.Transports.WebSockets
    • GraphQL.Server.Ui.GraphiQL
    • GraphQL.Server.Ui.Playground
    • GraphQL.Server.Ui.Voyager
    • GraphQL-Parser

Note: We’ve added the GraphQL.Server.Ui.Playground package to allow us to write queries within an interactive environment (http://localhost:5000/ui/playground) which also allows us to view the schema definitions etc. We’ve added GraphQL.Server.Ui.Voyager which adds a cool schema/type viewer (http://localhost:5000/ui/voyager). Finally we’ve added GraphQL.Server.Ui.GraphiQL to allow us to use the well known GraphiQL interactive environment (http://localhost:5000/ui/graphiql), feel free to remove all or some of these as required.

Remove the code from the Configure method of the Startup.cs file except for the following

if (env.IsDevelopment())
{
   app.UseDeveloperExceptionPage();
}

Now we’re going to add some tools, the playground (an interactive query web app) the voyager for digging into the schema and the GraphQL endpoint. So add the following to the Startup.cs below the code shown above

app.UseWebSockets();
app.UseGraphQLWebSockets<PersonSchema>("/graphql");
app.UseGraphQL<PersonSchema>("/graphql");
app.UseGraphQLPlayground(new GraphQLPlaygroundOptions()
{
   Path = "/ui/playground",
});
app.UseGraphiQLServer(new GraphiQLOptions
{
   GraphiQLPath = "/ui/graphiql",
   GraphQLEndPoint = "/graphql"
});
app.UseGraphQLVoyager(new GraphQLVoyagerOptions()
{
   GraphQLEndPoint = "/graphql",
   Path = "/ui/voyager"
});

Finally within Startup.cs, within the ConfigureServices method add the following code, which will register some types (which we’ll be supplying soon, the schema and query classes) and configures the GraphQL middleware.

services.AddSingleton<PersonSchema>();

services.AddGraphQL(options =>
{
   options.EnableMetrics = true;
   options.ExposeExceptions = true;
})
.AddWebSockets()
.AddDataLoader();

Schema

At this point we can see a need for the classes PersonSchema and PersonQuery. As you’ve realised, we are needing to create a schema object which will define our queries and types that we can query against. We’ll just create the bare bones schema class in PersonSchema.cs

public class PersonSchema : Schema
{
   public PersonSchema()
   {
      Query = new PersonQuery();
   }
}

Queries

Now we’ll create a bare PersonQuery.cs file which will be the starting point for some query functionality

public class PersonQuery : ObjectGraphType
{
}

At this point we could compiler and run the ASP.NET core application and navigate to http://localhost:5000/ui/playground to view the playground web application. However there’s not much we can do at this point.

Let’s add a domain object which represents a Person object, so add the following class. This represents the class that might come from a database or from another service

public class Person
{
   public string Name { get; set; }
}

For graphql-dotnet to allow us to use this class we need to wrap our Person object within a graphql-dotnet type, so create the following PersonType

public class PersonType : ObjectGraphType<Person>
{
   public PersonType()
   {
      Field(o => o.Name);
   }
}

In this case graphql-dotnet will now map fields to the Person object via the Field methods. This type then needs to be added to the PersonQuery class, to allow for querying again PersonType (and then ultimately Person) data. So add the PersonQuery constructor with the following code

public PersonQuery()
{
   Field<ListGraphType<PersonType>>("people", resolve: context => new[]
   {
      new Person {Name = "Scooby Doo"},
      new Person {Name ="Daphne Blake" },
      new Person {Name ="Shaggy Rogers" },
      new Person {Name ="Velma Dinkley" },
      new Person {Name ="Fred Jones" }
   });
}

Now if we run the application and navigate to http://localhost:5000/ui/playground we’ll find a Schema tab which displays information about how to query our data and if we write the following query

{
  people {
     name
  }
}

and run it from the playground we’ll see a list of the names from our Person data.

The previous example omitted the query keyword, i.e. it would look like this in a more formal definition of a query

query {
  people {
     name
  }
}

We can also implement our own query methods that might be called using the following (the C# for this is shown further down the post)

find(input : "Scooby Doo") {
   name
}

Or we can create a query which takes parameters/arguments from query variables. This example shows the use of an operation name, i.e. getPerson.

Note: operation names can be used for queries, mutations and subcriptions.

query getPerson($input : String) {
  find(input : $input) {
    name
  }
}

along with the input

{
  "input" : "Scooby Doo"
}

We’ll now create an input type in C# which will work with the above queries, such as

public class PersonInputType : InputObjectGraphType
{
   public PersonInputType()
   {
      Field<StringGraphType>("input");
   }
}

Now add another field to the PersonQuery constructor like this

Field<PersonType>(
   "find", 
   arguments: new QueryArguments(
      new QueryArgument<StringGraphType> 
      {
         Name = "input"
     }),
   resolve: context =>
   {
      var i = context.GetArgument<string>("input");
      return new Person {Name = i.ToString()};
   });

In the above, we essentially have created a new field in the PersonQuery which acts like a method, it is of type PersonType (the return type in this instance) and named find it takes a single argument of type string and expects the argument name of input. Finally it resolves to a method which we can then write something more substantial code for querying our data. In this simple example we’re just returning the argument passed in.

Note: We can also create multiple queries, so for example say we want to locate Person A and Person B and return data as a single result, we could write

{
   A : find(input : "Scooby") {
      name
   }
   B : find(input : "Shaggy") {
      name
   }
}

In the above query we use a GraphQL feature using aliases, the A and B acting like variable names. This allows us to create multiple queries against the same query/method.

When our queries start to become more complicated, we might prefer to place the field selection into, what’s know as a fragment, for example

{
   find(input : "Scooby") {
    ...fields
   }
}

fragment fields on PersonType {
  name
}

Obviously in our example it took more work to create and use the fragment that just using the field name, but you get the idea. However fragments can also be reused if we had multiple queries, so hence with more fields this become a much more useful technique.

Mutations

We’ve created our queries, which is great for scenarios where we’re simply reading data, but we can also create mutations, i.e. code that changes our data.

We can write a GraphQL mutation like this

mutation addPerson($input : String) {
   addPerson(input : $input)
   {
      name
   }
}

along with input such as

{
  "input" : "Scooby Doo"
}

What we now need to do is create our mutation class, like this

public class PersonMutation : ObjectGraphType<Person>
{
   public PersonMutation()
   {
      Field<PersonType>("addPerson",
         arguments: new QueryArguments(
            new QueryArgument<StringGraphType> {Name = "input"}),
         resolve: context =>
         {
            var n = context.GetArgument<string>("input");
            return new Person { Name = n };
         });
    }
}

We also need to alter the PersonSchema constructor to look like this

public PersonSchema()
{
   Query = new PersonQuery();
   Mutation = new PersonMutation();
}

Subscriptions

As you might expect, subscriptions are a way to handle something similar to events, hence we can connect to our service and received updates. Here’s a simple example of a query

subscription {
  personAdded {
    name
  }
}

Here’s a simple example of a subscription, to keep things simple this will simply send a message every second to any subcribers

public class PersonSubscription : ObjectGraphType
{
   public PersonSubscription()
   {
      AddField(new EventStreamFieldType
      {
         Name = "personAdded",
         Type = typeof(PersonType),
         Resolver = new FuncFieldResolver<Person>(ResolvePerson),
         Subscriber = new EventStreamResolver<Person>(SubscribePerson)
      });
   }

   private Person ResolvePerson(ResolveFieldContext context)
   {
      return context.Source as Person;
   }

   private IObservable<Person> SubscribePerson(ResolveEventStreamContext context)
   {
      return Observable.Interval(
         TimeSpan.FromSeconds(1))
           .Select(s => new Person 
           {
              Name = s.ToString()
           });
   }
}

Now we need to update the schema constructor like this

public PersonSubscription()
{
   AddField(new EventStreamFieldType
   {
      Name = "personAdded",
      Type = typeof(PersonType),
      Resolver = new FuncFieldResolver<Person>(ResolvePerson),
      Subscriber = new EventStreamResolver<Person>(SubscribePerson)
   });
}

Further reading

Refer to the GraphQL site for in depth examples and more complete explanations of the everything I’ve discussed above.

Also check out the GraphQL.net web site for graphql-dotnet documentation.

If you’re interested in code for the project, it’s available on GitHub.

Azure page “Hmmm… Looks like something went wrong” in Edge

I just tried to access my Azure account via Microsoft Edge and logged in fine but upon clicking on Portal was met with a fairly useless message

Hmmm... Looks like something went wrong

Clicking try again did nothing.

On searching the internet I found that if you press F12 in Edge to display the developer tools, the Cookies node had the following UE_Html5StorageExceeded.

Running localStorage.clear() from the developer tools console window is suggested here. Sadly this failed with an undefined response.

However Edge Crashing with HTML5 storage exceeded suggests going to Edge | Settings | Clear browsing data, clicking the button Choose what to clear and selecting Choose Cookies and saved website data followed by clicking the Clear button did work.

Oh and yes, sadly you will need to login again to various websites that you probably left logged in permanently.

Pre-emptive scheduler on Google Cloud

Having used Azure on and off for a bit, I’m now looking into the GCP (Google Cloud Platform) for a project and whilst this might not be to everyone’s interest, there’s a very interesting youtube video “Google Cloud Platform on a shoestring budget (Google I/O ’18)” on cost savings on GCP (especially interesting for start-ups or smaller developers but also for larger scale development).

Anyway, one interesting cost saving mentioned is, if your application runs on a compute engine/VM but you are okay with GCP killing your instance if/when it requires further resources, then cost savings (according to the video) can be from $24.67 per month down to $7.30 per month (costs may have changed since the video) running on an n1-standard1.

Why this is interesting is that, when writing for mobile, for example, we have a similar design paradigm to be aware of. The mobile application may be running but then at some point the OS wishes to free up space and stops the application. With the GCP scheduler’s pre-emptive capability we need to design our cloud solution in the same way as our mobile. i.e.

  • We’re informed for GCP (or OS on mobile) that the application is being suspended
  • We need to store our state somewhere, in the case of GCP we have approx. 30 seconds to do this
  • When GCP (or mobile OS) decides it can restart our application we need to read back our stored state get our applications back into the state they were prior to suspending

Note: If we run a load balancer in front of our compute engine we can better handle spinning up compute power when required.

In reality, unlike a mobile application, the notification of shutdown of your application is done at an OS level, through ACPI G2 Soft Off, so it’s generally better (from what I can see) to run your code in a stateless, almost serverless way. For example, we might run jobs as pre-emptive instances which take a message off of a pubsub or from a DB, process data and then mark to message/DB row as completed when the application has finished – in such a scenario, if the application is interrupted and shut down, the next application to be spun up and start work on the same data.

References

Creating and Starting a Preemptible VM Instance
Preemptible VM Instances
Saving Money with Preemptive VMs (Cloud Performance Atlas)

Platform specific code with Xamarin and Prism

When writing platform specific code within our iOS, Android and UWP projects we can use the built-in lightweight dependency injection code and attributes that comes out of the box with Xamarin, but when using Prism we’re probably using Unity or DryIoc as our preferred IoC library.

Let’s look at a simple example, whereby I have an interface in my shared code, which looks like this

public interface IFileService
{
   void Save(string data);
}

although there is file handling code etc. in .NET standard and we can implement something like this in the shared code, maybe we want some other more specific platform code. Here’s an Android file (i.e. within the Android project)

[assembly: Dependency(typeof(FileService))]
namespace Droid.Services
{
    public class FileService : IFileService
    {
        public void Save(string data)
        {
            // some Android specific code
        }
    }
}

The use of the DependencyAttribute makes this class available via Xamarin Forms built-in DependencyService, but not available to DryIoc, for example – hence what we want to do is use the built-in DI container and map this to our preferred IoC library.

All we need to do in our shared project’s App.xaml.cs file within the RegisterTypes method, we just do something like this

containerRegistry.RegisterInstance(DependencyService.Get<IFileService>());

Simple.

Adding SQLite to my Xamarin forms application

To be totally honest, this post Xamarin.Forms Local Databases covers everything you need to know.

Simple add Frank Krueger’s NuGet package to your shared code project. This library is both a database access library and ORM.

We’ll need to create the database file, hence we can create our file location using the following

private static readonly string DatabaseLocation =
   Path.Combine(
      Environment.GetFolderPath
        (Environment.SpecialFolder.LocalApplicationData), 
        "Capture.db3");

Create the database

var database = new SQLiteAsyncConnection(DatabaseLocation);
await database.CreateTableAsync<DataItem>();

Depending on the use case it may be best to create the database connection when the application starts and keep available whilst the application is running. In this instance my calls to the DB are going to be dictated by the user, i.e. pressing a button, so we can create the connection, then create a table for our data objects.

Note: The data object stored into the database must have a default constructor and not be abstract etc. for it to work with the ORM part of this library.

Closing the database

The database connection does not support the IDisposable interface, so we need to explicitly call

await database.CloseAsync();

Note: I’ve not checked the source for the library and had no issues with not closing the connection.

Inserting items into the database

We can insert new items into the database using

await database.InsertAsync(dataItem);

// or a batch insert using 

await database.InsertAllAsync(dataItems)

// or an insert OR replace using

await database.InsertOrReplaceAsync(dataItem)

Updating rows

We saw that we can InsertOrReplace rows but this is based upon whether the object is unique. In other words the data object is first inserted and then a key returned, if another object matches this newly inserted one, then it’s deleted.

We can call UpdateAsync if we know a data object has already been saved to the database and we just want to update it, i.e.

await database.UpdateAsync(dataItem);

and thus if we’re tracking, within our data object whether it’s been saved and decide whether to update or insert using

if(dataItem.Id != 0)
{
   await database.UpdateAsync(dataItem);
}
else
{
   dataItem.Id = await database.InsertAsync(dataItem);
}

Querying the database

It wouldn’t be much of a database if we couldn’t run queries on it. We can use

var list = 
   await database.QueryAsync<DataItem>(
      "SELECT * FROM [DataItem] WHERE [Success] = 0"
   );

We can also use Linq like this

var dataItem = 
   database.Table<DataItem>()
      .Where(i => i.Name == "Samsung").ToListAsync();

Hence here we’ll get a list of all DataItems with the name “Samsung”.

Deleting data

We can use

await database.DeleteAsync(dataItem);

Deleting the database

As the database is stored as a file we can simply delete the file from the file system using

if (File.Exists(DatabaseLocation))
{
   File.Delete(DatabaseLocation);
}

More on our data objects

The only thing we’ve specified in any of the above code, for our data objects, is that they must have a default constructor on a concrete class, but as this SQLite NuGet package is acting like an ORM (as opposed to us creating our tables etc. in SQL), we’ll probably need some of the following properties added to our data object…

In the above we’ve showed that we can get a key back from an insert etc. but we can also add a key to the data objects themselves and have SQLite supply the keys. So for example we can add the following to our data item class

[PrimaryKey, AutoIncrement]
public int Id { get; set; }

Now the id is our primary key and is automatically supplied during an insert using an AutoIncrement algorithm.

We can also add indexed properties, for example

[Indexed]
public string Name { get; set; }

We can ignore properties by marking them with the IgnoreAttribute, i.e.

[Ignore]
public int AverageDb { get; set; }

Other attributes, include the following UniqueAttribute, MaxLengthAttribute , NotNullAttribute, StoreAsTextAttribute (for enums) and CollationAttribute (BINARY, NOCASE, RTRIM are supported with BINARY being the default) for comparisons.

Transforming config files using SlowCheetah

It’s not unusual for our applications to require different configuration files (whether App.config or other) which might reflect different environments or build configurations. Under such circumstances I’ve tended to stick to using (good old) Nant to switch tokens in my configuration files as and when required.

An alternative to Nant or Cake etc. is Microsoft’s SlowCheetah, which allows us to create transformations within our configuration files, whether XML or JSON based as part of the build process within Visual Studio. These files will be named after the build configuration that you build your project with, i.e. Debug, Release etc.

To try this out, we first need to install SlowCheetah. So from Tools | Extensions and Updates, search for SlowCheetah and download it – you’ll need to restart Visual Studio to get it to installed.

Once installed we can right mouse click on the App.config and should see the option Add Transform.

Note: When we first add a transform Visual Studio will ask you to install the SlowCheetah NuGet package, select Yes when prompted.

Once you’ve executed Add Transform against the App.config you’ll find child nodes in solution explorer for App.Debug.config and App.Release.config (and any other build configurations you have).

If you right mouse click on either of these new config files, the context menu should show the option to Preview Transform. As the name suggests, this allows us to see what the transform will look like instead of having to build our solution to view the App.config within the bin folder.

Transforming .config files

In our simple implementation/example, we’ve got three App.config files now. The original App.config (which is ultimately what we’ll see in the bin folder as the exe.config) and a version of this file for Debug and Release builds.

What we need to do is add some configuration into the App.config as normal, so for example add the following

<appSettings>
   <add key="environment" value="NONE" />
</appSettings>

We can now add configuration to the Debug and Release config files to override this value. To save on duplicated config let’s concentrate on the App.Debug.config, so when we build using the Debug configuration SlowCheetah will transform our App.config with instructions from the App.Debug.config. Here’s an update to the App.Debug.config file

<appSettings>
   <add key="environment" value="DEBUG" 
      xdt:Transform="Replace" 
      xdt:Locator="Match(key)" />
</appSettings>

Note: the syntax for XML transformations is taken from XDT and for JSON from JDT.

As you can probably work out for yourself, we’re telling the transformation engine to replace the value for the given key. Without these two attributes no transformation takes place. Just adding configuration to this file, i.e. if we added another key/value solely to the App.Debug.config, will simply be ignored unless we tell the transformer what to do. For example if we wanted the App.Debug.config build to insert/add a new app settings key/value we can tell the transformer to do this using

<add key="url" 
   value="http://somewhere.com" 
   xdt:Transform="Insert"/>

Transformation syntax for XDT can be found here Transform Attribute Syntax.

Transform attributes include

  • Replace
  • Insert
  • InsertBefore
  • InsertAfter
  • Remove
  • RemoveAll
  • RemoveAttributes
  • SetAttributes

Transforming .json files

We may be using JSON files for configuration or support files (for example when creating .NET core applications or in any other type of application). SlowCheetah allows transformations on JSON as well using JDT.

Let’s add a JSON file to our test application, mine’s named Config.json and includes the following

{
  "appSettings" : {
     "environment" : "None" 
  }
}

Now in solution explorer within Visual Studio, right mouse click on Config.json and select Add Tranform. Again we’ll concentrate solely on the Config.Debug.json file that’s created as part of this process. Add the following to this file

{
  "appSettings" : {
    "@jdt.replace" : {
      "environment" :  "Debug" 
    } 
  }
}

As I’m sure you can see, we’re aiming to replace the value of the environment key to Debug (as we did with the .config previously). Obviously to see this happen and output to the relevant bin folder, select the Config.json (parent node) and set its property Copy to Output Directory to Copy if newer, now select each of the child nodes and switch those back to Do not copy as Visual Studio will set the child nodes to the same build action as the parent and we do not want/need these environment configurations in our output folder.

JDT includes the following verbs

References

SlowCheetah
SlowCheetah on GitHub

Using Openfin InterApplicationBus

In a previous post (Remoting using WCF) I covered interprocess communications using remoting and named pipes. If you’re using Openfin there’s an InterApplicationBus which basically allows for the same interprocess communication/messaging functionality.

Let’s see how this works. Let us assume we’re using Openfin and we have one application which should send a message and a second application which will receive a message.

Let’s see how to write some code that responds to messages

We’ll start by creating a WPF/Winforms or other application that remains running until explicitly closed. We want the application to remain open and wait listening for messages.

Add the OpenfinDesktop NuGet package (Install-Package OpenfinDesktop) to your application. Now within the class of your main window/form place the following

using Openfin.Desktop;
// may need this 
// using Window = System.Windows.Window;

we’ll make it simple by writing all our code within the MainWindow.xaml.cs constructor (for our WPF app.). So add the following to the constructor after InitializeComponent();

var runtime = Runtime.GetRuntimeInstance(new RuntimeOptions {Version = "9.61.38.40" });
runtime.Connect(() =>
{
   runtime.InterApplicationBus.subscribe("my-topic", (uuid, topic, message) =>
   {
      if (message != null)
      {
         Debug.WriteLine(message.ToString());
      }
   });
});

The Version used is important as it relates to code that is either already available on your machine or it should download, see OpenFin Versions. In my case this downloaded the latest code and hence when I ran the application I got an Openfin popup showing the download progress. The runtime etc. will be stored on your local storage (by default) in %localappdata%\OpenFin.

Also be aware – as you’d probably assume, the InterApplicationBus is running on it’s own thread, hence when you are handling messages it’s your responsibility to handle any marshalling, for example onto the GUI/Main thread.

We can also handle messages in a more event style for example, replace the code within the Connect method (above) with the following (this code also rids us of the lowe cased subscribe method name which is not in keeping with the C# naming conventions :-), beyond that I’m not wholly sure at this time if there’s any other difference in this code).

InterApplicationBus.Subscription<string>(runtime, "my-topic").MessageReceived += (sender, msg) =>
{
   if (msg != null)
   {
      Debug.WriteLine(msg.Message);
   }
};

Let’s see how to write some code that publishes messages

Create an second application, again make it a WPF/WinForms application so we don’t have to write code to keep the application open whilst we connect and send messages as these are asynchronous in nature.

Again, add the OpenfinDesktop NuGet package and using statements but within the constructor we’ll write the following code

var runtime = Runtime.GetRuntimeInstance(new RuntimeOptions { Version = "9.61.38.40" });
runtime.Connect(() =>
{
   runtime.InterApplicationBus.Publish("my-topic", "Hello World");
});

As you’d assume, this publishes a message to the bus with the topic my-topic, which our first application listens for. In this case a simple string (good old Hello World) is being sent, but we could send something a little more complex like a JSON message or the likes.

Now simply run the first application and once connected run the second and you should see the Hello World text in your Debug window.