gotranseo/template

How to use the repository pattern

VineFiner opened this issue · 3 comments

http://docs.vapor.codes/3.0/extras/style-guide/

There are a few benefits to this method. First, it makes testing a lot easier. This is because during the test environment you can easily utilize Vapor’s configuration abilities to swap out which implementation of the repository protocol gets used. This makes unit testing much faster because the unit tests can use a memory version of the protocol rather than the database. The other large benefit to this pattern is that it makes it really easy to switch out the database layer if needed. Because all of the ORM logic is abstracted to this piece of the application (and the controllers don’t know it exists) you could realistically swap out Fluent with a different ORM with minimal changes to your actual application/business logic code. This also means that you could switch out the specific database used during testing (i.e. using SQLite instead of MySQL). However, this strategy requires extra work to make your models generic as they can no longer conform the the specific database model types (e.g. MySQLModel).

If you run swift test, how to do it.

jdmcd commented

Hi there, not sure exactly what this is asking. Are you asking how to switch out the type of database during testing?

how to switch out the type of database during testing?Can you make a demo? Thank you.

jdmcd commented

Unfortunately I don't have an example, sorry. I have not used that pattern before and it will be different come Vapor/Fluent 4