Permalink
Browse files

first stab at braining out what I want to say

  • Loading branch information...
1 parent 3d896c3 commit 99df1b2ecf0733c9fa2fbb762688e7b2507a72a8 ben hengst committed Jul 28, 2011
Showing with 90 additions and 0 deletions.
  1. +90 −0 slides.yaml
View
@@ -0,0 +1,90 @@
+- Dependency injection
+- ... in perl.
+# Not a topic that is specifc to Java or any other buzzwordie-'enterprize' solution, you've likely been doing this for a long time.
+- Define
+- Dependency injection (DI) in object-oriented computer programming is a technique that indicates to a part of a program which other parts it can use, i.e. to supply an external dependency, or reference, to a software component.
+# in short DI is a lable from OO land, to refer to a practice of being able to shove in something from the outside.
+
+# ... well that's abstract and super fancy sounding! Let's take a look at a common example:
+
+my $obj = Object->new();
+my $val = $obj->find(this=>'that');
+
+# Lets take a peek under the hood for 'find'
+
+sub find {
+ my $self = shift;
+ $self->d->selectall_arrayref(
+ $self->qbuild( table => 'SomeTable'
+ , @_ # our input
+ )->dbi(Slice=>{});
+}
+
+# ok so all find does is take input, toss off to some "make SQL magic thing" (that we will be avoiding for now) and then calling some 'd' thing that appears to be a DBI object... Lets look further
+
+has d =>
+ is => 'ro',
+ isa => 'DBD::db',
+ default => sub{
+ require DBI;
+ DBI->connect(...)
+ },
+;
+
+# Looks like it's just a simple Moose Attribute, because it is. But the interesting thing here is that it stores an object.
+
+- so?
+# What do we think would happen if we did this:
+
+my $obj = Object->new(d=>DBI->connect(...));
+my $val = $obj->find(this=>'that');
+
+- TADA! that's DI.
+# honestly, this is all that fancy lable means.
+
+# So why is this important?
+## First and foremost this is a way of thinking that will allow you to further re-use your code as you are building patterns for solutions that make only vague assumptions on the details.
+
+# Let's take a look at our find sub again
+
+sub find {
+ my $self = shift;
+ $self->d->selectall_arrayref(
+ $self->qbuild( table => 'SomeTable'
+ , where => {@_} # our input
+ )->dbi(Slice=>{});
+}
+
+# So we have a hardcoded value for the table name, lets fix that because this function now is tightly bound to a specific aspect.
+
+has table =>
+ is => 'ro',
+ isa => 'Str',
+ default => 'SomeTable'
+;
+
+sub find {
+ my $self = shift;
+ $self->d->selectall_arrayref(
+ $self->qbuild( table => $self->table
+ , where => {@_} # our input
+ )->dbi(Slice=>{});
+}
+
+# now again we have a hook that is accessable from the outside to alter the behavor for find
+
+my $obj = Object->new(table => 'OtherTable', d => DBI->connect(...));
+my $val = $obj->find(...);
+
+# So wait a sec... we just created a very basic 'table object' that we can now pass it just about anything and we get what we want... yup Bingo.
+#
+
+
+
+
+# I have a tendancy to use this concept ALOT when testing:
+
+my $test_db = DBI->connect(...),
+my $obj = Object->new(d=>$test_db);
+is $obj->find(...), 'something';
+

0 comments on commit 99df1b2

Please sign in to comment.