Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components (ie, Report, Dimension, Menu, etc. subclasses) should immutable singletons #6229

Closed
diosmosis opened this issue Sep 16, 2014 · 3 comments
Labels
answered For when a question was asked and we referred to forum or answered it. c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. Task Indicates an issue is neither a feature nor a bug and it's purely a "technical" change.
Milestone

Comments

@diosmosis
Copy link
Member

These classes mainly provide metadata regarding plugin extensibility so there's no reason for them to be instantiated outside of Piwik core, nor is there reason for them to be modified outside of the classes themselves. Thus, they should be immutable and object identity should provide object equality (eg, there should only be one instance of each subclass created). They could potentially be stored in a DI container, however, it should be noted that since they do not need to be configured, they should be instantiated automatically and not be referenced in DI config.

@mattab mattab added c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. Task Indicates an issue is neither a feature nor a bug and it's purely a "technical" change. labels Sep 17, 2014
@mattab mattab added this to the Short term milestone Sep 17, 2014
@tsteur
Copy link
Member

tsteur commented Sep 22, 2014

Angular differentiates between a Service (like Singleton) and Factory (new Instance). Those classes should not be a singleton. If we really need / want a singleton the DI container should handle this.

Otherwise I don't see what the problem is with creating an instance of those classes. It is I think currently only done in core and they usually do not have any state anyway.

@diosmosis
Copy link
Member Author

Didn't get an email for this comment either...

This issue is about comparing component objects. For example, to see if two dimensions are equal (needed by PivotByDimension filter) you have to write:

!empty($lhs) && !empty($rhs) && $lhs->getId() == $rhs->getId()

This is more code than should be necessary. And the nature of these objects demands that there should only be one of them. A Dimension instance holds a description of an analytics dimension. It does not store state that can be altered, it is not manipulated or transformed by any algorithm and it does not provide logic that can be re-used by different types of software. There should not be more than one instance of a Dimension.

To illustrate better, consider scala, which has first class support for singletons (but different than Piwik's Singleton implementation). A dimension in Scala might look like this:

object Keyword extends VisitDimension {
    // dimension setup goes here (this is the constructor)
}

A report for the dimension would look like this:

object GetKeywords extends Report {
    this.module = "Referrers";
    this.action = "getKeywords";
    this.dimension = Keywords;
}

So the PivotByDimension check for a subtable dimension might look like:

val report = GetKeywords,
      pivotByDimension = City; // UserCountry.City
return report.subtableReport.dimension == pivotByDimension;

Which I think is a much better way of expressing the logic.

Of course using Piwik's Singleton base is not the same as a scala singleton and thus using it would be a mistake. DI is what I had in mind when I created the ticket.

@mattab mattab modified the milestones: Mid term, Short term Dec 18, 2014
@mattab
Copy link
Member

mattab commented May 5, 2016

This will be done as part of the UI & Client side improvements project, where we slowly move logic from server side controllers towards client side controllers (AngularJS), and improving our HTTP APIs so that clients (eg. browser) can do everything with such APIs. Some work is already done in Piwik 3.0 branch, for more info see this issue #7131

@mattab mattab closed this as completed May 5, 2016
@mattab mattab added the answered For when a question was asked and we referred to forum or answered it. label May 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered For when a question was asked and we referred to forum or answered it. c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. Task Indicates an issue is neither a feature nor a bug and it's purely a "technical" change.
Projects
None yet
Development

No branches or pull requests

3 participants