I try to implement dependency injection with repository pattern in expressJS. I want to achieve that using two different repository types (mongodb and SQL Server ) but one controller.
When app starts, I should choose one repository type (for example SQL Server repository) which is used by application. So I don't want to create different controllers for every repository types.
How can I do that? Is there any suggestion?
I suspect you are coming from a statically-typed languages like C# or Java in which using one class over another is somewhat convoluted since their interface must match. Keep in mind that in languages like JavaScript that are dynamically typed using one class or another is easy; there is little required ceremony to use one or the other, it's mostly up to you to make sure both classes implement the required functionality.
var dbProv = (config == "sql") ? new SqlProv() : new mongoProv();
dbProv.getAll();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With