lundi 31 décembre 2018

How can i reduce cyclomatic complexity from a model

i need to reduce cyclomatic complexity of this model because it has 26, this is a normal class for setters and getters

public class DetailRecord {
private int lengthReference1;
private int lengthReference2;
private int lengthPayersNit;
private int lengthTransactionAmount;
private String recordType;
private String payersNit;
private String payersName;
private String payersAccountBank;
private String accountNumberToBeDebited;
private String transactionType;
private String transactionAmount;
private String referenceOne;
private String referenceTwo;
private String expirationDateOrApplicationDate;
private String billedPeriods;
private String cycle;
private String reserved;
private String validationNit;
private String encabezadoTotal;

public DetailRecord() {
    lengthReference1 = 30;
    lengthReference2 = 30;
    lengthPayersNit = 13;
    lengthTransactionAmount = 17;
    recordType = "6";
    payersName = "                    ";


}

public int getLengthReference1() {
    return lengthReference1;
}

public int getLengthReference2() {
    return lengthReference2;
}

public int getLengthPayersNit() {
    return lengthPayersNit;
}

public int getLengthTransactionAmount() {
    return lengthTransactionAmount;
}


public String getRecordType() {
    return recordType;
}

public String getPayersName() {
    return payersName;
}

public String getPayersAccountBank() {
    return payersAccountBank;
}

how can i reduce the cyclomatic complexity? using a builder perhaps? or what can i do? abstract the class or maybe create a interface?

Design pattern for repeating a set of methods multiple times

Imagine we're writing a spreadsheet validation function. The user can enter multiple values in the spreadsheet, and there is a method that will verify if the values are correct. In addition to verifying if they're correct, there is also a "fix it for me" dialog that pops up and asks the user if they want to fix the problem automatically.

For example purposes, let's say we have the following fields:

  • Event url: The link to an event.
  • Event title: the name of the calendar event.
  • Invitees: a list of comma separated email addresses of users that should be invited to the event.

The user can then hit a "validate" button that will check the following:

  • That the Event title really matches the one in the URL. If it doesn't they are presented with an option to update the title.
  • That the invitees are all on the event. If they aren't, an option to invite the next one is presented to the user (this is only done once at a time).

What's a good programming design pattern to execute a set of functions over and over again?

function validateSpreadsheet() {
  validateEventTitle();
  validateInvitees();
}

Both validateEventTitle and validateInvitees should return one of 3 possible values:

  • Success
  • Retry (the user chose to use the "fix it for me" button.)
  • Error (the user didn't choose the "fix it" feature.)

If one of them returns Retry, the entire method validateSpreadsheet should be run (e.g. in case we decide to have the event title depend on the number of invitees).

I can think of several ways the function validateSpreadsheet could repeat its logic:

  • (A) While loop
  • (B) Recursion
  • (C) Array of functions

I can think of several ways the function validateEventTitle can report its status:

  • (1) it could return an enum with the 3 values (success, retry, error)
  • (2) it could raise an exception in the case of retry and/or error

I implemented pseudocode for solution C1 (see the end of the post), but C1 makes it hard to share code between the different methods. For example, if the meat of the code looked something like this:

function validateSpreadsheet() {
  var row = getRow();
  var title = getEventTitle(row);
  validateEventTitle(title, row);
  validateInvitees(row);
}

... that would be more difficult to get working with C1 since the methods are wrapped in functions. I realize there are ways to workaround this limitation.

I don't like solution B1, but for completeness sake, I included a version of it below too. I don't like that it uses the call stack for repetition. I also think the code is pretty messy with the double if checks. I realize I could create helper methods to make it a single if check for each method, but that's still pretty messy.

I implemented a working example of solution A2. This one seems to work well, but it heavily exploits exceptions in a way that would probably confuse a new programmer. The control flow is not easy to follow.

Is there already a design pattern to achieve something like this? I'd like to use that rather than reinventing the wheel.


Solution C1 (Pseudocode)

function solutionC1() {
  var functions = [ 
    method1, 
    method2 
  ];

  while (true) {
    var result = SUCCESS;

    for (var f in functions) {
      result = f();
      if (result === SUCCESS) {
        continue;  
      } else if (result === REPEAT) {
        break;
      } else {
        return result; // ERROR
      }
    }

    if (result === REPEAT) {
      continue;
    } else {
      return; // SUCCESS
    }
  }
}

Solution B1 (Pseudocode)

function solutionB1() {
  var result;

  result = method1();
  if (result === RETRY) {
    return solutionB1();
  } else if (isError(result)) {
    return result;
  }

  result = method2();
  if (result === RETRY) {
    return solutionB1();
  } else if (isError(result)) {
    return result;
  }
}

Solution A2 (Working with unit tests)

function solutionA2() {
  while (true) {
    try {
      // these two lines could be extracted into their own method to hide the looping mechanism
      method1();
      method2();
    } catch(error) {
      if (error == REPEAT) {
        continue;
      } else {
        return error;
      }
    }
    break;
  }
}

var REPEAT = "REPEAT";
var method1Exceptions = [];
var method2Exceptions = [];
var results = [];

function unitTests() {
  // no errors
  method1Exceptions = [];
  method2Exceptions = [];
  results = [];
  solutionA2();
  if (results.join(" ") !== "m1 m2") { throw "assertionFailure"; }
  
  // method1 error
  method1Exceptions = ["a"];
  method2Exceptions = ["b"];
  results = [];
  solutionA2();
  if (results.join(" ") !== "m1:a") { throw "assertionFailure"; }
  
  // method1 repeat with error
  method1Exceptions = [REPEAT, "a"];
  method2Exceptions = ["b"];
  results = [];
  solutionA2();
  if (results.join(" ") !== "m1:REPEAT m1:a") { throw "assertionFailure"; }

  // method1 multiple repeat
  method1Exceptions = [REPEAT, REPEAT, REPEAT, "a"];
  method2Exceptions = ["b"];
  results = [];
  solutionA2();
  if (results.join(" ") !== "m1:REPEAT m1:REPEAT m1:REPEAT m1:a") { throw "assertionFailure"; }
  
  // method1 multiple repeat, method2 repeat with errors
  method1Exceptions = [REPEAT, REPEAT, REPEAT];
  method2Exceptions = [REPEAT, REPEAT, "b"];
  results = [];
  solutionA2();
  if (results.join(" ") !== "m1:REPEAT m1:REPEAT m1:REPEAT m1 m2:REPEAT m1 m2:REPEAT m1 m2:b") { throw "assertionFailure"; }

  // method1 multiple repeat, method2 repeat with no errors
  method1Exceptions = [REPEAT, REPEAT, REPEAT];
  method2Exceptions = [REPEAT, REPEAT];
  results = [];
  solutionA2();
  if (results.join(" ") !== "m1:REPEAT m1:REPEAT m1:REPEAT m1 m2:REPEAT m1 m2:REPEAT m1 m2") { throw "assertionFailure"; }
  
   // [REPEAT, "Test"];
}

function method1() {
  // in reality, this method would do something useful, and return either success, retry, or an exception. To simulate that for unit testing, we use an array.
  var exception = method1Exceptions.shift();
  if (typeof exception !== "undefined") {
    results.push("m1:" + exception);
    throw exception;
  } else {
    results.push("m1");
  }
}

function method2() {
  // in reality, this method would do something useful, and return either success, retry, or an exception. To simulate that for unit testing, we use an array.
  var exception = method2Exceptions.shift();
  if (typeof exception !== "undefined") {
    results.push("m2:" + exception);
    throw exception;
  } else {
    results.push("m2");
  }
}

unitTests();

How to create relationship between objects based on some mapping

I have some object stored as json which have unique key.. And based on these keyes there is a mapping defined in an excel. I need to show some relationship like hierarchy in java class between those json objects based on that mapping.. I don't want to manually create the hierarchy on db for the json based on the ids. But I want to keep the objects separately and based on the mapping I want to create the hierarchy at runtime.. I want to know some best ways or formats that I can keep this mapping . Please guide me .

which design pattern to reduce a collection of entities?

I need to remove some entities from a collection of entities, to do so, I call 4 services one after the other to remove the entites who don't match some filters.

class MainService()
{
    public function removeEntities($collection)
    {
        $collection = $this->subServiceA->removeEntities($collection);
        $collection = $this->subServiceB->removeEntities($collection);
        $collection = $this->subServiceC->removeEntities($collection);
        $collection = $this->subServiceD->removeEntities($collection);

        return $collection;
    }
}

I think that decorator, chain of responsibility or observer can handle this case but I don't know which one suit it the best.

Extracting words with - included upper lowercase not working for words it only extracts chars

I'm trying to extract several words from a string with regex matcher &pattern. I did spend some time to make the regular expression I'm using but this doesn't work as expected, any help would be very appreciated.

I made the regular expression I'm using but this doesn't work as expected, some help would be great. I'm able to extract the chars from the words I want but not the entire word.

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Main {

public static void main (String[] args){

    String mebo = "1323 99BIMCP 1 2 BMWQ-CSPS-D1, 0192, '29229'";


    Pattern pattern = Pattern.compile("[((a-zA-Z1-9-0)/W)]");

    Matcher matcher = pattern.matcher(mebo);


    while (matcher.find()) {

        System.out.printf("Word is %s %n",matcher.group(0));

    }

}
}

This is current output:

Word is 1 Word is 3 Word is 2 Word is 3 Word is 9 Word is 9 Word is B Word is I Word is M Word is C Word is P Word is 1 Word is 2 Word is B Word is M Word is W Word is Q Word is - Word is C Word is S Word is P Word is S Word is - Word is D Word is 1 Word is 0 Word is 1 Word is 9 Word is 2 Word is 2 Word is 9 Word is 2 Word is 2 Word is 9

============

My expectation is to iterate entire words for example:

String mebo = "1323 99BIMCP 1 2 BMWQ-CSPS-D1, 0192, '29229'"

word is 1323 word is 99BIMCP word is 1 word is 2 word is BMWQ-CSPS-D1 word is 0192 word is 29229

criteria to noted before develop a web application

i have researched before asking here a question about this topic so i hope you'll read question. let suppose i am developing an dating app where user can came register and talk with others if they accepts the request.

so before ask i learnt about

  • Sketching App
  • Planning App's UI Flow
  • Database Normalization

But after all this i wants to know that should i have to start from developing an Admin Panel or the user's front view.

And please you can suggest if any other things to note about.

Not sure where to place my Models/Entities in the project

I have been reading tons of threads and still not sure i get it. First of all i want to verify that i get the basics "correct" or atleast it's an ok way to start a smaller project.

  • No Unit test will be written
  • No complext models will exists (Just alot of CRUD operations)
  • No business rules (I think)

I will start with the following structure in my solution:

Web.Domain.entities

I using ValidationAttributes on my properties, and i should belong to my ViewModel instead, but don't think i need a ViewModel, or don't really see the need of one (I just have to mapp everything twice?)

namespace Web.Domain.Entities
{ 
    public class Car
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "RegNumber missing.")]
        public string RegNumber { get; set; }

        public Car() { }

        public Car(IDataReader reader)
        {
            Id = Helper.GetSafeInt(reader, "Id");
            RegNumber = Helper.GetSafeString(reader, "RegNumber");
        }
    }
}

Project structure

Web.MVC - Basic folder for my MVC application.

Web.MVC.Views - Handling presentation of my Entities (Not have ViewModels)

Web.MVC.ViewModels - Not used, but i used it is this the correct place to store it?

Web.DAL - Static classes with static methods, CRUD operations.

Web.DAL.CarDAL - Static class, don't want to create a new instance every time, cleaner code.

Web.DAL.CarDAL.Get() - Static method, CRUD operations.

My Controller looks like this,

        public ActionResult Get()
        {
            var data = Web.DAL.CarDAL.Get();
            return View(data);
       }

As i looks like right now i need to reference my Web.Domain.entities from both Web.MVC.Controller and Web.DAL.CarDAL.

If i want to create some business logic, some recommend it to live within the Entities, is this a good place?

namespace Web.Domain.Entities
{
public class Car
{
    public int Id { get; set; }

    [Required(ErrorMessage = "RegNumber missing.")]
    public string RegNumber { get; set; }

    public Car() { }

    public Car(IDataReader reader)
    {
        Id = Helper.GetSafeInt(reader, "Id");
        RegNumber = Helper.GetSafeString(reader, "RegNumber");
    }

    public void BusinessRule1(string regNumber)
    {
        throw new Exception("BusinessRule1 failed.");
    }
}
}

And if start to use a ViewModel it really just seams like a copy of my Entity.

Feel free to comment anything about the above structure/code, and motivate way i need to do the change you say.

dimanche 30 décembre 2018

The difference between strategy and state design patterm, How a state is aware of its predecessor?

I read about state and strategy design patterns in refactoring.guru web site in pages State and Strategy. The author says

This structure may look similar to the Strategy pattern, but there’s one key difference. In the State pattern, the particular states may be aware of each other and initiate transitions from one state to another, whereas strategies almost never know about each other.

The author also says, the ConcereteState classes, store a variable context which is an object to Context class and by this variable, states may aware of each other.

There are two things I can't understand:

  1. How a state is aware of its predecessor?
  2. Where should I implement the logic of transition between states? For example state1 by input a moves to state2 and by b moves to state4, where exactly this logic must be implemented?

This is a simple implementation of strategy I implemented in php language

<?php
class Algorithms{
    public $algorithm;
    function __construct(AlgorithmsInterface $algorithm){
        $this->algorithm = $algorithm;
    }

    public function run(){
        $this->algorithm->run();
    }
}

interface AlgorithmsInterface{      
    public function run();
}

class Algorithm1 implements AlgorithmsInterface{
    public function run(){
        print "Algorithm1";
    }
}

class Algorithm2 implements AlgorithmsInterface{
    public function run(){
        print "Algorithm2";
    }
}


$payment = new Algorithms(new Algorithm2());
$payment->run();

and this is a simple implementation of State design pattern I implemented

<?php
    interface State{
        public function execute();
    }

    class Context{

        public $state;

        public function __construct(State $initialState){
            $this->state = $initialState;
        }

        public function changeState(State $state){
            $this->state = $state;
        }

        public function execute(){
            $this->state->execute();
        }
    }

    class State1 implements State{
        public function execute(){
            print "This is State1";
        }
    }

    class State2 implements State{
        public function execute(){
            print "This is State2";
        }
    }

    $initialState = new State1();
    $state2 = new State2();
    $context = new Context($initialState);
    $context->execute();
    $context->changeState($state2);
    $context->execute();

?>

I can't see much difference between state and strategy while I know exactly what is the intent of these strategies. Besides that, the logic of movement between states and the way a state should be aware of its parent are missed from the code.

Use @Pattern to validate UUID or UUID.toString()

As we know, @Pattern annotation can only be used for implementation of CharSequence, such as String. Thus the following validation on UUID won't work.

@Pattern(regexp="^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$")
private UUID                id;

Is there a way to use @Pattern annotation directly on UUID or based on UUID.toString()?

Implementing different iteration strategies on a composite task

I'm trying to improve my OOP understanding with a (what I thought was) simple task system. However, the more I think about it the more grows in complexity. Considering the following example:

Download document:
 | Send an email to a manager that a new document has been downloaded (with the file name and size)
 | Process document:
    -> Create report

In this example downloading a document is considered an expensive task. The order of sending an email to a manager and processing document shouldn't matter. They can however only be completed when a document has been fully downloaded.

Based on my reasoning there are 4 types of tasks:

  1. Simple task (Single task)
  2. Compound task (Combine multiple tasks in a single task, all get processed in a single go)
  3. Series task (Multiple tasks, the order of execution matters, processing happens step by step)
  4. Parallel task (Multiple tasks, the order of execution doesn't matter)

Now when implementing it seems to make sense to use the composite pattern (See image). Using an interface/abstract class as the base and inherit from it to create a SimpleTask and a CompositeTask. Then use the CompositeTask as the base class for the Compound/Series/Parallel.

However, this forces the CompositeTask to be non extendable. My assumption is that this would be undesirable for tasks which have certain expensive tasks with dependencies. Consider the example in which the document has to be downloaded before sending an email / processing(*). Therefor it seems like a good idea to place the type of execution outside the CompositeTask (strategy pattern?). This is the part where I'm starting to struggle cause I probably want some form of hybrid iterator/strategy class, however I'm unsure how to implement this. It raises the questions:

  • Is the "iterator strategy" the actual way to go or is my reasoning about this wrong/ did I miss something?
  • Who owns who? Does the composite own the strategy or is it the other way around? ( $composite->useStategy(...) or $strategy->setTask(...) )
  • Who's responsible for keeping track of the tasks? The composite or the strategy?
  • Should the strategy just return a list of items that should get executed or should it actually invoke the individual tasks?

Ideally / in the final version I would like to be able to step through every task step by step and have a some form of progress list to see how many tasks have been completed (loading screen). Keeping this in mind only adds to the confusion.

My apologies in advance for my English (non-native) or if this question is too broad or vague but I really don't know how to be more specific about this (nor do I know any other place to ask this question).


(*) Not sure if this is the best way of achieving this. But having the CompositeTask extendable allows to easily inject dependencies based on a task subclass. It feels a bit clunky though, but for example:

class RequiresDocument extends Task {
    function setDocument(document) {...}
}

class LoadDocumentTaks extends CompositeTask {
    function run() {
        $document = $this->fetchDocument();

        foreach ($this->tasks as $task) {
            if ($task instanceof RequiresDocument::class) {
                $task->setDocument($document);
            }
        }
    }
}

Have a problem with creating this using css,looking for ideas,like what shapes to use and how to approach

I got this assignment of creating this logo using CSS only,looking for help crating this,or something very much like this using css.

enter image description here

C++ Singleton Design pattern alternatives

I hate to beat a dead horse, that said, I've gone over so many conflicting articles over the past few days in regards to the use of the singleton pattern.

This question isn't be about which is the better choice in general, rather what makes sense for my use case.

The pet project I'm working on is a game. Some of the code that I'm currently working on, I'm leaning towards using a singleton pattern.

The use cases are as follows:

  • a globally accessible logger.
  • an OpenGL rendering manager.
  • file system access.
  • network access.
  • etc.

Now for clarification, more than a couple of the above require shared state between accesses. For instance, the logger is wrapping a logging library and requires a pointer to the output log, the network requires an established open connection, etc.

Now from what I can tell it's more suggested that singletons be avoided, so lets look at how we may do that. A lot of the articles simply say to create the instance at the top and pass it down as a parameter to anywhere that is needed. While I agree that this is technically doable, my question then becomes, how does one manage the potentially massive number of parameters? Well what comes to mind is wrapping the different instances in a sort of "context" object and passing that, then doing something like context->log("Hello World"). Now sure that isn't to bad, but what if you have a sort of framework like so:

game_loop(ctx)
   ->update_entities(ctx)
        ->on_preupdate(ctx)
             ->run_something(ctx)
                 ->only use ctx->log() in some freak edge case in this function.
        ->on_update(ctx)
            ->whatever(ctx)
                 ->ctx->networksend(stuff)
   ->update_physics(ctx)
        ->ctx->networksend(stuff)
        //maybe ctx never uses log here.

You get the point... in some areas, some aspects of the "ctx" aren't ever used but you're still stuck passing it literally everywhere in case you may want to debug something down the line using logger, or maybe later in development, you actually want networking or whatever in that section of code.

I feel like the above example would much rather be suited to a globally accessible singleton, but I must admit, I'm coming from a C#/Java/JS background which may color my view. I want to adopt the mindset/best practices of a C++ programmer, yet like I said, I can't seem to find a straight answer. I also noticed that the articles that suggest just passing the "singleton" as a parameter only give very simplistic use cases that anyone would agree a parameter would be the better way to go.

In this game example, you probably wan't to access logging everywhere even if you don't plan on using it immediately. File system stuff may be all over but until you build out the project, it's really hard to say when/where it will be most useful.

So do I:

  1. Stick with using singletons for these use cases regardless of how "evil/bad" people say it is.
  2. Wrap everything in a context object, and pass it literally everywhere. (seems kinda gross IMO, but if that's the "more accepted/better" way of doing it, so be it.)
  3. Something completely else. (Really lost as to what that might be.)

If option 1, from a performance standpoint, should I switch to using namespace functions, and hiding the "private" variables / functions in anonymous namespaces like most people do in C? (I'm guessing there will be a small boost in performance, but then I'll be stuck having to call an "init" and "destroy" method on a few of these rather than being able to just allow the constructor/destructor to do that for me, still might be worth while?)

Now I realize this may be a bit opinion based, but I'm hoping I can still get a relatively good answer when a more complicated/nested code base is in question.

samedi 29 décembre 2018

Scikit-learn pipelines : how to deal with hyperparameters in a clean way

Let's suppose that I have a Pipeline in scikit-learn (pipe and filter pattern).

Now let's suppose that this Pipeline is wrapped in a class of its own and that it has a pre-made list of hyperparameters, or a premade list of hyperparameters grid, as a static class constant. The class stays a pipeline so that it has the .fit and .transform (it inherits from the Pipeline object, but at construction, it already knows which pipeline steps to compose).

I now want pipelines of pipelines, so one master pipeline would compose each one. In the end, I want to perform a hyperparameter search, so I'd need the first pipeline to get the hyperparameters of every sub-pipeline and add the string prefix as needed.

So the question is: How to do this (optional: in a clean way)? I feel that I break some Object Oriented Programming (OOP)'s SOLID principles by having to get one massive hyperparameter grid out of each pipeline somehow recursively and to prepend prefixes to params with some __ separators.

Any suggestion will be appreciated. It's obvious that in my thought experiment, something is wrong with the way to share hyperparameters, and I'd like to fix that with. Can you suggest a design pattern or a more usable way to do that?

How to organize a C ++ large project in Visual Studio

I would like to separate my source code into folders to have a better organization by grouping the files into directories.

In general there are a lot of .h and .cpp files and I usually go separating the folders. I have always used netbeans, but recently I decided to test Visual Studio.

When starting the code (little thing, to follow a tutorial of creation of games in SDL), I began to organize the source in folders. In Visual Studio when I'm working with C ++ I see the option to add filter, to organize the file structure, but I do not see the option to add folders.

I can organize the code in what visually seems to be a folder structure, but when I go to check the files are a mess, it's all mixed up at the root of the program. Menu options only allow me Add a filter:

Apparently it gets organized, but only visually, the codes all remain in the same place.

I would like to know if inside visual studio there is a way to convert these filters into folders, or how to add the folder through visual studio, because I ended up creating the folders and reorganizing manually.

Not that I usually have files with the same name, but in that environment for example this would be impossible. Besides that I think it gets pretty messy if you look for something, or if you need to switch IDE in the future, because I believe that this logical organization will not be reused in another IDE such as Eclipse, Netbeans and etc.

What standard design pattern or concept is this?

Question: If I had a model object, call it Employee, and I wanted to expose different views of the person object depending on the consumer of the object, like a : ManagerEmployeeView, EmployeeEmployeeView or OwnerEmployeeView, what is this pattern called? Is it any good? What are the standard nomenclatures associated with such designs? I ask so that I don't reinvent the wheel.

Purpose: After modeling an object, I want to expose different "views" to consumers such that I only give them what they need and nothing more - for security sake and other purposes. The so called "views" are not to be mistaken with an MVC view, i.e. it does not render anything, it is only a model.

Thoughts: The models are rich model, not anemic, thus the views not only abstract properties but also provide different behaviors. I would like to know if there are similar approaches so that I can learn from them. Naming styles for such an approach/models is more than appreciated, even though it may be considered subjective, I feel as though proper naming is an important step to master the idea. ViewModels come to mind as a solution, but call it cargo cult, ViewModels are usually employed as a model for a view (UI) and not a "model for a model" (if that makes any sense) - correct me if I'm wrong.

Thanks in advance.

How come up with software requirements

Can anyone please help me with the right approach to this problem. In aiming to create software that is very functional and everyone loves, I've been investigating how to come up with software functional requirements. How to decide which features a software should have. I have created software and lost confidence whilst at it because I look at it and feel it should have been better than that. What am i not doing right?

vendredi 28 décembre 2018

What if one store is dependent on output of other store in flux architecture

So, I have a problem where on click of a button:

  • I do the processing in flux store lets name it Store_1. On the basis of output of Store_1, I have to make multiple ajax calls from Store_2.
  • Publish the ajax call data to view on as soon as its available without waiting for other ajax calls to complete. Eg. - when 1st ajax call is completed, publish that data to the view, then publish 2nd ajax call data to view when it's available & so on.

So, there are 2 problems I am trying to solve in flux -
1) How to use the output of store_1 in store_2
2) How to publish the ajax call results to the view multiple times.

I am trying to implement this in JavaScript

I don't understand how a Python GUI app should be designed

I've created a new app with Python and PyQT5. The app/gui is used to call external binaries that do some work with files. I have all buttons working and when I hit run I create a thread that executes the binaries (with subprocess.run). My pain point and what I don't understand is how to send feedback from the thread to GUI. If I use .join() I block the GUI. Events seams to be the wrong solution (How to send a signal to the main thread in python without using join?)

Thanks

Summary

What I have: (GUI classes) -> thread--> python method that calls subpocess.run I used the thread thinking that I need to decouple the GUI thread from the business logic.

What I need to figure out: how to determine if thread is done without blocking the GUI.

Resume design pattern for java

Hi
I'm implementing a task, in which i need to performs 5-6 steps, and if my code fails at some step in between my requirement is to retry from the same step where it failed (This info I'm maintaining in a DB table), I can solve this thing with complex if-else check, but I'm looking for some standard or good way to solve this problem.

How can I find particular string in html files using pattern regular expression?

I want to extract the content that is enclosed from the tag. I also want to not extract duplicate content but the same content if find a string more than 1 time I want to display it only the first time. I try the findstr from dos command line but I dont know very good the regular expression. Any idea?

Problem with business and data access layer design

I am creating a library to interact with third party api.This library will be wrapper around that third party library and i want to expose my wrapper methods to client(webapi,winform,console,mvc etc..).

Below are the methods that i want to expose to my clients to perform operations using third party apis but i dont want to give them direct access.They will always use my wrapper api to perform operations.

public interface IMyLibraryWrapperApi
    {
             //methods expost to clients
         int AddRegion(RegionRequest request);
    }

    public class MyLibraryWrapperApi : IMyLibraryWrapperApi
    {   
        private readonly IThirdPartyUnitOfWork _thirdPartyUnitOfWork;
        private readonly IRegionServices _regionService;
        public MyLibraryWrapperApi(string domain, string username,string password)
        {
            this._thirdPartyUnitOfWork = new ThirdPartyUnitOfWork(domain, username,password);
            this._regionService = new RegionServices(_thirdPartyUnitOfWork);
        }
        public int AddRegion(RegionRequest request)
        {
           return _regionService.CreateRegion(RegionRequest request);
        }
    }

Service/Business Layer :

public class RegionService : IRegionService
    {
        private readonly IThirdPartyUnitOfWork _thirdPartyUnitOfWork;
        public RegionService(IThirdPartyUnitOfWork thirdPartyUnitOfWork)
        {
            this._thirdPartyUnitOfWork = thirdPartyUnitOfWork;
        }
        public void CreateRegion(RegionRequest request)
        {
            _thirdPartyUnitOfWork.Insert(request.Name,request.Direction);
        }
    }

DataAccess Layer :

public interface IThirdPartyUnitOfWork 
    {
        int Insert(string name,string direction);
        T GetById(int id);
    }

    public class ThirdPartyUnitOfWork : IThirdPartyUnitOfWork, IDisposable
    {
        private readonly ServiceContext _serviceContext;
        public ThirdPartyUnitOfWork(string domain, string username, string password)
        {
            _serviceContext = new ServiceContext(domain);
            _serviceContext.Credentials = new ThirdPartyCredentials(username, password);
        }
        //Insert method implementation
        //GetById method implementation
    }

Now I want that IMyLibraryWrapperApi should always interact with Service Layer and Service layer will interact with data access layer but here as you can see that IThirdPartyUnitOfWork is being exposed in IMyLibraryWrapperApi and even any client can call IThirdPartyUnitOfWork which i dont want.

But with current design I am not getting how to design this layer properly so that they do not leak in to other layers.

Can anybody please help me with some suggestions to improve this design

mercredi 26 décembre 2018

What are the principles which make Spring beneficial

I don't get the benefit of using Spring instead of just using regular Java EE, and I think the reason is I don't have a good grasp of some of benefits of the design principals that Spring employs (dependency injections, etc.)

For example I don't understand what benefits we get by adding @Bean, @Component @Autowired and @Repository annotations.

My question is not what do those annotations do, the question is more what are the principals that Spring make the implementation of which easier or more effective?

Correct way to deal with abstract classes and abstract members in C++

I am looking for a (possibly) better approach to solve a problem with abstract classes. This question is more about an alternative design than the implementation provided here (I willingly simplified it).

I have a basic Content interface class like this:

class Content {
public:
    virtual void info() = 0;
};

class ContentA : public Content {
public:
    void info() override { cout << "Content A" << endl; }
    int serial() { return 123; }
    int num() { return 456; }
};

class ContentB : public Content {
public:
    void info() override { cout << "Content B" << endl; }
    int identifier() { return 789; }
};

I have a Container class interface which has to contain a Content object:

class Container {
public:
    virtual shared_ptr<Content> content() = 0;
    virtual void info() = 0;
    void contentInfo() { content()->info(); }
};

class ContainerA : public Container {
public:
    ContainerA(shared_ptr<ContentA> content) : m_content(content) {}
    shared_ptr<Content> content() { return m_content; }
    void info() {
        auto identifier = m_content->serial() * m_content->num();
        cout << "Container A: " << identifier << endl;
    }
protected:
    shared_ptr<ContentA> m_content;
};

class ContainerB : public Container {
public:
    ContainerB(shared_ptr<ContentB> content) : m_content(content) {}
    shared_ptr<Content> content() { return m_content; }
    void info() {
        cout << "Container B: " << m_content->identifier() << endl;
    }
protected:
    shared_ptr<ContentB> m_content; 
};

Usage example would be:

auto contentB = make_shared<ContentB>();
ContainerB containerB(contentB);
containerB.info();
// => "Container B: 789"
containerB.contentInfo();
// => "Content B"

Although ContentA and ContentB both inherit from Container, they have their own specialized methods intended to be specifically used by ContainerA and ContainerB respectively. ContainerA can only interact with ContentA, and ContainerB can only interact with ContentB. You can't instantiate ContainerA by passing a ContentB to its constructor.

I want to enforce the fact that, in the future, anyone needing to create ContainerC will also need first to create ContentC. This is why the content() method is virtual pure.

I'm not sure using such content() method is the correct way to do this. Is it? I don't know. You can test it on Ideone here.

Basically, I thought it would reduce repetition if m_content was a member of the abstract class Container. But by doing so, it won't compile as the exact Content inherited class is not known from the Container. That would required casting the content object, which is worse. See what I mean here.

Using Chain Of Responsibility Pattern

I was checking the following thread: Refactoring if-else if - else

Im seeing on the proyect Im working a lot of if with if else and else statements nested with each other and I wanted to take opportunity on this pattern but I wanted to check if this piece of code can be refactored like this.

So the code in question is the following:

            if (factura.fac_diferencia > 0)
            {
                if (factura.fac_diferencia == 1)
                {
                    throw new Exception("Error 1");
                }
                else if (factura.fac_diferencia == 2)
                {
                    throw new Exception("Error 2");
                }
                else if (factura.fac_diferencia == 3)
                {
                    throw new Exception("Error 3");
                }
                else if (factura.fac_diferencia == 4)
                {
                    throw new Exception("Error 4");
                }
                else
                {
                    throw new Exception("Another Error");
                }
            }

What I did is move the error codes in a constant class where I can manage this particular error codes and I moved the strings to a resx file they already had.

Now the code is like this:

                            ValidacionAprobacionFactura ValidaImporteDesglozado = new ImporteTotalDesTolerancia();
                            ValidacionAprobacionFactura PercepcionSuperaTolerancia = new PercSuperaTolerancia();
                            ValidacionAprobacionFactura DifTotalDesglozadoTranSuperaTolerancia = new DifTotalesDesgTranSuperaTolerancia();
                            ValidacionAprobacionFactura DifTotalTranSuperaTolerancia = new DifTotalesTranSuperaTolerancia();
                            ValidacionAprobacionFactura NoHayCoherenciaTotales = new NoCoherenciaTranFac();

                            ValidaImporteDesglozado.SiguienteSucesor(PercepcionSuperaTolerancia);
                            PercepcionSuperaTolerancia.SiguienteSucesor(DifTotalDesglozadoTranSuperaTolerancia);
                            DifTotalDesglozadoTranSuperaTolerancia.SiguienteSucesor(DifTotalTranSuperaTolerancia);
                            DifTotalTranSuperaTolerancia.SiguienteSucesor(NoHayCoherenciaTotales);

                            if (factura.fac_diferencia > Constantes.CODIGOERRORDEFAULT)
                            {
                                string resultado = ValidaImporteDesglozado.Ejecutar(factura.fac_diferencia);
                                throw new Exception(resultado);
                            }

I did not include the pattern in question as you may already know it.

I wanted to ask if this is the correct way to refactor this code as, this throws an exception. Maybe there is another type of Pattern for Bubbling an error message?

Another thing would be, for each type of error, I will need to instantiate a new ValidacionAprobacionFactura. Is there any way to accomplish the same maybe with one new? a list of this type maybe?.

Maintaining order of processing Streaming Events along with other conditions

I have an events service which corresponds to all the activities (viewed / submitted) by the user. Each event will have a session_id along with other attributes such as page_id, url, page_type (viewed / submitted ), etc.

I have the following problems that I need to cater:

  1. Since there would be a lot of events pushed, I want to write / push them somewhere in the fastest way possible.
  2. Event Processing for different session_ids should be done in parallel. For events with the same session id though, the processing should be synchronous. For example, customer-payment event should be before form-submitted event
  3. Event processing is done by a separate service. This service exposes a url where event data is pushed in order to be processed. Now, I don't want to overload this service with more requests than it can handle. If it can handle 2k requests concurrently, I should be able limit my concurrent calls to not more than 2000.

Here is what I have been able to come up till now.

For Problem 1:

I have a separate service that pushes events received from the browser to AWS DynamoDB. Then, I can enable Streams on the table created. And through proper setting of partitions while creating a table, I can ensure that event logs for a single session_id are sorted (by keeping the partition key as session_id and sort key as created_at).

However, I don't know how to solve the other two problems. The solutions that I have in mind can solve either of the two but not both.

  1. I can set up a pooling service that ensures that total number of requests to the event-processing doesn't exceed a certain amount. If the incoming requests are more, then it will queue them and process it as soon as the event-processing server is free i.e number of concurrent connections are less than 2000. But this solution will not ensure that events belonging the same session_id are processed synchronously. If I have pool limit of 2000 connections, and I have 20 events of the same session, my pooling-service will make 20 requests to event-processing service in parallel.
  2. I can have a service that spawns a new process for each session_id when processing an event. In that case, I will have a process per session_id for processing an event. So, I will ensure that events belonging to the same session_id are sent to a single process. Now, these processes need to be lightweight so that my service doesn't bloat up when there are multiple number of concurrent sessions. I can write the service in Go or Erlang over here. But this doesn't ensure that event-processing service gets no more than specified number of requests in parallel.

Can someone help in figuring out the solution or point me in the right direction?

mardi 25 décembre 2018

Autofac - Register for Transaction

Lets start with my architech. I will try to simplify my code as much as I can. If I totally mixed up, please warn me.

IUnitOfWork

public interface IUnitOfWork<T> : IDisposable
{
    IEntityRepository<T> Repository { get; }
    void Commit();
}

UnitOfWork

public class UnitOfWork<T> : IUnitOfWork<T>
{
    private IDbConnection _Connection;
    private IDbTransaction _Transaction;

    public IRepository<T> Repository { get; private set; }

    public UnitOfWork(IDbConnection Connection, IRepository<T> Repository)
    {
        _Connection = Connection;
        this.Repository = Repository;
         _Transaction = _Connection.BeginTransaction();
    }
}

RepositoryBase

public abstract class RepositoryBase<T> : IRepository<T>
{
    protected IDbTransaction Transaction;
    protected IDbConnection Connection { get { return Transaction.Connection; } }

    public RepositoryBase(IDbTransaction transaction)
    {
        Transaction = transaction;
    }
}

TestDAL

public class TestDAL : RepositoryBase<Test>, ITestDAL
{
    public DpTestDAL(IDbTransaction transaction) : base(transaction) {}
}

TestService (BLL)

public class TestService : ITestService
{
    private IUnitOfWork<Test> uow;
    public TestService(IUnitOfWork<Test> unitOfWork)
    {
        uow = unitOfWork;
    }
    public List<Test> GetAll()
    {
        return uow.Repository.GetAll().ToList();
    }
}

And my autofac configurations.

builder.RegisterType<TestService>().As<ITestService>();
builder.RegisterType(typeof(OracleConnection)).As(typeof(IDbConnection)).InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(RepositoryBase<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(UnitOfWork<>)).As(typeof(IUnitOfWork<>)).InstancePerDependency();
//builder.RegisterType(typeof(OracleTransaction)).As(typeof(IDbTransaction)).InstancePerLifetimeScope();

I am newbie this kind of architect and try to something my self. Please tell me if there is something wrong or totally wrong.

My problem is, I need to pass the IDbTransaction to data acess classess contructor. When I do not register IDbTransaction interface, exception is "could not resolve parameter", when I try to register with OracleTransaction the exception is "OracleTransaction" do not has a public contructor. Where did I mixed up?

Is libgdx ModelBatch suitable for Ashley?

In other words, what's the best practice to render modelparts in ashley's rendering subsystem?

  • The Question in Details:

The initial thought is put any visual effects handling into shader as much as possible. Performance of this is better because they are handled in GPU. There are lots of example like this at three.js and shadertoy.

To do this in Ashley, I can use a component with all the parameters sent to GPU before rendering any mesh. So the render sub system would look like:

public class SysDynamicRender extends IteratingSystem {
    ShaderProgram program;
    private void initShader() {
        program = new ShaderProgram("vert", "frag");

        // variables only about the shader program, not about meshes
        u_projTrans = program.getUniformLocation("u_projTrans");
        u_worldTrans = program.getUniformLocation("u_worldTrans");
        u_colorLoc = program.getUniformLocation("u_color");
        texBase = new Texture("data/modlib/color-blocks.jpg");
    }

    @Override
    protected void processEntity(Entity entity, float deltaTime) {
        // in Ashley, we only get meshparts from entity
        renderQueue.add(entity);
    }

    @Override
    public void update(float deltaTime) {
        super.update(deltaTime);
        // render
        for (Entity e : renderQueue) {
            // way 1: use the help of ModelBatch
            CmpModelInst cmpModelInst = e.getComponent(CmpModelInst.class)
            modelBatch.render(cmpModelInst.inst(), shader);

            // way 2: rendering meshpart directly (not work)
            // 2.1 get the Renderable (out)
            DynaAttr attr = e.getComponent(CmpVisuals.class).attr;
            ModelInstance inst = cmpModelInst.class).inst();
            Node n = inst.getNode("cube1_cube1_auv");
            out = inst.getRenderable(out, n);
            // 2.2 rendert meshpart with shader program
            program.setUniformMatrix(u_worldTrans, out.worldTransform);
            program.setUniformf(u_colorLoc, attr.effects());
            program.setUniformf(u_factorLoc, attr.u_factorF());
            out.meshPart.render(program);
        }

    renderQueue.clear();
    }
}

I think there must be a better way to implement update().

The question is what's that?

  • Further Consideration

The visual effects parameters like color, reflecting angles (for different color) are attributes of the shaders, they shouldn't been updated at each rendering call iterating over meshparts. It's a performance cost because CPU needing send data to GPU - if I understand correctly.

Ashley provided a schema for solving this. Just implement a dynamic shader and put the referencing component into entity, let Ashley call the correct sub system.

In this way, ModelBatch must not suitable? The ModelBatch.flush() is simply iterating over it's parts.

Reference:

ModelBatch.java: https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g3d/ModelBatch.java

wiki: https://github.com/libgdx/libgdx/wiki/ModelBatch

Something I don't understand?

Singleton vs static for logger and performance issues

As Some article suggest that, Logger is one of the example of singleton, but what will happen if application is used by more than 100000 users, Will it hit performance issue? Will static solve the purpose here? Or what should I use?

If I need to use other library like Log4Net then whats is best example of Singleton pattern.

How to avoid responsibility for subscribed methods

I was surprised to find out that my well functioning methods are kept responsible for failures in subscribed methods having mine fail too. How should I raise the event to notify all the subscribers of the success while keeping all the responsibility isolated?

public class A
{
    public event EventHandler<EventArgs> Success = null;
    public void DoSomething()
    {
        if (this.Success != null) { this.Success(this, EventArgs.Empty); }
    }
}

A a = new A();
a.Success += (object senderObj, EventArgs arguments) => { throw new Exception(); };

try
{
    a.DoSomething();
    int foo = 0; // will not be reached
}
catch (Exception ex) { }

I can catch the exceptions while raising events, but then not all of the subscribers are notified:

public class A
{
    public event EventHandler<EventArgs> Success = null;
    public void DoSomething()
    {
        if (this.Success != null) { try { this.Success(this, EventArgs.Empty); } catch (Exception ex) { } }
    }
}

A a = new A();
a.Success += (object senderObj, EventArgs arguments) => { MessageBox.Show("ok"); };
a.Success += (object senderObj, EventArgs arguments) => { throw new Exception(); };
a.Success += (object senderObj, EventArgs arguments) => { MessageBox.Show("ok2"); }; // is not reached

try
{
    a.DoSomething();
    int foo = 0; // is now reached
}
catch (Exception ex) { }

I was hoping to send the success notifications and have both the sender and each subscriber responsible for itself. What is the proper design?

React service singleton alternative

I'm making a ReactJS application using the google maps library & I'm trying to abstract most map logic to a service, so that I could exchange the google maps library with leaflet if I wanted to in the future.

The way it works now is that I have a component loading the map library and attaching it to a div. The onLoad callback will set the map in this service, so that I can keep a reference to it.

onMapLoad={(map: Map) => {
    mapService.setMap(map);
    props.mapLoaded();
}}

The service also needs to have access to my redux store to dispatch actions when for example a certain marker on the map is selected. I'm setting this while bootstrapping the application in the index.tsx file

const store = getStore();
mapService.setStore(store);

The service itself is a singleton, but I'm wondering if there's a better pattern to use with React or just in general. I'll post a shorter version of the service, with some methods left out for brevity. Does anyone have a pattern advice that might improve this practice?

interface MapService {
    predictPlaces: (query: string) => Observable<AutocompletePrediction[]>;
    addMarkerToMap: (surfspot: Surfspot) => void;
    centerMapAroundSuggestion: (suggestion: Suggestion) => void;
    setMap: (newMap: Map) => void;
}

let predictService: google.maps.places.AutocompleteService;
let geocoder: google.maps.Geocoder;
let map: Map;
let store: Store;
let markers: google.maps.Marker[] = [];

const setMap = (newMap: Map) => {
    map = newMap;
}

const setStore = (store: Store) => {
    store = Store;
}

const centerMapAroundSuggestion = (suggestion: Suggestion) => {
    if (!map) {
        throwGoogleMapsNotLoaded();
    }
    if (!geocoder) {
        geocoder = new google.maps.Geocoder();
    }
    ... further implementation ...
}

const predictPlaces = (query: string): Observable<AutocompletePrediction[]> => {
    if (!map) {
        return of([]);
    }
    if (!predictService) {
        predictService = new google.maps.places.AutocompleteService();
    }
    ... further implementation ...
}

const addMarkerToMap = (place: Place, onSelect: () => void) => {
    const marker = createMarker(place, onSelect);
    markers.push(marker);
}

const createMarker = (place: Place): Marker => {
    if (!map) {
        throwGoogleMapsNotLoaded();
    }
    const marker = new google.maps.Marker({
        ...options...
    });
    marker.addListener('click', () => {
        createInfoWindow(marker)
        if(!!store) {
            store.dispatch(createMarkerClicked(place))
        }
    });
    ... further implementation ...
}

function throwGoogleMapsNotLoaded() {
    throw new Error('Google maps not loaded');
}

export const mapService: MapService = {
    predictPlaces,
    addMarkerToMap,
    setMap,
    centerMapAroundSuggestion
}

why are service layers good at all?

I know it's a huge text, but thanks so much in advance for reading it. Appreciate that really.

Let's imagine I'm using MVC. controller got the request from user. do the logic in controller and return the response.

People say that controllers don't do any logic, they simply give the incoming request to service and all the logic stays in service classes methods. BUt I don't understand why this is good.

Argument 1) people say this is good because you have skinny controllers instead of fat controllers. but who cares skinny controllers if it doesn't give you benefit?

Argument 2) your business logic is somewhere else and not coupled to controller. Why is this argument even worth mentioning? Okay, I have all the logic in service classes' methods and in my controllers there're two lines of code. what did that give me? Service classes are still huge. Is this some kind of benefit?

Argument 3) one even told me that service layer is good because from service methods you return objects to controller and in controller we sometimes return json or some other format. he told me this is good if we have desktop/web/mobile application all together and we are writing api for them. but still doesn't make sense.

What people do and I hate is that they use repository and service(in service methods, they have business logic and repository classes method calls).

Here is how I think. IF using service classes(I call it helpers), in a service method, there shouldn't be a thing related to framework. if there's freamwork dependent code, then it's bad because all my business logic is tightly coupled to framework. What my friend advised is that I put get,insert,update eloquent calls in controller and pass the results to helper(service) which does the data modification. This way to test a helper(service) no need to inject repository or model at all. and why do we have to even need to test repository or model (it's already tested by the framework).

I need your help, This is the first time I'm really hopeful that people can really help me. I've been thinking about this and I still don't get it for a month already. I even have a deadline for my project and I don't want to write bad code. Project is huge. I just have to understand why service layer is gonna help me. The thing is I've read so much, (so much you can't imagine) and none of the articles really say the real benefits. Is it possible we discuss pros and cons with examples? I'd also love to chat privately to someone with experienced skills. I'd do anything to understand this.

Why is MVC more common in web development and MVP in desktop development?

It seems that MVC design pattern is very popular in context of web development and MVP design pattern on the other hand is quite common for desktop development. Why is this the case and why don't we see MVP web applications and MVC desktop applications?

lundi 24 décembre 2018

Looking for suggestions to implement this

I have a UI where I show a list of features in a dropwdown. Based on user selection from the dropdown, I render the feature. On landing I keep that dropdown disabled until I receive some data from ajax call.
So this is the flow -
* User lands on the page, dropdown for selecting features is disabled.
* Once loading completes, I send an ajax call to receive the data.
* Once that data is obtained, I enable the dropdown.
* User then selects a feature from the dropdown.
* That feature is rendered.
* At any time user can select any feature from the dropdown.
* Based on some user interaction, I will need to fire the ajax call again & disable the dropdown until then, so that I recalculate again on the server side which all features to be shown inside the dropdown.
* Cycle continues.

Why am I waiting for data to enable the dropdown?
Because the feature list which is being shown requires some data for getting rendered. That data is received using ajax call which I trigger after landing. The data is a map where key is featureName & value is the object which will have necessary information to render the corresponding feature. Hence, I don't want to enable dropdown until I have all the data for the features to be rendered.

I tried implementing this but it's getting very messy.
Breaking down the components which I am trying to implement in a clean way -
* Store each feature's corresponding data which I am getting through ajax call.
* On receiving the data, enable the dropdown.
* When another ajax call is required, disable the dropdown.
* When user selects 1 feature, do some processing on that feature's corresponding data & render that feature.

I am thinking about flux pattern where my first action would be when data is received from the ajax call & after that the further actions would come from UI depending on which feature user has asked to render. This approach is also not looking good.
Can there be any cleaner approach/design pattern which I can follow in Javascript. Any suggestions will be greatly appreciated.

Javascript: Which design better or approach could fit on those iterative methods?

Sorry for this not-specific but general title for post but I couldn't set it specifically while usage of Design Patterns are on a theoretical base.

I would like to share couple of code-snippets which shows a very similar usage but tiny changes. I've tried to make a easy-maintainable code block but I couldn't achieve the aim.

let preLabel = self.fieldLabel,
               asteriksLabel = preLabel += " *";

// Initialise way of function
function getLabel(self) {
    setAsteriks();
}

function hideLabel(self) {
    setAsteriks('EmptyText');
}

//and the function itself which I'm looking a Design Pattern;
function setAsteriks(emptyText = false) {
    if (emptyText) {
        if (!self.allowBlank) {
            self.setEmptyText(asteriksLabel);
        } else {
            self.setEmptyText(preLabel);
        }
    } else {
        if (!self.allowBlank) {
            if (self.fieldLabel.slice(-1) !== "*") {
                self.setFieldLabel(asteriksLabel);
            }
        } else {
            self.setFieldLabel(preLabel);
        }
    }
}

As you'll notice setAsteriks method queries emptyText parameter and through it decided to use setEmptyText or setFieldLabel.

Thanks for any advice.

Trying to create abstraction layer to support multiple third party as document management system

I am trying to create content management library for sharepoint and in future we could deal with any CMS like google drive etc.

Right now I am using sharepoint as Document Management system hence i am trying to write an abtraction layer in such a way that no matter what cms we use our client code should not be changed.It should always work with that abstraction.

So this is the structure which i have in mind :

I will have 1 ContentManagement.Library.Common which will have abstraction like this :

public interface IContentRepositoryConnection : IDisposable
    {
        string Username { get; set; }
        string Password { get; set; }
        string ClientId { get; set; }    
        string ClientSecret { get; set; }
        void Create(); // handshake with sharepoint/google drive server using username/password or ClientId/Secret
    }

public abstract class ContentRepositoryConnection : IContentRepositoryConnection,IDisposable
    {
        public abstract string Username { get; set; }
        public abstract string Password { get; set; }
        public abstract string ClientId { get; set; }
        public abstract string ClientSecret { get; set; }

        public void Create()
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }

Here is my seperate Sharepoint.library which will deal with sharepoint specific apis and sharepoint server :

public class SharepointConnection : ContentRepositoryConnection
    {
        //Where should i place SiteUrl Property which deal with sharepoint?
         //override relevant methods 
    }

Client (Console/Winform) : Will have reference of only ContentManagement.Library.Common

Client will always work with ContentRepositoryConnection class so that in future if client can easily swap between sharepoint/google drive.

Now in order to create ClientContext(just like EF Dbcontext), there are couple of parameters which sharepoints needs like SiteUrl,Username,password which might differ from Google Drive so how do i create this abstraction?

Now suppose tomorrow if i change my abstraction layer in a way that it can work with google drive and not impacting sharepoint client is possible?

This is how Client Context(Which is everything is Sharepoint) is created :

var ctx = new ClientContext(siteUrl,username,password);

I know every ORM like EF,dapper always works with abstraction(DbConnection class) instead of Concrete class(SqlConnection,OracleConnection etc..) so I am also trying to achieve same sort of abstraction in a nutshell.

Can anybody please guide me with some design changes or structure or can suggest some changes in above structure if there is any?

dimanche 23 décembre 2018

Java phone Number format regex

I am wanting to create a regex for the following number formats: +XXXXXXXXXX. +1(XXX)xxxxxx, +x(xxx)-xxx-xxxx, xxx-xxx-xxxx, xxx-xxxx, and Phone Number:,Phone:,Tel: with all the above formats. All with the output of xxxxxxxxxx

Below is a snippet of my code.

  public static String getPhoneNumber() // returns the phone number formatted as a sequence of digits
    {

        String regex = "^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})(?:Tel:)$";
        Pattern pattern = Pattern.compile(regex);
        for (int i = 0; i < line.length(); i++) 
        {
              //if phone number format includes -, . , spaces, + sign in front
              if (line.matches("[+]?\\d?[- .]?(\\([0-9]\\d{2}\\)|[0-9]\\d{2})[- .]?\\d{3}[- .]?\\d{4}$")) {
                phoneNumber = line.replaceAll("[^\\d.]", "").replace("-", "").replace(".", "").replace(" ", "").replace("(", "").replace(")", "")
                        .replace("+", "");

              }
              else
              {
                  getEmailAddress();
              }
                  }
        //System.out.println(phoneNumber);
        return phoneNumber;
    }

ABAP OOP (patterns) - How to properly encapsulate granular RFCs into ABAP objects?

I hope it's not too basic or maybe too wide question but I have doubts to migrate from ABAP RFC functions to classes in a satisfactory way.

Let's say that I have an object "Sales Order" that has multiple properties like ID, description and also a list of "Items". Each item also has multiple properties including a list of "Serial Numbers". In my real scenario things are much more complicated but this is OK for the question.

I have a long list of RFC functions that do very specific things, for instance:

  • Get Sales Order (with option to get items or no...)
  • Update Sales Order Header
  • Update Sales Order Long Text
  • Update Sales Order Status
  • Copy Sales Order
  • Sales Order Item Add
  • Sales Order Item Serial Number Add
  • ...

Now I want to implement this using ABAP Objects, I still need to use RFCs (can be new ones) to expose the calls due to an old middleware. However I'm not sure how to create the classes properly. My first idea is to create only CRUD RFCs mapped to respective methods and force myself to create the necessary objects for that which in this case would be: SalesOrder, SalesOrderStatus, SalesOrderLongText, SalesOrderItem, SerialNumber. Then I still would have some special function like Sales Order Copy that may required to be static but I'm not sure. This one could be handled by the frontend with Read + Create, but it's not a good idea for performance reasons.

A second option I thought is to just use static methods, but then I don't see how I benefit from OOP.

Do you think my first approach is correct?

Creating unique identifier inside a class

  1. The database has a method for generating a unique identifier for a particular person. Creating an identifier uses the merged name and surname converted into lowercase letters. If it's already an identifier is created then a continuation is added to it - the number where the number changes in the number from 1, 2,. . . until an identifier is assigned to an namesurname number that was not previously generated.

How do I implement this method?

  1. The database can store data using a vector or string. Distinguish using the bridge. How can this be achieved?

Builder Pattern for Parameter Object

Please pardon my poor knowledge of design patterns.

Sometimes methods have many parameters and introducing a Parameter Object is the right way to refactor your code according to refactoring-guru article.

Imagine a situation when we have a service which handles some sort of documents.

public class FinancialStatementService : IFinancialStatementService
{
    public void Print(Options input)
    {
        // creating basic document content and adding optional data below:

        if (input.HasAccountNo)
        {
            // include account number
        }
        if (input.HasPaymentDetails)
        {
            // include payment details
        }
        if (input.HasZeroBalances)
        {
            // include zero balances
        }
        if (input.HasTotal)
        {
            // include total
        }

        // and then print the document
    }
}


public class Options
{
    public DateRange DateRange { get; set; }

    public bool HasAccountNo { get; set; }
    public bool HasPaymentDetails { get; set; }
    public bool HasZeroBalances { get; set; }
    public bool HasTotal { get; set; }
}

The document consists of many parts, some are optional. Sometimes we need the document to contain all possible details.

But imagine a case when a certain organization doesn't want some of the details.

Ideally I'd like to have a class which handles options creation and has methods containing organization name e.g.

public class OptionsCreator
{
    // in tax office they want to see all possible details
    public static Options GetTaxOfficeOptions(DateRange dateRange)
    {
        return new Options() { HasAccountNo = true, HasPaymentDetails = true, HasZeroBalances = true, HasTotal = true, DateRange = dateRange };
    }

    // in some other organization they DO NOT NEED zero balances & payment details
    public static Options GetSomeOtherOgranizationOptions(DateRange dateRange)
    {
        return new Options() { HasAccountNo = true, HasPaymentDetails = false, HasZeroBalances = false, HasTotal = true, DateRange = dateRange };
    }
}

But I'm afraid the above example is an anti-pattern.

Another thing I can think of is a Builder Pattern.

Would Builder Pattern be the most optimal solution for the Parameter Object?


IMHO, implementing a Factory is an overkill as it would involve implementing an interface for the Parameter Object.

Couldn't quite understand the loops used to create the pattern

enter image description here

Can someone explain the loops used here?

samedi 22 décembre 2018

Which Design Patterns is this?

I am having a problem trying to differentiate between two design patterns. I can understand both and the intent for each one of them and who to implement them. But I can't figure the difference between them code wise, after renaming it to generic names and hiding the intended is it possible to tell which one is this?

interface IA{
      void A1();
    }

    class SubA1 : IA
    {
      public void A1()  {}
    }

    class SubA2 : IA
    {
      public void A1() {  }
    }

    class SubA3  : IA
    {
      public void A1() {  }
    }

public class Context{

  private IA _stateOrAction;

// i could be injecting a strategy algorithm or setting initial state 
  public Context(IA stateOrAction)
  {
    _stateOrAction= stateOrAction;
  }

// i could be changing a strategy algorithm or chaining current state    
  public void Set_IA(IA stateOrAction)
  {
    _stateOrAction= stateOrAction;
  }

  public void DoWorkDependingOnStateOrAlgorithm(object iCouldBeHereOrNot)
  {
    // just ignore that this is not same signature as interface
    _stateOrAction.A1(iCouldBeHereOrNot);
  }

}

Data only class and another class that accesses it's data?

I have a class that only holds data, specifically Collections of data, it is called *BezierSplineData:

class BezierSplineData
{
    public List<BezierSplineControlPoint> ControlPoints; // ControlPoints that can change the shape of the spline.
    public List<BezierSplinePoint> Points; // Actual points in the 3D world that represent the spline.
}

BezierSplineControlPoint has only 3 fields:

  • Position
  • FirstTangentPosition
  • SecondTangentPosition

BezierSplinePoint only 1:

  • Position

Another class called BezierSplineCalculator, it is used to calculate 3D points for a set of BezierControlPoint's.

class BezierSplineCalculator
{
    public BezierSplineData Data;

    private void Recalculate()
    {
        // Code that will recalculate the Data.Points based on the Data.ControlPoints;
        // Essentially generates the actual spline.
    }
}

Now the BezierSplineCalculator class can have methods such as, AddControlPoint(BezierSplineControlPoint controlPoint), and other methods like RemoveControlPoint, InsertControlPoint and probably more, this would cause a recalculation of the spline.

My question is, it doesn't look right that I have a separate class just for that as I would need to refer to that Data quite often, let's say I need to render that BezierSpline, I would create a BezierSplineRenderer class and access the Data field from the BezierSplineCalculator class.

Is this a code smell, should I keep the data and calculator in one class?

Clarifying uml class diagram of factory design pattern

I was learning factory method design patter and found following class diagram in a tutorial. I understand product and concreteProduct part but Creator and ConcreteCreator part look little vague to me. I appreciate if someone clarifies the url diagram. Thanks. enter image description here

Using Laravel contracts in Application/Domain layers

I'm building application at top of the Laravel. In my Domain layer I have services. One of these needs to send an EMail. Laravel has Illuminate\Mail package for these purposes.

But Mailer contract depends on \Illuminate\Mail\PendingMail class. https://github.com/laravel/framework/blob/5.7/src/Illuminate/Contracts/Mail/Mailer.php

Does it mean I need to write my own interface (port) for my Domain layer to fully decouple my application from framework?

Should concrete class follow the type hint of its interface?

In this code sample, the interface doesn't seem to care whether the implementing method foo() checks for an array type parameter even if it explicitly type-hinted array only

<?php

declare(strict_types = 1);

interface MyInterface
{
    public function foo(array $foo);
}


class Bar implements MyInterface
{
    public function foo($foo)
    {
        return $foo;
    }
}


echo (new Bar)->foo('test'); // runs just fine as string

I'm expecting at least a fatal, incompatible interface error but there's none.

My questions are:

  1. Is this an expected behavior?
  2. Should the interface not have type hints at all because it's not respect anyway?

vendredi 21 décembre 2018

How to use Memento design pattern to restore multiple states of object

As we know, using memento design pattern, we can restore an object to its previous state. I want to modify this to "restore the object to any of its previous states" depending on the requirement. So, let say there are 5 states of object like this -
S1 -> S2 -> S3 -> S4 -> S5
The current state of object is S5. Now, based on some dynamic requirement, I may need to restore its state back to S2. Can it be done with Memento or there is a different design pattern for this altogether. I am trying to do this in Javascript.

Stripes Border on hover using image

I want to create Stripes Border.

I want to use the img tag or div tag to include the image and the Striped Border.

This is how it needs to look like:

enter image description here

Now I am trying like this with border image as svg.

.feed-item:after {
  background: #0055b9;
  background: url(../images/studentslab_hover_bg.svg);
  background-repeat: no-repeat;
  background-size: 100% 100%;
  padding: 4vw 2.7vw 2vw 2vw;
  width: 104%;
  opacity: 0;
}

.feed-item:hover:after {
  opacity: 1;
  z-index: -1;
}

But in responsiveness, it's not covering full sometimes. So I want to use it like a border. is there any way?

Is this even the correct implementation of abstract factory method in c++? [on hold]

Im currently looking through some lecture notes, the lecturer has provide the following code for abstract factory pattern implementation, however he doesn't provide an interface which is what an abstract factory pattern is. There should be no definition of factory methods for an interface

Concrete product classes:
class inductor;
class resistor;
class circuit {
   void add_inductor(inductor*);
   void add_resistortor(inductor*);
};

Assembler class:
class circuit_simulation {
public:
  circuit* make_LR_circuit(circuit_factory& CF){
      circuit* cct=CF.make_circuit();
      inductor* L=CF.make_inductor(1);
      resistor* R=CF.make_resistor(1);
      cct->add_inductor(L);
      cct->add_resistor(R);
      return(cct);
}; 

Abstract factory base class:
class circuit_factory {
public:
  circuit_factory();
  virtual circuit* make_circuit() const {return(new circuit);}

  virtual resistor* make_resistor(float R) const {return(new 
resistor(R));}
  virtual inductor* make_inductor(float L) const {return(new 
inductor(L));}
};

Main code:
main(){
 circuit_factory CF;
 circuit_simulation sim;
 circuit* cct=sim.make_LR_circuit(CF);
// The circuit “contains” objects of 
// the type inductor, resistor etc
    }

Difference Between State and Strategy Design Pattern

I know it has been asked before but there is no clear answer to it , every one just explains each pattern without telling an actual difference

Is there any actual difference beside the intent and that a state cab preform a state transition in the state pattern

    interface IA{
      void A1();
    }

    class SubA1 : IA
    {
      public void A1()  {}
    }

    class SubA2 : IA
    {
      public void A1() {  }
    }

    class SubA3  : IA
    {
      public void A1() {  }
    }

public class Context{

  private IA _stateOrAction;

// i could be injecting a strategy algorithm or setting initial state 
  public Context(IA stateOrAction)
  {
    _stateOrAction= stateOrAction;
  }

// i could be changing a strategy algorithm or chaining current state    
  public void Set_IA(IA stateOrAction)
  {
    _stateOrAction= stateOrAction;
  }

  public void DoWorkDependingOnStateOrAlgorithm(object iCouldBeHereOrNot)
  {
    // just ignore that this is not same signature as interface
    _stateOrAction.A1(iCouldBeHereOrNot);
  }

}

the code above taken from a github repo which explained one of the two patterns ,
after putting a generic method and class name which hides the intent like this , is it still possible to till the difference , specially if we removed the Set_IA

What is the benefit of not returning anything for commands in the CQS pattern?

It's clear to me that performing some kind of modification to the underlying data when I do a "query" in a class would be misleading, like making getMyValue() do some kind of side effect. That would be difficult to reason about. But if I do some kind of update like updateModel, I don't see the problem with having it return a query, such as returning the correct state of an entity. I would assume that an error in the update would cause an exception to be thrown. Not returning any values just makes extra work to do a query.

What regrets would I have if I return values with commands in CQS?

Why do I get a foreach compiler error even though Iterable is implemented?

I'm trying to learn different design patterns in OOP and the current one I'm learning is iterator pattern. Therefore I have made two own interfaces (Iterable and Iterator).

I'm trying to iterate over List<Person> friends. But the row: for (Person p : p1) gives the following compiler error:

foreach not applicable to type 'com.company.Person'

Which to me makes no sense since I've implemented Iterable and overrided the iterator() method as far as I can see.

Can someone tell me what I'm missing?

Here is my code:

Main class:

Person p1 = new Person("Erik");
    p1.addFriend("Lars");
    p1.addFriend("Jenny");
    p1.addFriend("Janne");


    for (Person p : p1) {
        System.out.println(p.name);
    }

Iterator:

public interface Iterator<T> {

    boolean hasNext();

    T next();

    void remove();
}

Iterable:

public interface Iterable<T> {
    Iterator<T> iterator();
}

Person:

public class Person implements Iterable<Person>{
    private List<Person> friends = new ArrayList<>();
    String name;
    int index = 0;
    public Person(String name){
        this.name = name;
    }

    public void addFriend(String name){
        friends.add(new Person(name));
    }

    @Override
    public Iterator<Person> iterator(){
        return new Iterator<Person>() {
            //int index = 0;
            @Override
            public boolean hasNext() {
                System.out.println(index);
                return index < friends.size();

            }    

            @Override
            public Person next() {
                if(hasNext()){
                    return friends.get(index++);
                }
                else{
                    return null;
                }
            }
            @Override
            public void remove() {
                if(index<=0) {
                    friends.remove(index--);
                }
            }
        };
    }
}

Converting between protobuf messages

I have two protobuff models, called Car and MotorCar, too big to share. I receive one message in the Car format and I need to convert it to the MotorCar format and send it on. They contain some similar fields and nested objects with same names and some which are completely different i.e MotorCar has a concept of Engine, car does not but I need to look it up to provide it, based on the car model. I am unsure what approach to take for doing this work. Do I just use a mapper and fill out the extra objects that way:

public class Mapper {

    public MotorCar from (Car carMessage) {
        MotorCar.Builder motorCar = MotorCar.newBuilder();
        motorCar.setModel(carMessage.getModelName());
        ...
        motorCar.setEngine(getEngine(carMessage.getModelName()))
        return motorCar.build()
    }

    private Engine getEngine(String model) {
        ...
    }
}

My issue with this is that it will create a massive class with lots of setters. I have looked at the adapter pattern but I am unsure how to implement it for this scenario of protobuff messages without an interface class.

Any help appreciated.

Thanks

Object oriented design. What better?

I have a Printer class that should print a number and text. The number never changes for each Client class. I have more Client objects with different number values.

What design is better?

In the sample1 the number sends to print() method as argument, therefore all Client objects use single Print object. In the sample2 the number sends to the Printer constructor, therefore each Client object have own Printer object.

Please help me figure it out.

UML diagram

Looking for a better approach to store a list of dynamically created objects in javascript

I have an object let's name it Passengers which contains a list of names according to their default rank. Eg. -

Passengers {
       P1 : {Ticket Price:<x>, Distance:<x> ---},
       P2 : {Ticket Price:<x>, Distance:<x>---},
       |
       |
       Pn : {Ticket Price:<x>, Distance:<x>---}
      }

Now Based on some input, I am changing the ranking of the list of passengers & storing them in a new object. The input will be decided dynamically by the user. Like user can say I want to rank on the basis of ticket price, distance etc.
The catch here is user can go & come back to any ranking state at any time. Eg. - User first asks to rank by distance, then asks to rank by ticket price, then again asks to rank by distance. In this kind of scenario where user again asked to rank the list by distance , I don't want to recalculate the ranking logic as it's a costly affair. The ranking criteria can be many & are also decided dynamically. The most straightforward approach for reusability would be to store these objects in a Map where key is ranking criteria & value is its respective object.
Is there any design pattern or a better way in Javascript through which it can be achieved in a cleaner way. I went through Object state & Memento design pattern but these are not making much of sense for this requirement.
Any suggestions.

Django multi-table/concrete inheritance alternatives for basic data model pattern

tl;dr

Is there a simple alternative to multi-table inheritance for implementing the basic data-model pattern depicted below, in Django?

Premise

Please consider the very basic data-model pattern in the image below, based on e.g. Hay, 1996.

Simply put: Organizations and Persons are Parties, and all Parties have Addresses. A similar pattern may apply to many other situations.

The important point here is that the Address has an explicit relation with Party, rather than explicit relations with the individual sub-models Organization and Person.

diagram showing basic data model

This specific example has several obvious shortcomings, but that is beside the point. For the sake of this discussion, suppose the pattern perfectly describes what we wish to achieve, so the only question that remains is how to implement the pattern in Django.

Implementation

The most obvious implementation, I believe, would use multi-table-inheritance (a.k.a. concrete inheritance):

class Party(models.Model):
    """ Note this is a concrete model, not an abstract one. """
    name = models.CharField(max_length=20)


class Organization(Party):
    """ 
    Note that a one-to-one relation 'party_ptr' is automatically added, 
    and this is used as the primary key (the actual table has no 'id' 
    column). The same holds for Person.
    """
    type = models.CharField(max_length=20)


class Person(Party):
    favorite_color = models.CharField(max_length=20)


class Address(models.Model):
    """ 
    Note that, because Party is a concrete model, rather than an abstract
    one, we can reference it directly in a foreign key.

    Since the Person and Organization models have one-to-one relations 
    with Party which act as primary key, we can conveniently create 
    Address objects setting either party=party_instance,
    party=organization_instance, or party=person_instance.

    """
    party = models.ForeignKey(to=Party, on_delete=models.CASCADE)

This seems to match the pattern perfectly. It almost makes me believe this is what multi-table-inheritance was intended for in the first place.

However, multi-table-inheritance appears to be frowned upon, especially from a performance point-of-view, although it depends on the application. Especially this scary, but ancient, post from one of Django's founders seems cause for discouragement:

In nearly every case, abstract inheritance is a better approach for the long term. I’ve seen more than few sites crushed under the load introduced by concrete inheritance, so I’d strongly suggest that Django users approach any use of concrete inheritance with a large dose of skepticism.

Despite this scary warning, I guess the main point in that post is the following observation regarding multi-table inheritance:

These joins tend to be "hidden" — they’re created automatically — and mean that what look like simple queries often aren’t.

Alternatives

Abstract inheritance does not seem like a viable alternative to me, because we cannot set a foreign key to an abstract model, which makes sense, because it has no table. I guess this implies that we would need a foreign key for every "child" model plus some extra logic to simulate this.

As another alternative, it is often suggested to use explicit one-to-one relations (eoto for short, here) instead of multi-table-inheritance (so Party, Person and Organization would all just be subclasses of models.Model).

Both approaches, multi-table-inheritance (mti) and explicit one-to-one relations (eoto), result in three database tables. So, depending on the type of query, of course, some form of JOIN is often inevitable when retrieving data.

By inspecting the resulting tables in the database, it becomes clear that the only difference between the mti and eoto approaches, on the database level, is that an eoto Person table has an id column as primary-key, and a separate foreign-key column to Party.id, whereas an mti Person table has no separate id column, but instead uses the foreign-key to Party.id as its primary-key.

Question(s)

I don't think the behavior from the example (especially the single direct relation to the parent) can be achieved with abstract inheritance, can it? If it can, then how would you achieve that?

Is an explicit one-to-one relation really that much better than multi-table-inheritance, except for the fact that it forces us to make our queries more explicit? To me the convenience and clarity of the multi-table approach outweighs the explicitness argument.

Note that this SO question is very similar, but does not quite answer my questions. Moreover, the latest answer there is almost nine years old now, and Django has changed a lot since.

[1]: Hay 1996, Data Model Patterns

Java alternative of bad looking if-else or switch constructions

Looking for modern way to realise String translation to replace bad looking if-else or switch constructions:

if ("UK".equals(country)) 
     name = "United Kingdom";
  if ("GE".equals(country))
     name = "Germany";
  if ("FR".equals(country))
     name = "France";
  if ("IT".equals(country))
     name = "Italy";
  [...]

or

switch (country) {
      case "UK": name = "United Kingdom"; break;
      case "GE": name = "Germany" break;
      case "FR": name = "France"; break;
      case "IT": name = "Italy" break;
  [...]

jeudi 20 décembre 2018

Minimal element of an array

An abstract question, not related to any particular language:

If I have a function as follows

min(int, int) :: int

which returns the smallest value in an array, and

concat([int], [int]) :: [int]

which combines two arrays, how should I write a function like

minInArray([int]) :: Int

which returns the smallest element in an array, but where the ouput could be chained like so, even with an empty input array:

min(minInArray(array1), minInArray(array2)) == minInArray(concat(array1, array2))

In other words, is there any commonly-used neutral element which minInArray could return on empty input, which wouldn't mess up min()?

How to switch classes with same name and different namespaces?

I have red this article enter link description here

Main idea of the article is cast classes with same name to get more functionality. Its ok, i undestand it. But i cant undestand this code:

repository.query(new NewestNewsesSpecification());

He has NewestNewsesSpecification for sql and NewestNewsesSpecification for Realm.

So main question - how program can undestand what implementation NewestNewsesSpecification to use, if classes are with same names but in different namespaces? Its even not DI framework. Or it just every time i need different stotage i need find and change namespaces in code files?

Code of article in Java. Im writing in C#;

Where to put business logic when using Entity framework and ASP.NET

Typically I have started new projects with a solution containing:
- Web project: contains the mvc, javascript code etc. Makes calls to class library
- Class library1: contains Dbcontext, EF Data model, a class with CRUD methods to interface with Db via the DbContext and various "utility" methods
- Class library2: contains only POCO classes. This library is referenced by both the web project and library1

Ok, that works well, but when the amount of "business logic" starts to increase, this gets kinda messy, since I start putting in more rules that the business gives you. Makes me think there needs another "layer" or library where we put "business logic" that really is above/beyond just getting a data returned as a filtered list of POCO objects. Things such as checking attributes of orders based on some rules defined by some group within the business.

My question then is: Would you force every call from the client layer to go through the business library (see image below case #2), even for simple cases where you just need a simple list of lookup values of some sort?

enter image description here

Using query builders as data mapper layer

Can you use a query builder like (Doctrine DBAL, Aura.SqlQuery, etc) as your data mapper layer or do you need to abstract it further? E.g.,

Sample A

<?php

class PersonsRepository
{
    private $queryBuilder;

    public function __construct(QueryBuilder $queryBuilder)
    {
        $this->queryBuilder = $queryBuilder;
    }

    public function getName($id)
    {
        $row = $this->queryBuilder
                    ->select('persons')
                    ->where('id', $id);

        return $row['name'];                
    }
}

// example usage

$pdo = new PDO('...');
$repo = new PersonsRepository(
            new QueryBuilder($pdo));

Is that already ok or do you need to abstract it further like this:

Sample B

class PersonsRepository
{
    private $mapper;

    public function __construct($mapper)
    {
        $this->mapper = $mapper;
    }

    public function getName($id)
    {
        $row = $this->mapper->find([            
            'id' => $id
        ]);

        return $row['name'];                
    }
}

class PersonMapper
{
    private $queryBuilder;

    public function __construct(QueryBuilder $queryBuilder)
    {
        $this->queryBuilder = $queryBuilder;
    }

    public function find($id)
    {
        $row = $this->queryBuilder
                    ->select('persons')
                    ->where('id', $id);

        return $row;
    }
}

$pdo = new PDO('...');
$repo = new PersonsRepository(
            new PersonMapper(
                new QueryBuilder($pdo)));

If we go with the definition of data mapper

A layer of Mappers (473) that moves data between objects and a database while keeping them independent of each other and the mapper itself.

In essence, a query builder is a layer in between the repo and the db. That said, is Sample A considered to be using a data mapper already or does it have to be explicit, like Sample B?