Dependency Injection in Node.js with Box

栏目: IT技术 · 发布时间: 6年前

Dependency Injection in Node.js made easy with “Box”

May 17 ·3min read

Dependency Injection in Node.js with Box

In a nutshell, Dependency injection ( DI for short ) is a fancy term for a software design pattern in which the dependencies (or packages, services) are injected into the dependent object. It basically allows the creation of dependencies outside of the service and provide those objects to the dependent object.

Dependency Injection helps in decoupling of our modules which makes it easier to maintain better codebase. Manual DI kind of sucks to both, implement and maintain. So, I’m demonstrating how to use Haluka Box to make DI look awesome and useful.

To demonstrate the use of Box, let’s use the world’s favorite project “ Todo List ” project. ( I know you might be thinking another cliché Todo list project, but trust me, Box is gonna make everything look awesome ).

GitHub Repo for the project — https://github.com/hacktivistic/box-todo

Let’s start with the Todo Service —

Todo.js

Todo Service is just a ES6 class service having CRUD (create, retrieve, update, delete) methods to handle the Todo. Nothing special is going on here — It just uses Database to perform the respective operations. It also requires a User object so that a Todo can be mapped to a specific user.

In the above code, Database and User are the two dependencies of Todo. In normal cases, you would just create those objects and pass them into the Todo. But, with Box, we’ll do things a bit differently ;)

Now, let’s see Database — It’s just a sample in-memory array storage just for the sake of demonstration.

Database.js

Here, the Database has nothing out of the ordinary going on. We have a “ dataRepo ” array which acts as a in-memory data store and Database has bunch of methods, executing Array functions for storing, searching, updating and removing the stored values.

Packing Things Together

Now, that we have our dependent and dependency ready, let’s use Box to make our life easier. Back to our Todo Project, we’ll now install required modules for our awesome project.

npm install @haluka/box express

Box exposes a Container class which is what we’ll be using here. It can be initialized into the project as —

const { Container } = require('@haluka/box')

Let’s set up our Box Container and register all our services into it.

box.js

Registration of the services looks good. It isn’t that of a tough task to register all of the services. Even if y.ou have 100s of services, Box has plans to automatically register them for you. [See CHANGELOG for upcoming features.]

Now that everything is ready, let’s configure our Server with express . Since express is already registered into Box as http . We can just call, h.http to get the express app instance. Pretty handy, isn’t it.

index.js

Okay, I guess this was pretty much self explanatory. Remember we had exposed box through the middleware? Yes, we use that box from the req to resolve our dependencies in our routes.

Here, since logged user is not registered, the fallback user specified in opts is used. If you have implemented a session, you may register the logged user from session in middleware or pass it as a resolving option by using resolve method. For Example —

req.box.resolve('Todo', { User: req.session.user })
// pretty handy if you want to override fallback options or registered provider.

We can clearly see the benefit of using Box. We didn’t have to worry out the database or logged user in this case.

It is also easier to replace Database implementation in our app. We just need o use another Database provider like, RethinkDB or MongoDB , and setup the Database Service implementing the same interface and register it into the Database provide. With almost little to no changes in the Todo Service, we’ll be able to completely change the database provider.

This usually comes in handy if you have multiple environments on which your application is running. Based on the current environment, you can register different implementations of Database.

Conclusion

Dependency Injection is observed in many of our applications and usually is done manually, but to make it look better Box can be used. Due to the decoupling of the services, our services are framework independent so they can be used anywhere it could be stuffed with any framework. That’s what’s called a beautiful code.

Working code is good, beautiful code is best.

GitHub Repo for the project — https://github.com/hacktivistic/box-todo


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

黑客大曝光

黑客大曝光

Joel Scambray、Vincent Liu、Caleb Sima / 姚军 / 机械工业出版社华章公司 / 2011-10 / 65.00元

在网络技术和电子商务飞速发展的今天,Web应用安全面临着前所未有的挑战。所有安全技术人员有必要掌握当今黑客们的武器和思维过程,保护Web应用免遭恶意攻击。本书由美国公认的安全专家和精神领袖打造,对上一版做了完全的更新,覆盖新的网络渗透方法和对策,介绍如何增强验证和授权、弥补Firefox和IE中的漏洞、加强对注入攻击的防御以及加固Web 2.0安全,还介绍了如何将安全技术整合在Web开发以及更广泛......一起来看看 《黑客大曝光》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试