Service Repository it's a library to simplify the task to define and unify the distinct services existing inside any application. Allows:
- Create stateful services (singletons)
- Create stateless services acting as service factory
With the dependency injection development has become easier and faster, but with large projects increase the dificulty to start working with a preexistent project and it's difficult to understand by novice developers. For this reason we propose to go back to the old style factories, but with more fashion approach Of course you are free to use it as base for another dependency injection mechanism, but this is not the final intention
- First of all include the Jar file in your compile and execution classpath.
<dependency>
<groupId>org.bytemechanics</groupId>
<artifactId>service-repository</artifactId>
<version>X.X.X</version>
</dependency>
dependencies {
compile 'org.bytemechanics:service-repository:X.X.X'
}
- Create your service repository
package mypackage;
import org.bytemechanics.service.repository.ServiceSupplier;
import org.bytemechanics.service.repository.beans.DefaultServiceSupplier;
import org.bytemechanics.service.repository.ServiceRepository;
public enum MyServiceRepository implements ServiceRepository{
MY_SERVICE_0ARG(MyService.class,MyServiceImpl.class),
MY_SERVICE_SUPPLIER_0ARG(MyService.class,() -> new MyServiceImpl()),
MY_SINGLETON_SERVICE_0ARG(MyService.class,true,MyServiceImpl.class),
MY_SINGLETON_SERVICE_SUPPLIER_0ARG(MyService.class,true,() -> new MyServiceImpl()),
;
private final ServiceSupplier serviceSupplier;
<T> MyServiceRepository(final Class<T> _adapter,final Class<? extends T> _implementation,final Object... _args){
this.serviceSupplier=ServiceSupplier.builder(_adapter)
.name(name())
.implementation(_implementation)
.args(_args)
.build();
}
<T> MyServiceRepository(final Class<T> _adapter,final Supplier<? extends T> _implementationSupplier){
this.serviceSupplier=ServiceSupplier.builder(_adapter)
.name(name())
.supplier(_implementationSupplier)
.build();
}
<T> MyServiceRepository(final Class<T> _adapter,final boolean _singleton,final Class<? extends T> _implementation,final Object... _args){
this.serviceSupplier=ServiceSupplier.builder(_adapter)
.name(name())
.singleton(_singleton)
.implementation(_implementation)
.args(_args)
.build();
}
<T> MyServiceRepository(final Class<T> _adapter,final boolean _singleton,final Supplier<? extends T> _implementationSupplier){
this.serviceSupplier=ServiceSupplier.builder(_adapter)
.name(name())
.singleton(_singleton)
.supplier(_implementationSupplier)
.build();
}
@Override
public ServiceSupplier getServiceSupplier() {
return this.serviceSupplier;
}
public static final void startup(){
ServiceRepository.startup(Stream.of(MyServiceRepository.values()));
}
public static final void shutdown(){
ServiceRepository.shutdown(Stream.of(MyServiceRepository.values()));
}
public static final void reset(){
ServiceRepository.reset(Stream.of(MyServiceRepository.values()));
}
}
- get service instance
MyServiceRepository.MY_SINGLETON_SERVICE_0ARG.get();
MyServiceRepository.MY_SINGLETON_SERVICE_0ARG.get(MyService.class);
MyServiceRepository.MY_SINGLETON_SERVICE_0ARG.tryGet();
MyServiceRepository.MY_SINGLETON_SERVICE_0ARG.tryGet(MyService.class);