Skip to content

The Intwenty DataService

Johan Filipsson edited this page May 14, 2020 · 11 revisions

The Intwenty DataService

The Intwenty.DataService is one of the core somponents of Intwenty. It handles all data I/O for Intwenty Applications. By using the service it's guaranteed that your application can be persisted / retrieved the same way regardless of which supported database you're using.

The Intwenty.DataService implements IIntwentyDataService and you use it by injecting it into the class / controller where you need it.

Example

  public class MyIntwentyController : Controller
  {

         private readonly IIntwentyModelService _modelservice;
         private readonly IIntwentyDataService _dataservice;
         private readonly IntwentySettings _settings;

         public MyIntwentyController(IIntwentyModelService modelservice, 
                                     IIntwentyDataService dataservice, 
                                     IOptions<IntwentySettings> settings)
         {
             _modelservice = modelservice;
             _dataservice = dataservice;
             _settings = settings.Value;
         }

Methods

OperationResult CreateNew(ClientStateInfo state);

Creates JSON for a new application, if default values is defined they are set.

Example of returned JSON

{ ApplicationName:{AplicationId:10, SomeField: DefaultValue}, ApplicationSubTable1:[], ApplicationSubTable2:[] }


OperationResult Save(ClientStateInfo state);

Saves a new or existing Intwenty Application. If state.Id is 0 a new application will be saved otherwise it will be updated. If the operation is carried out with success, a new Id is returned if the application was new. If versioning is enabled for the application a new version id is also returned. (If versioning is not enabled version 1 is always returned)

Example of usage

    [HttpPost("/Application/API/Save")]
    public JsonResult Save([FromBody] System.Text.Json.JsonElement model)
    {
  
        var state = ClientStateInfo.CreateFromJSON(model);
        var res = DataRepository.Save(state);
        return new JsonResult(res);

    }

OperationResult DeleteById(ClientStateInfo state);

Deletes all application data (maintable and subtables) by id. If the application uses versioning, all versions are deleted.


OperationResult DeleteById(int applicationid, int id, string dbname);

  • Deletes data by Id
  • Parameter Id can be an Id of an application subtable row, or an application maintable Id
  • Parameter dbname can be an application subtable name or main tablename
  • If the dbname represents a main application table, all application data (maintable and subtables) is deleted.
  • If the dbname represents an application subtable, only the subtable row that matches the id parameter is deleted.
  • If the application uses versioning, all versions are deleted.

OperationResult GetLatestVersionById(ClientStateInfo state);

Get the latest version data for and application based on Id.

Example of usage

      var state = new ClientStateInfo() { Id = 1782, ApplicationId = 10 };
      var data = DataRepository.GetLatestVersionById(state);
      return new JsonResult(data);

Example of result

{"Customer: {"Id":1782,"Version":1,"OwnerId":0,"ApplicationId":10,"CreatedBy":"SYSTEM","ChangedBy":"SYSTEM","OwnedBy":"SYSTEM","CustomerId":"CUST1900","CustomerName":"John Philips","Address":"Farmersroad 15","Email":"[email protected]","Phone":"\u002B4577777777","Country":"Denmark","ZipCode":"777 88"}}}


OperationResult GetLatestVersionByOwnerUser(ClientStateInfo state);

Get the latest version data for and application based on Id.

Example of usage

      var state = new ClientStateInfo() { ApplicationId = 10, OwnerUserId="[email protected]" };
      var data = DataRepository.GetLatestVersionById(state);
      return new JsonResult(data);

Example of result

{"Customer: {"Id":2782,"Version":1,"OwnerId":0,"ApplicationId":10,"CreatedBy":"SYSTEM","ChangedBy":"SYSTEM","[email protected]":"SYSTEM","CustomerId":"CUST1900","CustomerName":"John Richards","Address":"Farmersroad 16","Email":"[email protected]","Phone":"\u002B4577777777","Country":"Sweden","ZipCode":"777 88"}}}


OperationResult GetList(ListRetrivalArgs args);

  • Get a list of (latest version) application data that matches the filter specified in args.
  • If there's a LISTVIEW defined for the application, the columns in the list view is returned otherwise all columns.
  • This function supports paging. It returns the number of records specified in args.BatchSize
  • Returns an OperationResult including a json array and the current paging rownum

OperationResult GetListByOwnerUser(ListRetrivalArgs args);

  • Get a list of (latest version) application data, based on OwnedBy and that matches the filter specified in args.
  • If there's a LISTVIEW defined for the application, the columns in the list view is returned otherwise all columns.
  • This function supports paging. It returns the number of records specified in BatchSize
  • Returns an OperationResult including a json array and the current paging rownum

OperationResult GetList(int applicationid);

  • Get a list of (latest version) application data.
  • All columns from the application's main table is returned.
  • Returns an OperationResult including a json array

OperationResult GetList(int applicationid);

  • Get a list of (latest version) application data based on OwnedBy.
  • All columns from the application's main table is returned.
  • Returns an OperationResult including a json array

OperationResult GetVersionListById(ClientStateInfo state);


OperationResult GetVersion(ClientStateInfo state);


OperationResult GetValueDomains(int ApplicationId);

Get value domains used by UI in the application specified by application id

  • Returns an OperationResult including a json array

OperationResult GetValueDomains();

Get all value domains used by UI in the application specified by application id

  • Returns an OperationResult including a json array

OperationResult GetDataView(ListRetrivalArgs args);

Gets a list of data based on the DataView defined by args.DataViewMetaCode and that matches the filter specified in args.

  • Returns an OperationResult including a json array

OperationResult GetDataViewRecord(ListRetrivalArgs args);

Gets the first record of data based on the DataView defined by args.DataViewMetaCode and that matches the filter specified in args.

  • Returns an OperationResult including a json array

OperationResult Validate(ApplicationModel model, ClientStateInfo state);


void LogError(string message, int applicationid=0, string appmetacode="NONE", string username="");


void LogWarning(string message, int applicationid = 0, string appmetacode = "NONE", string username = "");


void LogInfo(string message, int applicationid = 0, string appmetacode = "NONE", string username = "");


IIntwentyDbORM GetDbObjectMapper();

Get an object mapper that performs ORM operations regardless of the database you're using.

Example of usage

 [DbTablePrimaryKey("Id")]
 [DbTableName("myTableName")]
 public class CustomObject{

       [BsonId]
       [AutoIncrement]
       public int Id { get; set; }

        public string MyProperty { get; set; }

  }

  IIntwentyDbORM dbstore = _dataservice.GetDbObjectMapper();

  dbstore.CreateTable<CustomObject>();

  var t = new CustomObject() { MyProperty = "TEST" };

  dbstore.Insert(t);