I have a question about spring-data module.
I would like to know why you should start the method with 'findXX' when you make a method to look up something.
If you don't have a document that specifies exactly why you set this naming rule, I'd like to look at the discussion history that you've conducted to determine this naming convention.
Related
Laravel encourages us to use snake_case, e.g. first_name, for model attribute names. In particular, when snake case is used to access an attribute from outside the class, it will automatically look for an accessor named getFirstNameAttribute.
When it comes to model relationships however, it seems more natural to use camel case. For example, if a stadium has multiple access points, then the stadium class might have an accessPoints() method. I can call this method as a property ($stadium->accessPoints) to retrieve a list of access points, or I can call it as a method ($stadium->accessPoints()) to get an instance of the underlying query builder.
This is different to how I would normally approach naming conventions. I would normally name attributes using the same case (either snake_case, or camelCase), irrespective of how the attribute is realised.
I am now embarking on a large Laravel project. Should I stick with the two different syntaxes, or am I likely to regret it down the track?
There's no true convention, like in Assassin's Creed's saying,
"Nothing is true, everything is permitted"
Laravel follows the PSR-2 coding standard. Source: https://laravel.com/docs/5.5/contributions
Which redirects us to https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md
(from https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md which says: Code MUST follow all rules outlined in PSR-1.)
And there it says:
Whatever naming convention is used SHOULD be applied consistently within a reasonable scope. That scope may be vendor-level, package-level, class-level, or method-level.
And from that declaration, that's related to separation between environment level conventions, such as database design conventions and code conventions. Most database design conventions encourage snake case column names, and most code conventions use camel case method names, class names etc.
And when defining a model, to declare that, that the model property relates with a database column, the coding convention changes from camelCase to snake_case.
Well, it is a convention, so you should use the approach you are more comfortable with.
I personally use snake_case only on column attributes and camelCase on everything else (class names, relationships ecc).
Note that camelCase for class names and methods is basically necessary, because all laravel and external modules classes and methods use camelCase. If you use snake_case, it will be a mess with some methods implemented by you in snake_case and framework methods in camelCase.
For relationships, you will basically only use relationships defined by yourself, so i believe you can really pick the one you prefer.
Is there any strong reasons to choose one over the other when declaring the mappings for url resources?
#RequestMapping(Mappings.USER)
vs
#RequestMapping("${mappings.user}")
I understand that property files can be modified after deployment, and that might be a reason to keep it in properties if you want it to be changed easily, right? But also I think changing them easily could be undesirable. So for those with experience, which do you prefer, and why? I think a constants file might be easier to refactor, like if I wanted to change the name of a resource I would only have to refactor inside the constants class vs if I refactored properties I would have to refactor in the properties file and everywhere that uses the mapping (Im using eclipse and as far as I know it doesnt have property name refactoring like that). Or maybe a third option of neither and declaring them all as literals inside the controllers?
It all depends on your use case. If you need the change URIs without recompilation, property files is the way to go. Otherwise, constants provide type safety and ease of unit testing that SPEL doesn't. If you're not gonna change or reuse them (for example, same URI for GET and POST is very common), I don't see any need for constants at all.
I would like my first Aspect in a Roo project to run the advice when a web controller starts up. But I cant get the pointcut to match.
The controllers have a class name starting Cfx. I have tried with the following form:
pointcut setBrand() : initialization(Cfx*.new (..));
before() : setBrand()
{
log.info("xxxxxxxxxxxx setting brand");
}
As well as "initialization" I have tried (from the book AspectJ Cookbook) call(Signature) with new keyword, preinitialization, staticinitialization. What is the formula?
Maybe this is related - the Roo aspects do not have this form - no pointcut for example. How are they working? Where is this documented?
Thanks
PS apologies, this is a re-post. I posted this to the Spring Roo forum but got no response. http://forum.springsource.org/showthread.php?129374-Aspect-to-trap-Controller-creation-how-to
I know next to nothing about Roo or Spring, but some AspectJ, so I am going to answer your question from an AspectJ perspective only, assuming that you are an AOP newbie (sorry if my assumption is incorrect):
If you want to do something when a class is loaded, use a staticinitialization(TypePat) pointcut.
If you want to do something when an object (instance) is created, use something like execution(ConstructorPat). The initialization is for special purposes and preinitialization is needed even more rarely. I am assuming that the first one will do for you, not knowing your exact purpose.
Further assuming that something like execution(Cfx*.new (..)) is basically the thing you want, I suggest you look at possible errors or warnings like "advice defined in ... has not been applied [Xlint:adviceDidNotMatch]", because it might just be a pointcut matching issue. Please note that the type pattern you use assumes the matched constructors are in the same package as the aspect and that they have standard visibility (not public or anything else). So unless there is a class-loading issue, maybe you just want to specify more exactly (or more generally) what you want to match. Examples:
com.bigboxco.my_app.Cfx*.new(..)
com.bigboxco..Cfx*.new(..)
public com.bigboxco..Cfx*.new(..)
!private com.bigboxco..Cfx*.new(..)
* com.bigboxco..Cfx*.new(..)
A good strategy could be trying to match one of your constructors by replicating its exact signature and using its fully qualified class name, then working on from that point to make it more general.
Update: I know you can do a web search by yourself, but anyway here are some useful links:
AspectJ quick reference
AspectJ language semantics with topics about signatures, matching etc.
I know naming conventions for tables used by plugins generally start with the name of the plugin and then the model pluralized. For example lets say I had a plugin called Poll, with a model also called PollPoll and another model called PollTag then the resulting table names would be poll_polls and poll_tags. They would also have a habtm relationship so what is the convention for that table name? I believe it would poll_poll_polls_poll_tags, although it is a little redundant it makes sense since the first poll_ represents the name of the plugin, while poll_polls and poll_tags relates to the models.
Also have any naming conventions changed for plugins in 1.3? Is the above stated correct?
Not sure about cake 1.3 (I'm not using it yet), but this if you're right this sounds like a perfectly acceptable case of breaking convention and defining the jointable, and foreign keys in the model relationships and possibly in the plugin.
Why does your plugin require a join table? Seems like a design issue. Perhaps there is a case where this is needed, but if I had a HABTM relation with a plugin, I would add a modelname column to the plugin's table, rather than have to create a new table for each model I wanted to use the plugin.
It's actually not yet a convention that "tables used by plugins generally start with the name of the plugin and then the model pluralized."
The only place that idea is introduced is in an example in the book, which actually says, "it is recommended that you name your plugin controllers something relatively unique in order to avoid namespace conflicts with parent applications ... you might want to be creative with controller names, or prepend the name of the plugin to the classname."
Your Table/Model/Controller/View names must follow normal CakePHP naming conventions, and take reasonable precautions to avoid namespace clash. So it would be perfectly fine to have a "foo_orders" table for a "foo_order" model in plugin Bar.
What are some defining characteristics of a Ruby DSL that separate it from just a regular API?
When you use an API you instantiate objects and call methods in an imperative manner. On the other hand a good DSL should be declarative, representing rules and relationships in your problem domain, not instructions to be executed. Moreover ideally DSL should be readable and modifiable by somebody who is not a programmer (which is not the case with APIs).
Also please keep in mind the distinction between internal and external DSLs.
Internal domain specific language is embedded in a programming language (eg. Ruby). It's easy to implement, but the structure of the DSL is dependent on the parent language it is embedded in.
External domain specific language is a separate language designed with the particular domain in mind. It gives you a greater flexibility when it comes to syntax, but you have to implement the code to interpret it. It's also more secure, as the person editing domain rules doesn't have access to all the power of the parent language.
DSL (domain specific language) is an over-hyped term. If you are simply using a sub-set of a language (say Ruby), how is it a different language than the original? The answer is, it isn't.
However, if you do some preprocessing of the source text to introduce new syntax or new semantics not found in the core language then you indeed have a new language, which may be domain-specific.
The combination of Ruby's poetry mode and operator overloading does present the possibility of having something that is at the same time legal Ruby syntax and a reasonable DSL.
And the continued aggravation that is XML does show that perhaps the simple DSL built into all those config files wasn't completely misguided..
Creating a DSL:
Adding new methods to the Object class so that you can just call them as if they were built-in language constructs. (see rake)
Creating methods on a custom object or set of objects, and then having script files run the statements in the context of a top-level object. (see capistrano)
API design:
Creating methods on a custom object or set of objects, so the user creates an object to use the methods.
Creating methods as class methods, so that the user prefixes the classname in front of all the methods.
Creating methods as a mixin that users include or extend to use the methods in their custom objects.
So yes, the line is thin between them. It's trivial to turn a custom set of objects into a DSL by adding one method that runs a script file in the right context.
The difference between a DSL and an API to me is that a DSL could be at least understood (and verified) if not written as a sub-language of Ruby by someone in that domain.
For example, you could have financial analysts writing rules for a stock trading application in a Ruby DSL and they would never have to know they were using Ruby.
They are, in fact, the same thing. DSLs are generally implemented via the normal language mechanisms in Ruby, so technically they're all APIs.
However, for people to recognize something as a DSL, it usually ends up adding what look like declarative statements to existing classes. Something like the validators and relationship declarations in ActiveRecord.
class Foo << ActiveRecord::Base
validates_uniqueness_of :name
validates_numericality_of :number, :integer_only => true
end
looks like a DSL, while the following doesn't:
class Foo <<ActiveRecord::BAse
def validate
unless unique? name
errors.add(:name, "must be unique")
end
unless number.to_s.match?(/^[-]?\d$/)
errors.add(:number, "must be an integer")
end
end
end
They're both going to be implemented by normal Ruby code. It's just that one looks like you've got cool new language constructs, while the other seems rather pedestrian (and overly verbose, etc. etc.)