Framework Demo

This demo, based on SqlServer, is meant to show the versatility of the WYSE framework and the freedom that goes wit it. Combining .NET with SQL to create something that is more and better than the sum of the two parts. The following are examples of how better database interaction could take shape, but should not be considered as 'this is how you should do it'. WYSE is built to offer the freedom to do it the way you want. The idea is to be able to code without having to make code quality damaging compromises.

...and since code speaks louder than words demo code (stack trace) and database results are shown in a developer-tools-like panel as you interact with the server.

Advanced Language Features

The framework supports several ways of building queries and also facilitates dynamic SQL statements decoration. The latter enables to statements to be modified, which in turn offers the possibility to neatly encapsulate query building code centrally.

Building on the previous example featuring custom Selects... The following examples feature a shared/generic repository base that implements basic CRUD operations.

This repository base uses a centralized custom QueryBackbone that can decorate a query in a way that paging is added.

The small window you are using greatly diminishes your demo experience. Please consider viewing using a larger window.

Behind The Scenes Explanation

This is where you get to see what's happening behind the scenes when you interact with the site on the left side.

The view consists of three (collapsible) parts...

1. Full Code, Queries, And Results

The server handles the web requests that are the result of you interacting with the site.
In doing so, the demo backend code is executed which in turn uses WYSE.

The relevant/notable methods taking part in that process are shown in the first vertical tab.
Basically you are seeing a (partial) stack trace.
It is tree structure of which the nodes are collapsible/expandable for your browsing convenience.

The leaves of the STACK TRACE TREE are where interaction with the database takes place.
That is generally where you will find the generated SQL statements and the JSON of the converted database results.

2. Queries And Results Only

A filtered list of entries where only the database queries and results are shown.

3. API Results

If applicable: this shows the JSON of the call to the server as if it had been an API call.

  • Trace Entries: 6
      ...Framework.Controllers.HomeController.GetAddress:
      [HttpGet]
      public async Task<ViewResult> GetAddress(Guid id)
      {
          var traceTree = _traceTreesRepository.Record();
          traceTree.RecordCodeMethodExecution(MethodBase.GetCurrentMethod());
      
          var result = await _addressService.GetAddressResult(id, traceTree);
      
          return CreateView(traceTree, result);
      }
    • Trace Entries: 1
        ...Framework.Services.AddressService.GetAddressResult:
        public async Task<Address> GetAddressResult(Guid id, TraceTree parentTraceTree)
        {
            var traceTree = parentTraceTree.AddChild();
            traceTree.RecordCodeMethodExecution(MethodBase.GetCurrentMethod());
        
            return await _addressRepository.GetById(id, parentTraceTree);
        }
    • Trace Entries: 1
        ...Framework.Repositories.RepositoryBase.GetById:
        public async Task<TItem> GetById(Guid entityId, TraceTree parentTraceTree)
        {
            var traceTree = parentTraceTree.AddChild();
            traceTree.RecordCodeMethodExecution(MethodBase.GetCurrentMethod());
        
            /*
             * _createSelectById gets passed on, by for example the AddressRepository
             * (that inherits from RepositoryBase<AddressTable, AddressSubselect, Address>),
             * like so: (parentTraceTree, id) => new AddressSubselect(parentTraceTree, id: id))
             */
            TSelect selectById = _createSelectById(parentTraceTree, entityId);
        
            return await GetSingle(selectById, parentTraceTree);
        }
    • Trace Entries: 1
        ...Framework.DataSpace.Queries.ItemSubselectBase ctor:
        /*
         * ItemSubselectBase is the Framework Demo base class for all subselects used in
         * repositories and other dynamic query building.
         * This class inherits from WYSE's convertable subselect that enables query result row 
         * conversion (extending DbDataReader) and (Hierarchical) Object Relational Mapping.
         * Usage examples (different overloads):
         * - var select = new AddressSelect()  // Gets all addresses
         * - var select = new AddressSelect(id: new Guid("11111111-1111-1111-1111-111111111111"))  // Get a single address by id
         * - var select = new AddressSelect(isDeleted: true)  // Only gets the deleted addresses
         * - var select = new AddressSelect(new PagingSettings { PageNumber = 3, PageSize = 20 })
         * So a Select with WYSE can be used to encapsulate SQL logic,
         * making it kind of like a coded version of a SqlServer function or view,
         * but much more flexible!
         */
        protected ItemSubselectBase(
            ItemTableBase<TItem> sourceTable,
            TraceTree parentTraceTree,
            PagingSettings? pagingSettings,  /* If not null then paging is added to the query */
            Guid? id,  /* If not null then a select by id is done */
            bool? isDeleted)  /* If 'null' then both deleted and not deleted */
        {
            var traceTree = parentTraceTree?.AddChild();
            traceTree?.RecordCodeConstructorExecution(typeof(ItemSubselectBase<TSelect, TItem>));
        
            _sourceListConvert = sourceTable.Convert;
        
            // Funneling is a concept used in WYSE to expose the columns of a (nested)
            // subselect one level higher in a query, so it can be used in a From or in
            // Where/Join expressions for example.
            Id = /*WYSE*/Funnel(sourceTable.Id);
            Created = /*WYSE*/Funnel(sourceTable.Created);
            LastModified = /*WYSE*/Funnel(sourceTable.LastModified);
            IsDeleted = /*WYSE*/Funnel(sourceTable.IsDeleted);
        
            var columnExpressions = new List</*WYSE*/IExpression>(
                sourceTable./*WYSE*/ListColumns());
        
            if (pagingSettings is not null)
            {
                // Add SqlServer's 'COUNT(Id) OVER()' to selected columns.
                // The modifying of the Where clause for row selection is done in
                // the query backbone. Check the traces below.
                sourceTable.CountUnpaged = new /*WYSE*/Count(sourceTable.Id).Over();
                CountUnpaged = /*WYSE*/Funnel(sourceTable.CountUnpaged);
        
                columnExpressions.Add(sourceTable.CountUnpaged);
            }
        
            // This makes the statement: SELECT [columnExpressions] FROM [sourceList]
            /*WYSE*/From from = this./*WYSE*/Columns(columnExpressions).From(sourceTable);
        
            // Determine the applicable where clause sub expressions (based on SQL parameters)
            var whereExpressions = new List</*WYSE*/BooleanExpression>();
        
            if (id.HasValue)
            {
                whereExpressions.Add(
                    sourceTable.Id == new /*WYSE*/UniqueIdentifierParam("Id", id));
            }
            if (isDeleted.HasValue)
            {
                whereExpressions.Add(
                    sourceTable.IsDeleted == new /*WYSE*/BitParam("IsDeleted", isDeleted));
            }
        
            if (whereExpressions.Count > 0)
            {
                // Append a Where clause with a condition that is all the
                // sub expressions 'AND'-ed together
                from.Where(whereExpressions./*WYSE*/And());
            }
        }
    • Trace Entries: 1
        ...Framework.DataSpace.Queries.Customers.AddressSubselect ctor:
        public AddressSubselect(
            AddressTable addressTable,
            TraceTree parentTraceTree,
            PagingSettings? pagingSettings = null,
            Guid? id = null,
            bool? isDeleted = false)
            : base(addressTable ?? throw new ArgumentNullException(nameof(addressTable)),
                  parentTraceTree,
                  pagingSettings,
                  id,
                  isDeleted)
        {
            var traceTree = parentTraceTree?.AddChild();
            traceTree?.RecordCodeConstructorExecution(typeof(AddressSubselect));
        
            Town = Funnel(addressTable.Town);
        }
    • Trace Entries: 6
        ...Framework.Repositories.RepositoryBase.GetSingle:
        private async Task<TItem> GetSingle(TSelect select, TraceTree parentTraceTree)
        {
            var traceTree = parentTraceTree.AddChild();
            traceTree.RecordCodeMethodExecution(MethodBase.GetCurrentMethod());
        
            // A scriptable can be anything in the WYSE language that can be part of a script:
            // - Executables; like Selects, Inserts, Updates, Deletes, Merges (SqlServer only)
            // - Variable and Temp table declarations
            // - If/Elses
            // ...and more.
            var scriptables = new List</*WYSE*/IScriptable>();
        
            // The Framework has one centralized place where the select statements are
            // dynamically decorated to apply business rules, encapsulated.
            // In some cases this means that select becomes a grander script with inital SQL
            // 'Scriptables', prefixing the initial Select statement.
            _queryBackbone.Using(traceTree).Modify(select, scriptables);
        
            // Create a script containing the scriptables (if any) plus the modified Select
            return await _sqlExecuter.GetScriptItemAsync(
                // Merge Script with the Select into total Script
                new /*WYSE*/Script(scriptables) + select,
                select.SourceListConvert,
                traceTree);
        }
      • Trace Entries: 1
          ...Framework.DataSpace.Queries.Modification.QueryBackbone.Modify:
          public override void Modify(
              /*WYSE*/Select select,
              List</*WYSE*/IScriptable> preparationScriptables)
          {
              var traceTree = _parentTraceTree.AddChild();
              traceTree.RecordCodeMethodExecution(MethodBase.GetCurrentMethod());
          
              // You can build a pipleline of modifiers to modify an executable (here a Select),
              // and add scriptables (like table variables) used in the modified Select.
              // These modifiers let you cut/paste strongly typed SQL language snippets.
              // In the case of this Framework Demo there are 3 custom modifiers:
              // - Division security decorator
              // - Paging
              // - Identifiers resetter
              // ...more on those below.
              base.Modify(select, preparationScriptables);
          }
      • Trace Entries: 1
          ...Framework.DataSpace.Queries.Modification.DivisionSecurityDecorator.Modify:
          public void Modify(/*WYSE*/Select select, List</*WYSE*/IScriptable> scriptables)
          {
              var traceTree = _parentTraceTree.AddChild();
              traceTree.RecordCodeMethodExecution(MethodBase.GetCurrentMethod());
          
              if (!select.Uses<IDivisionReferringTable>())
              {
                  return;
              }
          
              // Put all the Ids of the divisions the current user has rights to in a 
              // TableVariable (Inherits from WYSE SqlServer TableVariable).
              var userDivisionIdsTableVariable = new UserDivisionIdsTableVariable();
          
              scriptables.Add(new /*WYSE*/Declare(userDivisionIdsTableVariable));
          
              var fillTableVariable = new UserDivisionIdsTableVariable.Fill(
                  userDivisionIdsTableVariable,
                  _sessionContext.CurrentUser.DivisionId,
                  traceTree);
          
              scriptables.Add(fillTableVariable);
          
              // This is a WYSE ISnippetReplacementFactory
              var securityReplacement = new SecurityReplacement(
                  userDivisionIdsTableVariable,
                  traceTree);
          
              // SQL Statements can be remade by chaining multiple ISnippetReplacementFactory instances.
              // Here just one is needed.
              _ = /*WYSE*/StatementRebuilder.Rebuild(
                  select,
                  /* rebuild the original Select, instead of a clone of it that is returned */
                  rebuildOnClone: false,
                  [securityReplacement]);
          }
      • Trace Entries: 1
          ...Framework.DataSpace.Queries.Modification.PagingDecorator.Modify:
          public void Modify(/*WYSE*/Select select, List</*WYSE*/IScriptable> preparationScriptables)
          {
              var traceTree = _parentTraceTree.AddChild();
              traceTree.RecordCodeMethodExecution(MethodBase.GetCurrentMethod());
              
              if (_pagingSettings is null)
              {
                  return;
              }
          
              // SQL Statements can be remade by chaining multiple ISnippetReplacementFactory instances.
              // Here just one is needed.
              _ = /*WYSE*/StatementRebuilder.Rebuild(
                  select,
                  /* rebuild the original Select, instead of a clone of it that is returned */
                  rebuildOnClone: false,
                  [new PagingAppending(select, _pagingSettings, traceTree)]);
          }
      • Trace Entries: 1
          ...Framework.DataSpace.Access.SqlExecuter.GetScriptItemAsync:
          public async Task<TItem> GetScriptItemAsync<TItem>(
              /*WYSE*/IScript script,
              Converter<DbDataReader, TItem> converter,
              TraceTree parentTraceTree)
          {
              var traceTree = parentTraceTree.AddChild();
              traceTree.RecordCodeMethodExecution(MethodBase.GetCurrentMethod());
          
              return await Get(
                  script,
                  reader => reader./*WYSE*/GetItem(converter),
                  parentTraceTree);
          }
      • Trace Entries: 2
          ...Framework.DataSpace.Access.SqlExecuter.Get:
          private async Task<TItem> Get<TItem>(
              /*WYSE*/IExecutable executable,  /* A Select or a Script */
              Func<DbDataReader, TItem> getResult,
              TraceTree parentTraceTree)
          {
              var traceTree = parentTraceTree.AddChild();
              traceTree.RecordCodeMethodExecution(MethodBase.GetCurrentMethod());
          
              using SqlConnection connection = _connectionFactory.Create();
              connection.Open();
          
              // Aliases and parameter names can be given custom values.
              // If omitted, unique identifer values are supplied
              // In the case of this Demo, these generated values are overwritten,
              // based on the current render settings, with more legible values.
              // WYSE supports several kinds of Resetting/renaming.
              ResetIdentifiers(executable);
          
              using SqlCommand command = connection.CreateCommand()
                  // A WYSE Render context determines certain (overridable) render settings.
                  // Here the context is SqlServer. More SQL flavors are supported.
                  // A WYSE render builder is used for stringifying
                  // the strongly typed SQL statements.
                  /*WYSE*/.For(executable, _renderContextFactory.Create(), _createRenderBuilder);
          
              try
              {
                  using var reader = await command.ExecuteReaderAsync();
          
                  var result = getResult(reader);
          
                  traceTree.RecordDbCommandExecutionSuccess(
                      command.CommandText,
                      command.Parameters.ToDictionary(),
                      result);
          
                  return result;
              }
              catch (Exception exception)
              {
                  traceTree.RecordDbCommandExecutionFailure(
                      command.CommandText,
                      command.Parameters.ToDictionary(),
                      exception);
          
                  return default;
              }
          }

          Command Parameters:

          @Id : 33333333-3333-3333-3333-333333333333
          @IsDeleted : False

          Command Text:

          SELECT
              [56154_FrameworkDemoCustomers_demo_Address].[Town] [56154_FrameworkDemoCustomers_demo_Address_Town],
              [56154_FrameworkDemoCustomers_demo_Address].[Id] [56154_FrameworkDemoCustomers_demo_Address_Id],
              [56154_FrameworkDemoCustomers_demo_Address].[Created] [56154_FrameworkDemoCustomers_demo_Address_Created],
              [56154_FrameworkDemoCustomers_demo_Address].[LastModified] [56154_FrameworkDemoCustomers_demo_Address_LastModified],
              [56154_FrameworkDemoCustomers_demo_Address].[IsDeleted] [56154_FrameworkDemoCustomers_demo_Address_IsDeleted]
          FROM [56154_FrameworkDemoCustomers].[demo].[Address] [56154_FrameworkDemoCustomers_demo_Address]
          WHERE
          (
              ([56154_FrameworkDemoCustomers_demo_Address].[Id] = @Id)
              AND
              ([56154_FrameworkDemoCustomers_demo_Address].[IsDeleted] = @IsDeleted)
          );

Command Parameters:

@Id : 33333333-3333-3333-3333-333333333333
@IsDeleted : False

Command Text:

SELECT
    [56154_FrameworkDemoCustomers_demo_Address].[Town] [56154_FrameworkDemoCustomers_demo_Address_Town],
    [56154_FrameworkDemoCustomers_demo_Address].[Id] [56154_FrameworkDemoCustomers_demo_Address_Id],
    [56154_FrameworkDemoCustomers_demo_Address].[Created] [56154_FrameworkDemoCustomers_demo_Address_Created],
    [56154_FrameworkDemoCustomers_demo_Address].[LastModified] [56154_FrameworkDemoCustomers_demo_Address_LastModified],
    [56154_FrameworkDemoCustomers_demo_Address].[IsDeleted] [56154_FrameworkDemoCustomers_demo_Address_IsDeleted]
FROM [56154_FrameworkDemoCustomers].[demo].[Address] [56154_FrameworkDemoCustomers_demo_Address]
WHERE
(
    ([56154_FrameworkDemoCustomers_demo_Address].[Id] = @Id)
    AND
    ([56154_FrameworkDemoCustomers_demo_Address].[IsDeleted] = @IsDeleted)
);