Quantcast
Channel: TMS Software
Viewing all articles
Browse latest Browse all 1008

TMS XData is coming

$
0
0

A new framework named TMS XData is cooking in the TMS labs. It's hard to define what a framework is with a few words, especially if such framework is modular and many pieces of it can be used separately. Nevertheless, using a very broad definition I could say that TMS XData is a framework that allows you to provide and consume application data through Rest services. But maybe instead trying to define it, I should give you a small sample.

XData can be strongly integrated with TMS Aurelius, our ORM framework. I have posted some articles here about Aurelius and some very simple samples, so in case you still don't know about it, you can check how to getting started or, related to this article, how to create simple associations with it.

Now let's build a very simple XData server that serves Aurelius objects:

program SimpleXDataServer;

{$APPTYPE CONSOLE}

uses
  Aurelius.Drivers.Interfaces,
  Aurelius.Engine.ObjectManager,
  XData.Http.Server,
  XData.Aurelius.EntityServer,
  Utils,
  Customer;

procedure CreateCustomer(const CustomerName, CountryName: string);
var
  Manager: TObjectManager;
  Customer: TCustomer;
  Country: TCountry;
begin
  Manager := TObjectManager.Create(SQLiteConnection);
  Country := TCountry.Create;
  Country.Name := CountryName;
  Customer := TCustomer.Create;
  Customer.Name := CustomerName;
  Customer.Country := Country;
  Manager.Save(Customer);
  Manager.Free;
end;

var
  Conn: IDBConnection;
  Server: THttpServer;
begin
  ReportMemoryLeaksOnShutdown := true;
  Conn := SQLiteConnection;
  BuildDatabase(Conn);
  CreateCustomer('Paul', 'United States');
  CreateCustomer('Jonas', 'Germany');
  CreateCustomer('Hiroto', 'Japan');
  CreateCustomer('Jian', 'China');
  CreateCustomer('Sergei', 'Russia');

  Server := THttpServer.Create;
  Server.RegisterHandler(
    TAureliusEntityServer.Create(
      'http://aurelius:2001/tms/xdata', SQLiteConnection));
  Server.Start;

  WriteLn('Running Simple XData Server');
  ReadLn;
  Server.Free;
  DestroyDatabase(Conn);
end.
The code above uses the classes TCustomer and TCountry defined in the blog post "Associations (foreign key)". They are simple classes and I'm not showing them again here for simplicity. Also, be aware that try..finally blocks were removed also to make code cleaner.

The above application is a regular TMS Aurelius application, that connects to a database, creates needed tables and insert some data. The different part is the one that creates a THttpServer instance and register a request handler under the address "/tms/xdata". What happens here is that Aurelius objects are now being provided through a very well defined Rest API.

XData uses by default the OData protocol, which makes it very compatible with other clients and platforms. It also makes everything automatic for XData: your TCustomer objects, for example, are available at the address "http://aurelius:2001/tms/xdata/TCustomer" (Naming can be configured as well).

Another interesting thing is how this data is easily accessible from everywhere. As an example, here is the full source code of a .NET C# console application that connects to such server and retrieves the customer objects from it, using Linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;

namespace DotNetClient
{
    class Program
    {
        static void Main(string[] args)
        {
            DataServiceContext context = new DataServiceContext(new Uri(@"http://aurelius:2001/tms/xdata"));
            IQueryable<TCustomer> customers = context.CreateQuery<TCustomer>("TCustomer");
            foreach (TCustomer customer in customers) 
                Console.WriteLine("Customer {0} from {1}", customer.FName, customer.FCountry.FName);
            Console.ReadLine();
        }
    }

    public class TCustomer
    {
        public int FId { get; set; }
        public string FName { get; set; }
        public TCountry FCountry { get; set; }
    }

    public class TCountry
    {
        public int FId { get; set; }
        public string FName { get; set; }
    }
}
And here is the output generated by this C# application.



More info coming soon!




Viewing all articles
Browse latest Browse all 1008

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>