commonsensesoftware / more-rs-di Goto Github PK
View Code? Open in Web Editor NEWRust Dependency Injection (DI) framework
License: MIT License
Rust Dependency Injection (DI) framework
License: MIT License
So on build_provider()
it is possible to analyze and validate all registered services whether they are correct or not.
The build_provider()
returns Result<ServiceProvider, Error>
.
Validation rules on build_provider()
:
It is nice to see a simple di crate. I haven't seen other packages which have scoped descriptors implemented.
It would be nice to have a feature when services are instantiated on demand, not immediately when parent service is being created.
For example:
pub struct MyService {
sp: ServiceProvider,
// s1: &Service1,
// s2: &Service2,
}
impl MyService {
fn new(sp: ServiceProvider) -> Self {
Self { sp }
}
fn s1(&self) -> &Service1 {
&self.sp.get_required::<Service1>();
}
fn s2(&self) -> &Service2 {
&self.sp.get_required::<Service2>();
}
pub fn do_work_one(&self) {
&self.s1.work_one();
}
pub fn do_work_two(&self) {
&self.s2.work_two();
}
}
and then somewhere else if we execute the following scope, only one service Service1
will be instantiated:
{
var service = provider.get_required::<MyService>();
service.do_work_one();
}
This is very useful when you have many heavy services and you don't want to instantiate them all. In other programming languages usually getter
is used (eg. the property looks like this s1: Service1 { get { return &self.sp.get_required::<Service1>(); } }
).
Note! Because of this approach it is hard to tell which services are dependent on MyService
. But with some macros or just simply by scanning properties/methods and looking for their returning types I assume it is possible.
Trying to add service provider in salvo web framework and seems to fail.
let service_provider = ServiceCollection::default().build_provider();
let router = Router::new()
.hoop(salvo::affix::inject(service_provider))
48 | .hoop(salvo::affix::inject(service_provider))
| -------------------- ^^^^^^^^^^^^^^^^ `(dyn std::any::Any + 'static)` cannot be sent between threads safely
| |
| required by a bound introduced by this call
I do have async enabled.
more-di = { version = "1.0.0", features = ["async"] }
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.