Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a method that depends on many other classes like this
public function getProfileData(
ProfilesService $profile_service,
ContactInfoService $contact_info_service,
CoursesService $courses_service,
InterestsService $interests_service,
LanguagesService $languages_service,
PersonalInfoService $personal_info_service,
ProjectsService $projects_service,
SkillsService $skills_service,
AwardsService $awards_service,
EducationsService $education_service,
ExperiencesService $experiences_service,
TargetJobsService $target_jobs_service,
ProfileHiddenSectionsService $hidden_sections_service) { }
I read about dependency injection and I know that if you exceed 6-10 dependencies it leads you to a code smell.
But this getProfileData() method really need all of these dependencies so what is the best practice to solve this problem ?!
The code smell you are experiencing is called Constructor over-injection (and this particular variation is Method over-injection). As #Nkosi said in the comments, the source of this is a Single Responsibility Principle violation.
How to solve this problem, however, depends pretty much on the situation. Chapter 6 from the book Dependency Injection: Principles, Practices, and Patterns actually contains a very elaborate description of your options. In short, among other things, you can use the following refactorings:
Facade Services
Introduce Domain Events
Hide similar components behind a Composite
Extract cross-cutting concerns using Decorators
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I just learned about Laravel Service Containers and it seems like a great functionality.
as I understood it prevents us from rewriting 50 controllers when we should modify a commonly used entity or variable throughout the project.
the problem being is that I don't see a proper Use case for this feature, I mean if you have a piece of data or an entity that you're repeatedly using: this can be customized through a model
so when should I use service containers in laravel ?
what are the Pros and cons of this functionality ?
as I understood it prevents us from rewriting 50 controllers when we should modify a commonly used entity or variable throughout the project.
I do not believe you understand it correctly.
Service Containers are just a fancy term that Laravel came up with to describe dependency injection. The major benefit is for unit testing and its biggest competitor is the facade pattern that Laravel also uses. The biggest benefit of dependency injection is that you can mock expectations without requiring additional "scaffolding" code that bootstraps the test. More information about using dependency injection for unit testing: https://medium.com/philipobenito/dependency-injection-as-a-tool-for-testing-902c21c147f1
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I know very little about design patterns out there. In fact i never worked with one yet, as i always went for raw coding. But i think its time to enrich my knowledge on design patterns out there. Specially i want to know more about Factory, Singleton & Strategy design patterns. I googled about them of course but I still not clear about their differences, how to implement them etc.
If anyone can suggest me some good document where i can read much more, that would be very much helpful.
Thanks in advance for helping.
https://sourcemaking.com/design_patterns is a very helpful website, with a lot of explanations and code samples, including PHP ones. I added very short summaries in my own words below. Disclaimer: because they summaries are very short, the may not be very accurate, but give you an idea on how the patterns compare.
Factory Method: https://sourcemaking.com/design_patterns/factory_method
In short: you have a separate class that is responsible for creating instances of a certain class. This is to make sure that a class is always constructed 'in the right way'.
Singleton Pattern: https://sourcemaking.com/design_patterns/singleton
In short: only one instance of a singleton class is possible, the class itself has a static class variable that stores the instance, and a static method that returns the stored instance, or create one if it is not yet created.
Strategy Pattern: https://sourcemaking.com/design_patterns/strategy
In short: if there are multiple ways to solve some problem, provide a set of classes that each contain one implementation to the problem and let the client decide which implementation to use.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Background : in PHP 5.4 there are now traits which allow you to composite classes in a way that you can re-use those 'mixins' anywhere else too. That is being widely used in horizontal programming and comes with important benefits : reducing code bloat.
Question :
how to emulate the very same behavior on lower PHP versions by not going over the decorator pattern and static code generators ?
Requirements: the solution should work as you would expect from a language with proper multiple inheritance; also member-variable override in the order you've defined in the list of base-classes.
Solution(s) I have found so far :
The only real candidate at hand I've found so far is the 'class-mixer' over here. The problem with this script is that it partly depends on eval which may has some disadvantages. There are also a few other libraries but non of them really turned well, ie : constructor inheritance
Not really but acceptable results: Code-Generators as in Doctrine and others.
the last solution which came up was to emulate 'multiple inheritance', using the same approaches as in Javascript libraries, ie: implementing the C3 based algorithms.
thanks!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I created this framework to fill what I felt was a need for a PHP-like framework that was really efficient. My goal is to make it into a real open-source project, but since I have never launched a popular open-source project, I could use some feedback and advice.
My question is, has anyone here used it, and if so, what was your experience?
Whether you have or haven't, do you have any advice for the non-programming aspects? What is it missing, in terms of
Documentation
Out of the box ease of use
Community features and support
Whatever else I can't think of right now.
Basically any advice on how I can take it from its current state and make it at least somewhat popular?
http://phponpie.com
I saw this before, but I wasn't sure at all why I should use this instead of Zend Framework or Symfony, so as zerkms said, it seems like just another framework.
The code quality has some question marks about it as well. The code seems to mix and match PHP4 and PHP5 styles (no visibility declaration on some class methods, some class constructors used PHP4-style constructors...) and it had inconsistent file naming (interface iDb in Db.php) and inconsistent coding style, even in the same file.
Not sure how easy it would be to unit tests apps written with this either.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am not trying to create yet another web framework.
For one of the applications I am working on, I want to create a custom framework. I don't want to use any already available framework.
What are the common things to consider?
What should be the architecture?
Thanks :)
If the point of a framework is to make tedious things easy, a good start would be to consider what is tedious.
What are the common things to consider?
Purpose. Usually, when you start building a piece of software, you have a purpose in mind. What will it do that other programs can't?
If you can't answer that question, then take any existing open source framework, change its name and your job is done. Now you have your own framework.
Well if you are going to write a custom framework then I assume the framework needs to be tailored to your needs, otherwise you would use one that is already available. So figure out what your needs are and go from there ;)
What are the most often repeated operations in your application? Is there a division of labor that a framework could make more apparent?