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 4 years ago.
Improve this question
So I was wondering the other day, where to put private methods, that do some dirty work in Web API.
I need to extract certain things from JSON, methods will do the job and return some result.
So where do I keep those methods?
do I need to write a separate library (dll)?
or just do this stuff in the controller?
I don’t think it can have a single answer – it depends on…
If you think this private method can be reused from some other controller in future, better to have a separate class, if you think it can be reused from separate modules (not just from controllers), a separate class library project can be the answer.
But if you consider this private method is designed to support for a specific action of a controller, you can write within controller, before taking any decision a few more parameters to be considered like unit testability or slimness of API etc.
You should keep your controller thin as much as possible. You can move logic code to service package, for example JSONService class or JSONLib.
I often use following layout:
controller/
lib/
service/
model/
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 2 years ago.
Improve this question
I'm new to Symfony and I was really surprised when I saw that very important parts of program logic are "hidden" in the annotations. Or actually in PHP comments. I understand, these annotations are used by the libraries, but is not it a bad solution overall - couldn't it be made differently? What is the logic behind that? From my prospective they make the code hard to understand, they are not processed by atom (or can I install some plugin for this?..), and it's overall strange idea. No?)
All That configuration can be stored in php/xml/yml files.
From my experience the idea of annotation is very convenient.
For example, you look at controller , and you see route (link to call controller action), data passed ( Parameter Converter).
If it is done right, you have all the important data in one place, just above your code.
Processing annotation by IDE it's a different story.
Check if there is any atom plugin for this , or change IDE that can handle this.
Personally, i'm using phpstrom , and i don't have any issue with annotations
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 have this application where requests are filtered based on the string in the struct and made to execute different functions.
My approach is to have a Map which maps the strings to the function pointers and execute them. However this approach is being contended by a teammate who wants to do this filtering by reflection. We are using Go and it is for monitoring the activity of our site.
Teammates approach: Use reflection to switch the object based on the string, pass the string to the function and let the function call the relevant function.
My approach: Simple map from string to functions
Any help is appreciated.
Downside of reflection based auto-discovery is that you forever have to be careful on what you add to the system because it can be automatically picked up.
vs map approach where you would need to explicitly expose each function.
Reflection is more cool and auto-magical, for sure. But auto-magical doesn't lend it self very well to security or long term maintainability.
Plus, a map[string]func(with specific signature) won't compile if you attempt register a non-matching function.
Where you will find stuff with reflection and then ponder why it's a runtime fail.
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 8 years ago.
Improve this question
I have:
UI layer
Business Layer
Data access layer
Common code layer
When I need to, for example, perform string manipulation that is unique to the application and isn’t a candidate for common code. Where would you place this function?
Currently I have it in the controller but is does not feel right.
I would move it out of the controller but keep it within the MVC application, as it sounds like presentation logic. It could be a helper or maybe an extension method.
I would recommend you to make Common.UI, which will store all common things for Pressentation Layer in a separate project. In the future it will help you to switch between different pressentation layers and use same common features.
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 8 years ago.
Improve this question
I am using ASMX web services.
I have two functions so, should I create two functions in a single ASMX or should I create separate ASMX for each function?
Does that impact performance? Which choice will have the highest performance?
With all things performance you need to profile it before making changes to increase performance otherwise you could end up optimizing the wrong thing.
Most of the times the Pareto principle applies, a small portion of code or a few modules in the entire application are responsible for most of the execution time. Making optimizations there will have the greatest impact on performance.
Have you optimized everything that could be optimized and drawn the conclusion that the service endpoint can cause performance issues?
You should write the code how it's easier to maintain. Do those two functions belong together or are they completely unrelated? Does it make sense to have them exposed through one ASMX or two? That should be your criteria for how to define your endpoints.
My guess is that both choices will have similar performance but if you absolutely need to know build them both ways, profile them, and see which one performs better.
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 8 years ago.
Improve this question
In Symfony2 (could apply to other frameworks too) are there any factually based best practices defined for when to put a small chunk of reusable code in a trait as oppose to a service (or even in a model)?
e.g. scenario:
I had a small piece of code which modified a url slightly (added a specific port based on the current environment and machine). It was used by several controllers. I was told this would be best in a trait as a service would be overkill because it was only a few lines of code and there was only one parameter being passed to the function (the url).
Symfony2 manual defines a service as:
"a generic term for any PHP object that performs a specific task."
There may be some subjective opinions here, but the question guidelines say:
Great subjective questions inspire answers that explain “why” and
“how” and Great subjective questions insist that opinion be backed up
with facts and references
And that is what I am looking for. Factual reasons why or why not to use a trait over a service for a small chunk of code.
Update
A great suggestion has been: traits aren't easy to test whereas services are