DEFINITION OF ELOQUENT DESIGN CODENB: I’ve omitted a lot of code here, just to demonstrate my point I didn’t need it class User extends Model The reason I love it, is because what you actually do is group those few responsibilities in an entirely sensical and logical place, you end up with a class which can give you lots of information all of those responsibilities can be so beautifully wrapped as so you end up with something like this: But, if you love SRP, how can you like the Eloquent Model, you heathen? So, let’s be honest, it rips the Single Responsibility Principle apart, but I’m totally okay with that. The power of the Eloquent Model is that it actually does more than that, it also defines relationships etc. That means it has, at least, two responsibilities: Executing queries against a table, and representing a record. You can’t really say “it is responsible for interacting with a specific table” because it is also representational of a record. The big problem people have with Eloquent is that the Model class fulfils multiple responsibilities. So then how does the Eloquent Model break it? That level of abstraction and obscurification will only ever be bad for managing and debugging code. The latter approach to single responsibility is most definitely an anti-pattern. Do I need a class for executing SQL queries, and one for generating them? Maybe that means I need a class which prepares INSERT statements, another for UPDATE, and so on and so forth. So, let’s look at the other end of the spectrum and go into tiny, tiny detail. If you defined the single responsibility of a class as “do the business logic” then you could have a God class, which technically has a single responsibility. However, a single responsibility has to be defined. It makes sense, separate things into logical blocks, and if each class has its own responsibility (and only the one) then it’s quite easy to find how the classes are supposed to work together, perfick. This is another slight issue I have with the Single Responsibility Principle. Single Responsibility is pretty subjective Or means your profit margin is higher if you’re running your own business, freelancing or contracting. Ultimately maintainable code which is easy to extend is cheaper for the business whose employing you. DEFINITION OF ELOQUENT DESIGN SOFTWAREI joked earlier about principles being like the Pirate’s Code, but one does have to be careful when using design patterns and principles, if we’re not careful they become anti-patterns which essentially means their over usage and religious subscription actually causes more problems in the software than it would’ve done had it have been omitted completely.Īll patterns should be used in a way which makes sense and makes the code more efficient and maintainable. I’d like to make my first point, which quite simply is: The Single Responsibility Principle is a principle. So, now that the introductions are out of the way the Single Responsibility Principle isn’t mandatory I can’t sell Laravel well enough, honestly it’s amazing. It basically gives you all of the tools you need to build bespoke software, but in a way that means you can do it at lightening speed, with crazy accuracy. Laravel: Is a framework for Rapid Application Development, which, like Eloquent, is amazing. This is quite handy for programming as you can store all of the logic to do with saving in the Model. DEFINITION OF ELOQUENT DESIGN UPDATELike all principles or methodologies they’re like the Pirate’s Code, they’re more like guidelines than hard and fast rules of software design.Įloquent: Is an ORM (object relational model) which comes as standard with Laravel, and is absolutely fantasticĪctive Record: Is a design pattern whereby a record from a database table is returned as an object that can be manipulated and can update the database as part of its role. Usually this means you can do something like $myRecord = RecordFactoryOrSomething::getTheRecord() SOLID Principles: I’ll do an article on this one day, but it’s a discussion for another time. The idea is to keep your code modular and easy to switch out. In short, this means one class should have one job. Single Responsibility Principle : The “S” in the “SOLID” principles. I thought I would discuss a controversial one today, in order to give an articulated response to a common argument about Eloquent (and generally the Active Record pattern)įirstly, just in case you don’t know, here are some definitions:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |