Giter VIP home page Giter VIP logo

Comments (8)

EisenbergEffect avatar EisenbergEffect commented on June 9, 2024

In this case you two options:

  1. Explicitly register App with the container using registerTransient. This tells the container that every time it is requested, a new instance should be returned.
  2. Create a child container from the original using createChild This creates a new container that inherits the services of its parent. Manually register the App service with the child container as either singleton or transient and resolve App from the child.

Child containers are for more advanced scenarios such as wanting to have a parent container for your app, but a scoped child container for each web request. Child containers allow you to override services from the parent while still leveraging other instances managed by the parent. Usually if you just want to get new instances of a particular type, you just register it as a transient. So, options 1 is probably what you want. Another way you can accomplish transient registration automatically is to annotate the App class with the Transient annotation.

from dependency-injection.

EisenbergEffect avatar EisenbergEffect commented on June 9, 2024

I've added a few more tests. There is a test showing the API registerTransient to go along with the annotations tests.

from dependency-injection.

djindjic avatar djindjic commented on June 9, 2024

Ok, thanks, it works for me:

    class App {}

    var container = new Container();
    container.registerTransient(App, App);

    var app1 = container.get(App);
    var app2 = container.get(App);

    expect(app1).not.toBe(app2);

Can you tell me what do that args mean (App, App)? Why App on two places?

from dependency-injection.

EisenbergEffect avatar EisenbergEffect commented on June 9, 2024

The first arg is the key or type to register as. The second arg is the type of instance that key should resolve to. This enables scenarios like this:

container.registerTransient(AbstractType, ConcreteType);

So whenever someone requests AbstractType, they will get an instance of ConcreteType. This is used by the framework internally to allow it to work against abstractions of key services. I was thinking today that there should probably be a shortcut like this:

container.registerTransient(ConcreteType);

So you can easily use this when you don't have an abstraction. What do you think?

from dependency-injection.

djindjic avatar djindjic commented on June 9, 2024

I think that shortcut is prettier in my case.
If it could be worth to you to hear beginner's experience, from my perspective, I have expected to get new instance by default. Ok, in this moment I don't have an wide look to framework and what other aurelia tools need from it, but just saying.

from dependency-injection.

EisenbergEffect avatar EisenbergEffect commented on June 9, 2024

I'll go ahead and add the shortcut. That should be easy. Regarding default behavior, I've seen DI frameworks that do either. For purposes of Aurelia, the singleton approach works best. Since we don't really have docs yet...I think providing those and making the default behavior known should solve the problem.

from dependency-injection.

djindjic avatar djindjic commented on June 9, 2024

ok, I will watch the project get notif about shortcut

from dependency-injection.

EisenbergEffect avatar EisenbergEffect commented on June 9, 2024

It's in master now. I've also added a few more tests and re-arranged them a bit. So, hopefully that will help you see some more details. I still need to add more, but it's a start.

I'm currently adding the api docs to the source code. I'll commit that shortly. Then, I'm going to try to write up some preliminary documentation. You will be able to find that in the doc folder when it's pushed.

Hopefully, I will have another release of this module in the next day or two.

from dependency-injection.

Related Issues (20)

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.