Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency Injection in Nameko

Matt Bennett
September 21, 2014
1.4k

Dependency Injection in Nameko

Nameko is a python framework for building service-oriented software, developed by onefinestay. This talk explains how nameko uses Dependency Injection, why it's an important paradigm and how it simplifies writing services and testing them.

More info at https://github.com/onefinestay/nameko

Matt Bennett

September 21, 2014
Tweet

Transcript

  1. 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
  2. Nameko Python framework for building service- oriented software Bookings Payments

    Homes Open-source software Active development on GitHub
  3. Nameko out of the box Everything you need to do:

    AMQP RPC AMQP Pub-Sub Bookings Payments Homes
  4. 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 )
  5. 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 ( ) . . .
  6. 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
  7. 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
  8. 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 " }
  9. Integration Testing Testing a service with something else Host services

    the "normal" way Restrict scope of interaction through dependencies
  10. 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
  11. 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