# Will Factory.of() be supported in Aurelia 2?

**URL:** <https://discourse.aurelia.io/t/will-factory-of-be-supported-in-aurelia-2/4282>\
**Category:** vNext\
**Created:** [June 29, 2021, 3:54am UTC](https://discourse.aurelia.io/t/will-factory-of-be-supported-in-aurelia-2/4282 "2021-06-29T03:54:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![estaylorco](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/estaylorco/32/1887_2.png) [@estaylorco](https://discourse.aurelia.io/u/estaylorco)\
**Post date:** [June 29, 2021, 3:54am UTC](https://discourse.aurelia.io/t/will-factory-of-be-supported-in-aurelia-2/4282/1 "2021-06-29T03:54:55Z")

</div>

I noticed in the documentation for Aurelia 2 that there is a black **X** next to the DI `Factory` resolver. Is this simply an indication that it hasn’t been implemented yet, or is this resolver going away? If the latter, will there be an equivalent?

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 29, 2021, 6:07am UTC](https://discourse.aurelia.io/t/will-factory-of-be-supported-in-aurelia-2/4282/2 "2021-06-29T06:07:42Z")

</div>

This resolver will be added, there doesn’t seem to be anything special about this resolver that we don’t wanna add in v2. Though can you give an example of how you are using it?

---

<div class="post-metadata">

**Author:** ![estaylorco](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/estaylorco/32/1887_2.png) [@estaylorco](https://discourse.aurelia.io/u/estaylorco)\
**Post date:** [June 29, 2021, 11:10pm UTC](https://discourse.aurelia.io/t/will-factory-of-be-supported-in-aurelia-2/4282/3 "2021-06-29T23:10:54Z")

</div>

First, that’s a relief!

Second, as to an example, I’m using it all over the place, actually. I use it wherever I have traditional dependencies to supply in the constructor, but also dependencies that I need Aurelia to inject.

Just one example can be found in `TsiDocumentHub` (note that I’m using the **babel-contracts** plugin that I set up with Eiffel-style Design-by-Contract terminology):

```auto
@inject(SessionService, TaskQueue, Factory.of(TsiDocumentManager))
export class TsiDocumentHub extends TsiMessenger {
  constructor(sessionService, taskQueue, documentManagerFactory) {
    invariant: {
        sessionService, 'Session service exists';
        taskQueue, 'Task queue exists';
        documentManagerFactory, 'Document manager factory exists';
    }

    super();

    ...
    this._documentManagerFactory = documentManagerFactory;
    ...
  }
  ...
}

```

where `TsiDocumentManager` has this signature:

```auto
@inject(TsiGroupStylesManager)
export class TsiDocumentManager extends TsiMessenger {
  /**
   * @param {string} messageChannel Message channel.
   * @param {string} key Key under which this Document Manager is stored in the Document Hub.
   */
  constructor(groupStylesManager, messageChannel, key) {
    invariant: {
      groupStylesManager, 'Group styles manager exists';
      messageChannel, 'Message channel exists';
      key, 'Key exists';
    }

    super();

    ...
  }
  ...
}

```

The `messageChannel` and `key` dependencies are simply passed in in the traditional way, but Aurelia injects the `TsiGroupStylesManager`, of course.

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 29, 2021, 11:20pm UTC](https://discourse.aurelia.io/t/will-factory-of-be-supported-in-aurelia-2/4282/4 "2021-06-29T23:20:08Z")

</div>

How will you use `_documentManagerFactory ` later?

1. Is this like this

```auto
const manager = this._documentManagerFactory()

```

1. or like this

```auto
const manager = this._documentManagerFactory(extraParam1, extraParam2, extraParam3)

```

if it’s (1), shouldn’t `Lazy.of` also provide the same effect?

EDIT: Actually nvm, lazy doesn’t guarantee it’s a new instance every call.  
Thanks for the example.

---

<div class="post-metadata">

**Author:** ![estaylorco](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/estaylorco/32/1887_2.png) [@estaylorco](https://discourse.aurelia.io/u/estaylorco)\
**Post date:** [June 29, 2021, 11:39pm UTC](https://discourse.aurelia.io/t/will-factory-of-be-supported-in-aurelia-2/4282/5 "2021-06-29T23:39:48Z")

</div>

To your point, `Lazy.of` is not the way for me to go here. Also, this is but one example of many throughout my codebase (well, maybe not _many_, but a good number).

Consider further down in the `TsiDocumentHub` class:

```auto
/**
   * Message handler for requesting that a new document manager be created in the hub,
   * and associated with the given key.
   * @param {object} messageData The message payload.
   * @private
   */
  _onMessageRequestDocumentManager(messageData) {
    require: {
      messageData, 'Message data exists';
      messageData.messageChannel, 'Message channel exists';
      messageData.key, 'Key exists';
      this._documentManagers, 'Document managers exists';
      this._documentManagers instanceof Map, 'Document managers is of type Map';
    }

    this._documentManagers.set(
      messageData.key,
      this._documentManagerFactory(messageData.messageChannel, messageData.key),
    );

    ensure: {
      this._documentManagers.has(messageData.key), 'Document manager is amongst document managers';
    }
  }

```

The instance of `TsiDocumentManager` receives from Aurelia the injected dependency of `TsiGroupStylesManager` referenced in my previous reply.

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 30, 2021, 12:54pm UTC](https://discourse.aurelia.io/t/will-factory-of-be-supported-in-aurelia-2/4282/6 "2021-06-30T12:54:17Z")

</div>

It’s added in this PR [feat(di): @factory resolver by bigopon · Pull Request #1211 · aurelia/aurelia (github.com)](https://github.com/aurelia/aurelia/pull/1211)

---

<div class="post-metadata">

**Author:** ![estaylorco](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/estaylorco/32/1887_2.png) [@estaylorco](https://discourse.aurelia.io/u/estaylorco)\
**Post date:** [June 30, 2021, 1:11pm UTC](https://discourse.aurelia.io/t/will-factory-of-be-supported-in-aurelia-2/4282/7 "2021-06-30T13:11:53Z")

</div>

Thanks so much! I will keep an eye on this.
