The post belongs to NectarCommerce and Extension Framework Awareness
Extension Framework Game Plan
Introduction to Metaprogramming
Ecto Model Schema Extension
Ecto Model Support Functions Extension
- Phoenix Router Extension
- Phoenix View Extension
- Extension Approach Explained
- Developer Experience and Workflow developing Favorite Product Extension
- Developer Experience and Workflow testing Favorite Product Extension
What will be NectarCommerce
Off-the-shelf Opensource E-commerce application for building an online store.
Provides an Extension Framework to support features not included in core as extensions.
Strives for unobtrusive parallel development of NectarCommerce and Extensions
NectarCommerce is committed to providing a ready-to-use e-commerce solution but the definition of 100% is different under different business domains.It aims to solve common use-cases as part of the project and relying on extension framework to tap the rest.
Phoenix Router Extension
Note:This blog post is very similar to Ecto Model Schema Extension
, so if you are already familiar with it, you can jump straight to the
We want to allow Extensions to add routes into Nectar Router without modifying the Nectar Router source.
Minimum three parts are needed to create & use an extension effectively:
- Library Code
- Service Code
- Consumer Code
An extension and its use with Nectar can be viewed as Producer / Consumer relationship bound by a communication protocol.
Extensionwhich wants to add a route, say a list of favorites, is a Producer (Service Code)
Nectar Routeris a Consumer (Consumer Code)
allowing the route additions through a communication protocol (Library Code)
Let’s begin the journey of incremental changes to bring consumer, service and library code into existence starting from a simple use-case of adding a route for showing favorites.
Note: Please refer Introduction to Metaprogramming
for more information on Metaprogramming in Elixir
A straightforward way to add favorites route in Nectar.Router would be adding it directly in Nectar.Router, see full version here
, but it requires change in Nectar source. Let’s move to next step for avoiding any modification to Nectar.Router
We can add a function to Nectar to which other extensions can delegate the responsibility of route additions, see full version here
. See Nectar.ExtendRouter example below on how to use it
Now, with delegation function
in place, we can work towards providing a way to register the routes to be added, see full version here
. Please check the usage of Module attributes for same below.
Earlier, Module.put_attribute need to be used multiple times to define multiple routes whereas now we wrapped it in an anonymous function to encapsulate the collection of routes through a simple and consistent interface, see full version here
. There can be multiple extensions used for different functionality and hence multiple routes need to be registered and defined
Now, Nectar.ExtendRouter is getting cluttered with ancillary method definitions. Let’s move it out to another module and use it, see full version here
Let’s further reduce the boilerplate of registering defined_routes module attribute and importing define_route method definition with using
callback, see full version here
Reference of defined_routes Module attribute is scattered across Nectar.RouterExtender and Nectar.RouterExtension so let’s move it out to Nectar.RouterExtension to consolidate the usage via
and definition together, see full version here
With above changes, it’s now possible to define routes any number of times, see full version here
. Also, routes can now be added using
in Nectar.ExtendRouter without making any changes to Nectar.Router.
Check all the revisions at once, here
Now, in the final version
, you can easily find the three components, consumer, service and library code
, as desired in extensible system
Our aim with these posts is to start a dialog with the Elixir community on validity and technical soundness of our approach. We would really appreciate your feedback and reviews, and any ideas/suggestions/pull requests for improvements to our current implementation or entirely different and better way to do things to achieve the goals we have set out for NectarCommerce.
Enjoy the Elixir potion !!