Slide 1

Slide 1 text

Dependency Injection in Nameko Matt Bennett ~ Senior Engineer ~ onefinestay

Slide 2

Slide 2 text

Dependency Injection Design pattern whereby an object's dependencies are injected or passed by reference into it, rather than constructed by the object. c l a s s W i d g e t ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f ) : s e l f . s e s s i o n = g e t _ d b _ s e s s i o n ( ) c l a s s W i d g e t ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f , s e s s i o n ) : s e l f . s e s s i o n = s e s s i o n Pretty simple

Slide 3

Slide 3 text

Nameko Python framework for building service- oriented software Bookings Payments Homes Open-source software Active development on GitHub

Slide 4

Slide 4 text

Nameko out of the box Everything you need to do: AMQP RPC AMQP Pub-Sub Bookings Payments Homes

Slide 5

Slide 5 text

Definitions Entrypoints To interact with a service Dependencies For the service to interact with external things

Slide 6

Slide 6 text

Example Service f r o m n a m e k o . r p c i m p o r t r p c f r o m . d e p e n d e n c i e s i m p o r t d a t a b a s e _ c o n n e c t i o n c l a s s H e l l o S e r v i c e ( o b j e c t ) : d a t a b a s e = d a t a b a s e _ c o n n e c t i o n ( D B _ U R I ) @ r p c d e f h e l l o ( s e l f , n a m e ) : s e l f . d a t a b a s e . a d d ( n a m e ) r e t u r n " H e l l o , { } " . f o r m a t ( n a m e )

Slide 7

Slide 7 text

Running HelloService f r o m n a m e k o . c o n t a i n e r s i m p o r t S e r v i c e C o n t a i n e r c o n f i g = { ' A M Q P _ U R I ' : ' a m q p : / / g u e s t : g u e s t @ l o c a l h o s t : 5 6 7 2 / ' } c o n t a i n e r = S e r v i c e C o n t a i n e r ( H e l l o S e r v i c e , . . . , c o n f i g ) c o n t a i n e r . s t a r t ( ) . . .

Slide 8

Slide 8 text

Worker Lifecycle 1. Entrypoint "fires" 2. Worker created 3. Dependencies injected 4. Execute method 5. Discard worker w o r k e r _ i n s t a n c e = H e l l o S e r v i c e ( ) w o r k e r _ i n s t a n c e . d a t a b a s e = H e l l o S e r v i c e . d a t a b a s e . i n j e c t ( ) r e s u l t = w o r k e r _ i n s t a n c e . h e l l o ( n a m e ) d e l w o r k e r _ i n s t a n c e

Slide 9

Slide 9 text

Why do it this way? Simple service logic Complex application f r o m n a m e k o . r p c i m p o r t r p c f r o m . d e p e n d e n c i e s i m p o r t d a t a b a s e _ c o n n e c t i o n c l a s s H e l l o S e r v i c e ( o b j e c t ) : d a t a b a s e = d a t a b a s e _ c o n n e c t i o n ( D B _ U R I ) @ r p c d e f h e l l o ( s e l f , n a m e ) : s e l f . d a t a b a s e . a d d ( n a m e ) r e t u r n " H e l l o , { } " . f o r m a t ( n a m e ) Separation of concerns

Slide 10

Slide 10 text

Unit Testing Testing a service in isolation f r o m m o c k i m p o r t c a l l f r o m n a m e k o . t e s t i n g . s e r v i c e s i m p o r t w o r k e r _ f a c t o r y h e l l o _ s v c = w o r k e r _ f a c t o r y ( H e l l o S e r v i c e ) a s s e r t h e l l o _ s v c . h e l l o ( " M a t t " ) = = " H e l l o , M a t t " a s s e r t h e l l o _ s v c . d a t a b a s e . a d d . c a l l _ a r g s _ l i s t = = [ c a l l ( " M a t t " ) ] f r o m n a m e k o . t e s t i n g . s e r v i c e s i m p o r t w o r k e r _ f a c t o r y d a t a b a s e = s e t ( ) h e l l o _ s v c = w o r k e r _ f a c t o r y ( H e l l o S e r v i c e , d a t a b a s e = d a t a b a s e ) a s s e r t h e l l o _ s v c . h e l l o ( " M a t t " ) = = " H e l l o , M a t t " a s s e r t d a t a b a s e = = { " M a t t " }

Slide 11

Slide 11 text

Integration Testing Testing a service with something else Host services the "normal" way Restrict scope of interaction through dependencies

Slide 12

Slide 12 text

Replace Injections f r o m n a m e k o . t e s t i n g . s e r v i c e s i m p o r t r e p l a c e _ i n j e c t i o n s c o n t a i n e r = S e r v i c e C o n t a i n e r ( B o o k i n g S e r v i c e , . . . ) p u b l i s h e r _ m o c k = r e p l a c e _ i n j e c t i o n s ( c o n t a i n e r , " h o m e s _ p u b l i s h e r " ) c o n t a i n e r . s t a r t ( ) . . . Bookings Payments Homes

Slide 13

Slide 13 text

Worker Lifecycle All dependencies participate worker setup worker result worker teardown

Slide 14

Slide 14 text

Nice Examples Log Aggregator Sentry Reporter Statsd Monitor

Slide 15

Slide 15 text

Summary Dependency Injection Simple idea Separation of concerns Nameko Encourages explicit dependencies Provides nice testing tools And other goodies... Nameko Workshop -- Tomorrow 10:20, MADE room

Slide 16

Slide 16 text

Thanks! Questions? https://github.com/onefinestay/nameko P.S. onefinestay are hiring...