Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3311010
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:49:35+00:00 2026-05-17T21:49:35+00:00

I am trying to define the best way to refactor the project I am

  • 0

I am trying to define the best way to refactor the project I am working on.

Due to lack of good design almost all project is made up of:

1) forms containing business logic

2) huge datamodules (1 per form + some extra ones)

3) some units that contain common code (libraries)

There is no OOP (except for some small areas), code reuse it is at a minimum level.

One problem is also that dataaware controls are used, so it was very simple to drop many datasets+datasources on the datamodules and link directly to the DB in an highly coupled manner.

Ideally i would like to extract classes, like TCustomer, TEmployee, to get advantage os encapsulation and to make it possible to create new UI in the future without duplicating all code.

Anyway my question is: how do I can keep dealing with dataaware controls? Should I implement a function that returns a dataset, and I link the dataawarecomponent.datasource to the function result?

function TCustomer.LoadByID(aCustomerID: integer): TDataset

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:49:36+00:00Added an answer on May 17, 2026 at 9:49 pm

    You are bound to the architecture your application was designed around. Don’t try to fight against it. Let the data aware controls do what they are good at, data synchronization. If your controls are already bound to their data sources using the dfm there shouldn’t be a problem.

    What you do need to refactor is any event handlers you have attached to your controls. I suggest you take a look at the Supervising Controller pattern. I’ve found example implementations for:

    • ASP.NET
    • .NET Rich clients(either winforms or wpf)
    • Smalltalk(this one pioneered the pattern and predates the label of “supervising controller)

    While there are a few examples of UI architectural patterns in Delphi those that are geared toward desktop applications tend to be about the Passive View rather than Supervising Controller. So here is my take on it.

    You’ll want to start with defining at least one interface for each form in your application. I say at least one because some forms are complex and may need to be broken into multiple interfaces.

    IProductView = interface
    end;
    

    Then have your form implement it.

    TProductForm = class(TForm, IProductView)
    ...
    end;
    

    Next you’ll need a presenter/controller. This will handle everything except data synchronization.

    TProductPresenter = class
    private
      FView: IProductView;
    public
      constructor Create(AView:IProductView);
    end;
    

    Create an private field in your form class and create/free the presenter when the form is created/freed. Whether you use the form’s constructor/destructor or the onCreate/onDestroy events doesn’t matter much.

    TProductForm = class(TForm, IProductView)
    private
      FPresenter: TProductPresenter;
    public
      constructor Create;
    ...
    end;
    
    implementation
    TProductForm.Create
    begin
      FPresenter := TProductPresenter.Create(self);
    end;
    

    Now when you need the form or one of its controls to respond to an event delegate responsibility to the presenter. Lets assume you need to check that the product name uses proper capitalization.

    TProductForm.NameDBEditChange(Sender: TObject);
    begin
      FPresenter.ValidateName;
    end;
    

    Rather than pass the control or its text property as an argument you expose the data as a property on the interface…

    IProductView = interface
      function GetName:string;
      procedure SetName(Value: string);
      property Name: string read GetName write SetName;
    

    …and implement GetName and SetName on the form.

    TProductForm.GetName: string;
    begin
      Result := NameDBEdit.Text;
    end;
    
    TProductForm.SetName(Value: string);
    begin
      NameDBEdit.Text := Value;
    end;
    

    Its important to expose the data in the simplest form possible. You don’t want the presenter depending on the product name being stored in a TDBEdit. The presenter should only see what you explicitly allow it to see through the interface. The main benefit of this is you can modify the form as much as you want(or replace it entirely) and as long as it adheres to the interface no changes will need to be made to the presenter.

    Now that all your business logic has been moved to your presenter it will resemble a god class. Your next step will be to refactor that logic into appropriate classes broken up by responsibility. When you reach this point you’re in a much better position to attempt an architectural redesign (if your still considering it).

    “Wow! That looks like a lot of work!” you might say. You’d be right (but then you knew it would be a lot of work before you got started). It doesn’t have to be done all at once. None of these steps is changing the behavior of the logic just where it takes place.

    Advantages

    • UI is now easy to modify
    • Business logic can more easily be tested in isolation
    • Can be implemented incrementally

    Disadvantages

    • It is more work at first though this is eventually offset by more maintainable code later on.
    • Not suitable for all applications. For small projects the additional infrastructure may not be worth the effort.

    Other references

    • A Delphi Implementation of the Model View Presenter Framework (example is custom UI control)
    • TDD’ing the GUI
    • A Simple start with MVP in Delphi for Win32, Part 1, Part2
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to figure out what the best way to define the Created At
I'm trying to find the best way to define models for team / member
I'm trying to figure out the best way to design a rails model. For
I'm trying to figure out there best way to define a global array with
I'm trying to learn the best way to arrange/define code. Taking the below as
I am trying to work out the best way of using a viewmodel in
I'm trying to figure out the best way to share an instance of a
I'm trying to understand the best way to use Spring (for dependency injection) with
I am trying to figure out what is the best way to add a
What is the best way to implement the following? I am trying to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.