Giter VIP home page Giter VIP logo

more-rs-di's People

Contributors

commonsensesoftware avatar jescobar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

more-rs-di's Issues

Validation on build_provider

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():

  1. Singletons and Transient cannot be dependent on Scoped.
  2. Dependency loop.
  3. Service cannot be instantiated (no constructor or constructor contains unregistered types).
  4. Ambiguous traits (has multiple implementations but required as single).
  5. Ownership issues ???
  6. ...

Instantiation of services on demand

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.

Provider cannot be sent between threads safely

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"] }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.