{"_id":"553d394e9660ba0d00569d20","user":"543466ea0e8e2b0e00341818","__v":0,"version":{"_id":"553d394d9660ba0d00569d07","project":"543467200ef9c00800164ecc","forked_from":"54bedb2fffb79c320030db1f","__v":1,"createdAt":"2015-04-26T19:15:25.245Z","releaseDate":"2015-04-26T19:15:25.245Z","categories":["553d394d9660ba0d00569d08","553d394d9660ba0d00569d09"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"0.11.2","version":"0.11.2"},"category":{"_id":"553d394d9660ba0d00569d08","pages":["553d394e9660ba0d00569d13","553d394e9660ba0d00569d14","553d394e9660ba0d00569d15","553d394e9660ba0d00569d16","553d394e9660ba0d00569d17","553d394e9660ba0d00569d18","553d394e9660ba0d00569d19","553d394e9660ba0d00569d1a","553d394e9660ba0d00569d1b","553d394e9660ba0d00569d1c","553d394e9660ba0d00569d1d","553d394e9660ba0d00569d1e","553d394e9660ba0d00569d1f","553d394e9660ba0d00569d20","553d394e9660ba0d00569d21","553d394e9660ba0d00569d22","553d394e9660ba0d00569d23"],"project":"543467200ef9c00800164ecc","version":"553d394d9660ba0d00569d07","__v":1,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-15T18:00:23.862Z","from_sync":false,"order":0,"slug":"glim","title":"GLIM"},"githubsync":"","project":"543467200ef9c00800164ecc","metadata":{"title":"","description":"","image":[]},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-15T18:11:42.546Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"never","params":[],"url":""},"isReference":false,"order":11,"body":"The concept of Facade is there to hide abstractions from core classes. In glim, facades can be used to hold single instances of objects that can be used everywhere during the runtime.\n\nConsider an example of a class namely \"Database\". A Database class example would be the following;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# database.py\\nclass Database:\\n\\n  \\tdef __init__(self, config):\\n      \\tself.config = config\\n        self.connection = None\\n  \\n  \\tdef connect(self):\\n\\t\\t\\t\\t# perform database connection using config\\n        self.connection = connect_to_db()\\n    \\n    def query(sql):\\n      \\t# perform database query execution using the db connection\\n        return self.connection.execute_sql(sql)\\n      \\t\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nThis example is a primitive db connection library, so it won't reflect the real world. It is just given to teach the concept of Facades. In this example, you can instantiate an object of Database by the following;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# some_script.py\\nfrom database import Database\\n\\nconfig = {\\n\\t\\t'host': 'localhost',\\n    'port': 3306,\\n  \\t'user': 'root',\\n  \\t'password': 'pwd'\\n}\\n\\ndb = Database(config)\\ndb.connect()\\n\\nsql = \\\"SELECT 1 FROM test.hello\\\"\\n\\nprint(db.query(sql))\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nIt might be noticed that a database instance could be used when the web server starts and used everywhere in model or service layer. Facades can provide this type of abstraction. So, let's make a Database Facade;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"from database import Database\\nfrom glim.core import Facade\\n\\nclass DB(Facade):\\n  \\taccessor = Database\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nA typical Facade has two functions namely `register` and `boot`. The register function can inject a configuration dict to the accessor object, in this example, namely Database class. The `boot` function can inject `(*args, **kwargs)` which can be anything. Therefore, glim can't understand how to boot an object. However, it can automatically use register function with config passed. Glim can boot these objects by the following;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# create the database instance, holds it statically\\nDB.register(config)\\n\\n# create the database instance with multiple arguments\\nDB.boot(*args, **kwargs)\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nBy default, glim has a number of facades. These facades are the following;\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Facade name\",\n    \"h-1\": \"The mapped accessor\",\n    \"0-0\": \"glim.facades.Config\",\n    \"0-1\": \"glim.core.Config\",\n    \"1-0\": \"glim.facades.View\",\n    \"1-1\": \"glim.component.View\",\n    \"2-0\": \"glim.facades.Log\",\n    \"2-1\": \"glim.log.Log\",\n    \"3-0\": \"glim.facades.Database\",\n    \"3-1\": \"glim.db.Database\",\n    \"4-0\": \"glim.facades.Orm\",\n    \"4-1\": \"glim.db.Orm\"\n  },\n  \"cols\": 2,\n  \"rows\": 5\n}\n[/block]\nThese mappings provide the mapped accessors to be instantiated in the runtime first and use them everywhere.\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"No singletons\",\n  \"body\": \"Again, this pattern is not a singleton pattern. There exists singleton manner but the implementation is completely different. Singletons mostly use private constructors which is a bad practice.\"\n}\n[/block]","excerpt":"","slug":"facades","type":"basic","title":"Facades"}
The concept of Facade is there to hide abstractions from core classes. In glim, facades can be used to hold single instances of objects that can be used everywhere during the runtime. Consider an example of a class namely "Database". A Database class example would be the following; [block:code] { "codes": [ { "code": "# database.py\nclass Database:\n\n \tdef __init__(self, config):\n \tself.config = config\n self.connection = None\n \n \tdef connect(self):\n\t\t\t\t# perform database connection using config\n self.connection = connect_to_db()\n \n def query(sql):\n \t# perform database query execution using the db connection\n return self.connection.execute_sql(sql)\n \t", "language": "python" } ] } [/block] This example is a primitive db connection library, so it won't reflect the real world. It is just given to teach the concept of Facades. In this example, you can instantiate an object of Database by the following; [block:code] { "codes": [ { "code": "# some_script.py\nfrom database import Database\n\nconfig = {\n\t\t'host': 'localhost',\n 'port': 3306,\n \t'user': 'root',\n \t'password': 'pwd'\n}\n\ndb = Database(config)\ndb.connect()\n\nsql = \"SELECT 1 FROM test.hello\"\n\nprint(db.query(sql))", "language": "python" } ] } [/block] It might be noticed that a database instance could be used when the web server starts and used everywhere in model or service layer. Facades can provide this type of abstraction. So, let's make a Database Facade; [block:code] { "codes": [ { "code": "from database import Database\nfrom glim.core import Facade\n\nclass DB(Facade):\n \taccessor = Database", "language": "python" } ] } [/block] A typical Facade has two functions namely `register` and `boot`. The register function can inject a configuration dict to the accessor object, in this example, namely Database class. The `boot` function can inject `(*args, **kwargs)` which can be anything. Therefore, glim can't understand how to boot an object. However, it can automatically use register function with config passed. Glim can boot these objects by the following; [block:code] { "codes": [ { "code": "# create the database instance, holds it statically\nDB.register(config)\n\n# create the database instance with multiple arguments\nDB.boot(*args, **kwargs)", "language": "python" } ] } [/block] By default, glim has a number of facades. These facades are the following; [block:parameters] { "data": { "h-0": "Facade name", "h-1": "The mapped accessor", "0-0": "glim.facades.Config", "0-1": "glim.core.Config", "1-0": "glim.facades.View", "1-1": "glim.component.View", "2-0": "glim.facades.Log", "2-1": "glim.log.Log", "3-0": "glim.facades.Database", "3-1": "glim.db.Database", "4-0": "glim.facades.Orm", "4-1": "glim.db.Orm" }, "cols": 2, "rows": 5 } [/block] These mappings provide the mapped accessors to be instantiated in the runtime first and use them everywhere. [block:callout] { "type": "warning", "title": "No singletons", "body": "Again, this pattern is not a singleton pattern. There exists singleton manner but the implementation is completely different. Singletons mostly use private constructors which is a bad practice." } [/block]