Skip to content

Project Structure

Ashish Singh edited this page Apr 27, 2018 · 1 revision

Accessing Domain, Model and Schema

  • Snitch has a following contextual design to maintain hierarchy
Domain
   |-- Model
        |-- Schema

Schema

It's a general convention being followed that, while designing a schema for a particular entity the following should be kept in mind:

  • The module should have entity Schema
  • The module should have the changesets, which can be broken down into create and update changesets.
  • The module should also contain validation functions for specific attributes of the entity.
  • Some general validations which are common among entities should be placed inside snitch_core/lib/tools/validations.

Model

  • The model should include all the functions which require to touch the database directly.
  • The model functions should use QueryHelper for common functions such as create, update, get and delete.
  • All the functions which involve a custom queries to the database should also be included in this module.
  • For accessing schema inside model the convention is to use the schema name prepended with Schema. or appending schema name with Schema. Please do not use any other convention to maintain consistency across the project.
Example
 alias Snitch.Data.Schema
 alias Snitch.Data.Schema.Variant, as: VariantSchema
      
 # accessing inside function 
 def some_func() do
    Schema.Variant.changeset()
 end
 
 def some_other_func() do
    VariantSchema.changeset()
 end

Domain

  • The domain module for any entity should utilise the model functions to do any transaction with the database. Please avoid using any Repo related queries in the domain functions, instead they should be delegated to the model for the same entity.
  • Accessing Schema inside domain module should be avoided at all costs to maintain the abstraction. Only in very rare scenarios this would be acceptable. The access should always be delegated to the concerned model.
  • Again for accessing models or in rare cases schema inside domain the model name should be prepended with Model. similarly, for schema it should be Schema. or appending them with Model and Schema respectively.
Example
 alias Snitch.Data.Model
 alias Snitch.Data.Model.Variant, as: VariantModel
      
 # accessing inside function 
 def some_func() do
    Model.Variant.changeset()
 end
 
 def some_other_func() do
    VariantModel.changeset()
 end
  • Even the use of a domain functions anywhere should be done in a manner similar to above e.g.
alias Snitch.Domain

def some_func() do
  Domain.Variant.some_call()
end
Clone this wiki locally