Lets say I have angular dart module as below
class CarouselModule extends Module {
CarouselModule() {
install(new TransitionModule());
bind(Carousel);
bind(Slide);
}
}
Application app = applicationFactory()
..addModule(new CarouselModule());
app.run();
Now at runtime, I want switch to a different module. Is this possible in angular-dart?
Related
I am working on a multi-module android project. In main module we have a CoreComponent with CoreModule. CoreModule provides some objects. I want to inject those objects in our feature modules without creating new components.
What is the best way to do that?
Main Module
#Component
CoreComponent ( modules = CoreModule.class )
#Module
CoreModule
Feature Module
#Module
FeatureModule(includes = CoreModule.class)
Unlike with Dagger 1, Dagger 2 modules don't have to be complete: CoreModule and FeatureModule can refer to bindings that they do not define. When Dagger 2 aggregates all of your modules to generate an implementation of your component, it will let you know whether you are missing any bindings.
#Component(modules = {CoreModule.class, FeatureModule.class})
public interface CoreComponent {
}
#Module
public class CoreModule {
#Provides public static A getA() { /* ... */ }
}
#Module /* no includes */
public class FeatureModule {
/* Feature Module injects binding A from CoreModule, even without includes= */
#Provides public static B getB(A a) { /* ... */ }
}
Should you use includes? It depends. If you have FeatureModule include CoreModule, then any time you refer to FeatureModule it will automatically include CoreModule, which can make FeatureModule easier to use, and Dagger will remove any duplicate inclusions of FeatureModule. However, if you want to use the bindings in FeatureModule but a MockCoreModule in your integration tests, then you won't be able to let FeatureModule include CoreModule because it will presumably conflict in bindings with MockCoreModule.
Notably, this assumes that FeatureModule and CoreModule are a part of the same overall Component, because you've only shown us one component. If you have a separate FeatureComponent, then you'll need to list it in your question; the relationship (if any) between CoreComponent and FeatureComponent: component dependency, subcomponent, or unrelated separate component. Each of those cases will be different.
Is possible to get all services extended from a certain interface in grails
So I can collect them within one service
Yes, I think you can do this by autowiring to List type. Let's say you have three implementations of an interface BaseService in grails-app/services as:
grails-app/services/exampleapp/BaseService.groovy
package exampleapp
interface BaseService {
}
grails-app/services/exampleapp/OneImplService.groovy
package exampleapp
class OneImplService implements BaseService {
}
grails-app/services/exampleapp/TwoImplService.groovy
package exampleapp
class TwoImplService implements BaseService {
}
Now, you can inject all implementations of BaseSerive as follows:
grails-app/services/exampleapp/TestService.groovy
package exampleapp
import org.springframework.beans.factory.annotation.Autowired
class TestService {
#Autowired
List<BaseService> baseServiceList
void doSomething() {
assert baseServiceList.size() == 2
}
}
Please note that the above example is tested with Grails 5 web application. But, I would be surprised if this does not work in the previous versions of Grails.
Angular 6 has introduced tree shakeable providers with the
#Inject({provideIn:'...'}
syntax.
In order to take advantage of the tree shaking aspect, does it make a difference whether I inject the service directly via the constructor or via an Injector?
Example:
Sevice:
#Inject({provideIn:'...'}
class MyService {}
Consumer1:
#Component()
class MyComponent {
constructor(s: MyService) {}
}
Consumer2:
#Component()
class MyComponent {
constructor(#Inject(Injector) aInjector: Injector) {
const s: MyService = aInjector.get(MyService);
}
}
In both cases MyService gets injected. I would expect that only in the first case the AOT compiler will be able to determine that the service is used via static analysis.
What would be the effect if I used the second version? Would AOT detect the injection of Injector and then basically skip tree shaking for providers? Or is it smart enough to detect that MyService is fetched from the injector?
I'm working with Guice and have one design question. My App consists of few module:
myapp-persistence (JPA Entities, DAO, other DB related stuff)
myapp-backend (Some background daemons, they use myapp-persistence )
myapp-rest (REST app that depends on myapp-persistence)
myapp-persistence must have singleton HibernateSessionFactory. It's by Hibernate design.
No problem I can solve it with Guice:
class MyAppPersistenceModule extends AbstractModule {
override def configure(): Unit = {
bind(classOf[SomeStuff])
bind(classOf[ClientDao])
bind(classOf[CustomerDao])
bind(classOf[SessionFactory]).toProvider(classOf[HibernateSessionFactoryProvider]).asEagerSingleton()
}
#Provides
def provideDatabaseConnectionConfiguration: DatabaseConnectionConfiguration = {
DatabaseConnectionConfiguration.fromSysEnv
}
}
The problem with passing DatabaseConnectionConfiguration to that singleton. myapp-persistence module doesn't really care how to get that config. Right now it's taken from sys variables.
myapp-rest is play-app and it wants to read conf from application.conf and inject it into other components using Guice.
myapp-backend does more or less the same.
Right now I'm locked myself with
#Provides
def provideDatabaseConnectionConfiguration: DatabaseConnectionConfiguration = {
DatabaseConnectionConfiguration.fromSysEnv
}
And I don't understand how to make it flexible and configurable for myapp-rest and myapp-backend.
UPD
According to answer, I did it this way:
Defined trait
trait DbConfProvider {
def dbConf: DbConf
}
Singleton factory now depends on provider:
class HibernateSessionFactoryProvider #Inject()(dbConfProvider: DbConfProvider) extends Provider[SessionFactory] {
}
myapp-persistence module exposes public guice module with all piblic persistence module DAO.
myapp-persistence has module used only for testing purposes. myapp-persistence Injector load module described below:
class MyAppPersistenceDbConfModule extends AbstractModule {
override def configure(): Unit = {
bind(classOf[DbConfProvider]).to(classOf[DbConfSysEnvProvider])
}
}
DbConfSysEnvProvider reads DB connection settings from sys env. Non production use case.
Play app has it's own conf mechanism. I've added my custom module to app conf:
# play-specific config
play.modules.enabled += "common.components.MyAppPersistenceDbConfModule"
# public components from myapp-persistence module.
play.modules.enabled += "com.myapp.persistence.connection.PersistenceModule"
And my configuration service:
#Singleton
class ConfigurationService #Inject()(configuration: Configuration) extends DbConfProvider {
...}
I am not an expert on Play-specific setup, but generally this kind of design problem is solved in one of the following ways:
No default. Remove the binding of DatabaseConnectionConfiguration from the upstream module (myapp-persistence), and define it in each downstream module (myapp-backend, myapp-rest) as appropriate.
Default with override. Keep the default binding of DatabaseConnectionConfiguration like you did, implementing the most common configuration strategy there. Override it in downstream modules using Guice Modules.override(..) API when needed.
Implement a unified configuration mechanism across the modules, that does not depend on particular frameworks used. (E.g. Bootique, which is built on Guice ... Haven't used it with Play though).
I personally prefer the approach #3, but in the absence of something like Bootique, #2 is a good substitute.
I'm trying to learn how to implement logging using the examples/tutorial in:
http://blog.dartwatch.com/2013/05/campaign-to-use-real-logging-instead-of.html#comment-form
But having imported the libraries this line in main will not compile because the class 'PrintHandler' is not recognized and Google has not been a help in this case. My server application consists of a main and three classes. I'm new at Dart. Below I've extracted the logging code that I added.
In what library is 'PrintHandler'? Is this a class I need to write?
library server;
import 'package:logging_handlers/logging_handlers_shared.dart';
import 'package:logging/logging.dart';
final _serverLogger = new Logger("server"); // top level logger
void main() {
Logger.root.onRecord.listen(new PrintHandler()); // default PrintHandler
_serverLogger.fine("Server created");
}
class A {
}
class B {
}
class C {
}
It looks like the class was changed to LogPrintHandler but the tutorial and documentation were not updated.