AppRoll

What is AppRoll?
AppRoll is a simple modular framework with dependency injection based on reflection. It is designed to be used as a base for a all kinds of applications, from simple CLI tools to complex web applications.
How to use it?
AppRoll is designed to be used as a base for your application.
First, it is worth learning about the presence of 2 abstractions, Module and Provider.
A module is a structure that follows simple rules:
- It has a name ending with "Module"
- It has a function called "RollStart" with any dependencies (modules or provider) as arguments and returns an error
- Modules can only be initialized in AppRoll in a single instance
- Modules cannot create circular dependencies between each other (this is checked at runtime, and will return an error if a circular dependency is detected)
- Modules can have dependencies on other modules
A provider is a structure that follows simple rules:
- Is a pointer to a structure, cannot be a primitive (int, uint, float, etc...) or a function.
Simple example
type UserRepositoryModule struct {
DB *gorm.DB
}
func (m *UserRepositoryModule) RollStart(db *gorm.DB) error {
// db *gorm.DB is a dependency of this module and added as provider to AppRoll
m.DB = db
return nil
}
type UserServiceModule struct {
UserRepository *UserRepositoryModule
}
func (m *UserServiceModule) RollStart(repo *UserRepositoryModule) error {
m.UserRepository = repo
return nil
}
func main() {
app := approll.NewApp(approll.Config{})
// It can be in any order, but it is recommended to add providers first
err := app.AddProvider(func() *gorm.DB {
db, _ := gorm.Open("sqlite3", "test.db")
return db
}())
if err != nil {
panic(err)
}
// It can be in any order
err = app.AddModule(&UserRepositoryModule{}, &UserServiceModule{})
if err != nil {
panic(err)
}
if err := app.Start(); err != nil {
panic(err)
}
}