Repository to group all available services (stateless and statefull). The intention is have some point where all the internal project services are defined
This project is maintained by bytemechanics
Service Repository it’s a library to simplify the task to define and unify the distinct services existing inside any application. Allows:
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
<dependency>
<groupId>org.bytemechanics</groupId>
<artifactId>service-repository</artifactId>
<version>X.X.X</version>
</dependency>
dependencies {
compile 'org.bytemechanics:service-repository:X.X.X'
}
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()));
}
}
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);