The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,152 other subscribers

Archive for the ‘Spring4D’ Category

Delphi MVVM links

Posted by jpluimers on 2021/12/16

After I did conference presentations and wrote a magazine article and in 2013 about Delphi MVVM using DSharp, I saw a 3 articles in 2018, then I went through a big rollercoaster involving rectum cancer treatment.

After recovering, I found that there is one commercially project working on an MVVM framework implementation.

So here are some links on MVVM from new to old:

In my experience experimenting with MVVM and other three- and four-letter frameworks, I found a recurring concept is “convention over configuration”. This relies heavily on having matching names in the various layers.

There you see a big drawback in the Delphi compiler: it lacks a NameOf expression, which means a lot of literal strings ending in your code. Few of the MVVM frameworks have good logging explaining when literals mismatch.

The C# compiler also took a while (until C# version 6 in 2015) to get one, but now supports the [Wayback] nameof expression – C# reference | Microsoft Docs. Some more documentation about this:

Furthermore, the Delphi compiler lacks helpers on interfaces and helpers for generics (heck, I would actually want full extension methods) and generic parameters for methods on interfaces (which is different from the generic parameters on the interface declaration itself). This lack of these features often cause for convoluted syntax to workaround this. Stefan explains the first lacks more clearly in [Wayback] Delphi sorcery: Why no extension methods in Delphi?.

Requests for the nameof expression and interface helpers have been there since at least 2015:

–jeroen

Posted in Delphi, Development, Software Development, Spring4D | Leave a Comment »

Tuple class and records in the Spring4D framework

Posted by jpluimers on 2021/07/01

The Spring Framework for Delphi has some cool tuple support.

The [WayBack] Tuple class has 3 create methods that build generic Tuple<> records with 2, 3 and 4 fields:

–jeroen

Posted in Delphi, Development, Software Development, Spring4D | Leave a Comment »

Delphi TestInsight: when supporting it, test if it is running at all. Same library has a deferer pattern.

Posted by jpluimers on 2021/04/08

Interesting idea by Cesar Romero (who has some interesting repositories at [WayBack] cesarliws (Cesar Romero) · GitHub and [WayBack] cesarliws — Bitbucket) when using TestInsight for Delphi: first test if TestInsight is running at all: [WayBack] foundation-4-delphi/Foundation.Test.Utils.pas at master · cesarliws/foundation-4-delphi · GitHub.

function IsTestInsightRunning: Boolean;
{$IFDEF TESTINSIGHT}
var
  TestInsightClient: ITestInsightClient;
begin
  TestInsightClient := TTestInsightRestClient.Create;
  TestInsightClient.StartedTesting(0);
  Result := not TestInsightClient.HasError;
end;
{$ELSE}
begin
  Result := False;
end;
{$ENDIF}

procedure RunRegisteredTests;
begin
  ReportMemoryLeaksOnShutdown := True;

{$IFDEF TESTINSIGHT}
  if IsTestInsightRunning then
    TestInsight.DUnit.RunRegisteredTests
  else
{$ENDIF}
    DUnitTestRunner.RunRegisteredTests;
end;

Another interesting bit from the same library is the deferer pattern (which is different from the promise pattern!)  in [WayBack] foundation-4-delphi/Foundation.System.pas at master · cesarliws/foundation-4-delphi · GitHub with the below code examples.

I think a better name might be DeferExecutionToEndOfScopeFor.

procedure ProcessFile (const FileName: string);
var
  File: TFile;
begin
  File: = TFile.Open (FileName);
  Defer (File.Close);
  while not File.EOF
  begin
    // ... process file
  end;
end; // Defer will be executed here [File.Close]

procedure ExecSql (const ConnectionString, Sql: string);
var
  Database: TDatabase;
  Exec: IDeferred;
  Query: TQuery;
begin
  Database: = TDatabase.Create (ConnectionString);
  Exec: = Defer (Database.Free);
  Database.Open;
  Exec.Defer (Database.Close);

  Query: = Database.Query (SQL);
  Exec.Defer (Query.Free);
  Exec.Defer (Query.Close);
  if Query.IsEmpty then
   Exit;

  while not Query.EOF
  begin
    // ... process query
  end;

  Exec.Defer (
    procedure
    begin
      Writeln ('Finished ExecSql');
    end
  );
end; // Defer will be executed here [Writeln, Query.Close, Database.Close, Database.Free]

–jeroen

Posted in Delphi, Development, Software Development, Spring4D, TestInsight | Leave a Comment »

Want to use SmartPointers in Delphi? Use the Spring4D one: it is unit tested and optimised

Posted by jpluimers on 2021/01/06

A while ago, there was a nice discussion on smart pointers at [WayBack] Smart Pointers – Generics vrs non-generic implementastion – RTL and Delphi Object Pascal – Delphi-PRAXiS [en].

Conclusion from that:

  • many people think that reference counted interfaces are the same as Smart Pointers
    (basically Smart Pointers are the next level and of course they are based on reference counting)
  • there are a lot of Smart Pointer implementations, but few have a test suite, nor are optimised , nor easy to use
  • The combo Shared/IShared<T>/TShared<T> from Spring4D has all of the above advantages
  • in order to optmise Smart Pointer implementations, you really have to well know the effects of modern Delphi language constructs on the compiler in various target platforms

The discussion mentioned above includes both feature and speed comparisons.

I was a bit amazed that at CodeRage 2018, Marco Cantu introduced yet another smart pointer implementation: one worse than existing implementations, and one with only basic demonstration code, leaving out a test suite.

There have many posts on my blog about smart pointers (see the list below), but Spring4D smart pointer implementation has been around for such a long time that any well respected Delphi developer by now should use them. The source is at  Shared/IShared (search for {$REGION 'Shared smart pointer'} at the current repository).

This list below on my Smart Pointer related blog posts might not be fully complete, but at least mentions that by now you should be using Spring4D.

Some comments on the CodeRage 2018 demos

Read the rest of this entry »

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development, Spring4D | Leave a Comment »

Spring4D Mock – Delphi Unit Testing : Writing a simple spy for the CUT – Stack Overflow

Posted by jpluimers on 2020/10/28

Reminder to self: write a longer article on Delphi mocking as Spring4D mocking is much better than Delphi Mocks, especially because of code-completion reasons.

Spring4D has a Mock record that can return a Mock<T> on which you can verify using methods like Received.

See:

I got some code that I need to dig up from an old project with many more Spring4D Mock examples.

Note that:

For now, these are a start [WayBack] Delphi Unit Testing : Writing a simple spy for the CUT – Stack Overflow:

Sounds like a use case for a mock (I am using the term mock here because most frameworks refer to their various kinds of test doubles as mock)

In the following example I am using DUnit but it should not make any difference for DUnitX. I am also using the mocking feature from Spring4D 1.2 (I did not check if Delphi Mocks supports this)

unit MyClass;

interface

type
  TMyClass = class
  private
    fCounter: Integer;
  protected
    procedure MyProcedure; virtual;
  public
    property Counter: Integer read fCounter;
  end;

implementation

procedure TMyClass.MyProcedure;
begin
  Inc(fCounter);
end;

end.

program Tests;

uses
  TestFramework,
  TestInsight.DUnit,
  Spring.Mocking,
  MyClass in 'MyClass.pas';

type
  TMyClass = class(MyClass.TMyClass)
  public
    // just to make it accessible for the test
    procedure MyProcedure; override;
  end;

  TMyTest = class(TTestCase)
  published
    procedure Test1;
  end;

procedure TMyClass.MyProcedure;
begin
  inherited;
end;

procedure TMyTest.Test1;
var
  // the mock is getting auto initialized on its first use
  // and defaults to TMockBehavior.Dynamic which means it lets all calls happen
  m: Mock<TMyClass>;
  o: TMyClass;
begin
  // set this to true to actually call the "real" method
  m.CallBase := True;
  // do something with o
  o := m;
  o.MyProcedure;

  // check if the expected call actually did happen
  m.Received(Times.Once).MyProcedure;

  // to prove that it actually did call the "real" method
  CheckEquals(1, o.Counter);
end;

begin
  RegisterTest(TMyTest.Suite);
  RunRegisteredTests();
end.

Keep in mind though that this only works for virtual methods.

  • In case you need to test a method from a base class which cannot be affected by the $RTTI directive, you can use a little trick and redefine it in subclass in public section as override; abstract; this will cause the RTTI to be generated. – Honza RFeb 5 ’16 at 8:02

–jeroen

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development, Spring4D | Leave a Comment »

 
%d bloggers like this: