How to setup Zenject that when an interface is requested, it setups up a new game object, and returns the a...












0















Zenject is great, but I haven't found a solution that feels right, for instantiating a game object



I have a script, that provides various event hooks for monobehaviour events. Start, PostStart, Update, FixedUpdate, and others, so non-Unity controllers can execute in at certain events. It is based on an interface I named "IEventsController"



My setup before, would have been something like this:



private static IEventsController _Events;
public static IEventsController GetEvents()
{
if (_Events == null)
{
var go = new GameObject("EventsController");
_Events = go.AddComponent<EventsController>();
}

return _Events;
}


I'm trying to figure out if Zenject has a built in solution for this. I could use a factory or a method, but the factory has a whole extra class to manage this when Zenject might already handle it. Using a method to generate this still requires the static reference to check if its been created or not, and it feels wrong for that to sit in the installer script. Also, there is the FromComponent Series of bindings that might have something for this, but nothing I've seen so far.



A sample binding method call I might expect:



Container.BindComponent<IEventsController>()
.To<EventsController>()
.ViaNewGameObject("Events Controller");



  • Thanks.










share|improve this question



























    0















    Zenject is great, but I haven't found a solution that feels right, for instantiating a game object



    I have a script, that provides various event hooks for monobehaviour events. Start, PostStart, Update, FixedUpdate, and others, so non-Unity controllers can execute in at certain events. It is based on an interface I named "IEventsController"



    My setup before, would have been something like this:



    private static IEventsController _Events;
    public static IEventsController GetEvents()
    {
    if (_Events == null)
    {
    var go = new GameObject("EventsController");
    _Events = go.AddComponent<EventsController>();
    }

    return _Events;
    }


    I'm trying to figure out if Zenject has a built in solution for this. I could use a factory or a method, but the factory has a whole extra class to manage this when Zenject might already handle it. Using a method to generate this still requires the static reference to check if its been created or not, and it feels wrong for that to sit in the installer script. Also, there is the FromComponent Series of bindings that might have something for this, but nothing I've seen so far.



    A sample binding method call I might expect:



    Container.BindComponent<IEventsController>()
    .To<EventsController>()
    .ViaNewGameObject("Events Controller");



    • Thanks.










    share|improve this question

























      0












      0








      0








      Zenject is great, but I haven't found a solution that feels right, for instantiating a game object



      I have a script, that provides various event hooks for monobehaviour events. Start, PostStart, Update, FixedUpdate, and others, so non-Unity controllers can execute in at certain events. It is based on an interface I named "IEventsController"



      My setup before, would have been something like this:



      private static IEventsController _Events;
      public static IEventsController GetEvents()
      {
      if (_Events == null)
      {
      var go = new GameObject("EventsController");
      _Events = go.AddComponent<EventsController>();
      }

      return _Events;
      }


      I'm trying to figure out if Zenject has a built in solution for this. I could use a factory or a method, but the factory has a whole extra class to manage this when Zenject might already handle it. Using a method to generate this still requires the static reference to check if its been created or not, and it feels wrong for that to sit in the installer script. Also, there is the FromComponent Series of bindings that might have something for this, but nothing I've seen so far.



      A sample binding method call I might expect:



      Container.BindComponent<IEventsController>()
      .To<EventsController>()
      .ViaNewGameObject("Events Controller");



      • Thanks.










      share|improve this question














      Zenject is great, but I haven't found a solution that feels right, for instantiating a game object



      I have a script, that provides various event hooks for monobehaviour events. Start, PostStart, Update, FixedUpdate, and others, so non-Unity controllers can execute in at certain events. It is based on an interface I named "IEventsController"



      My setup before, would have been something like this:



      private static IEventsController _Events;
      public static IEventsController GetEvents()
      {
      if (_Events == null)
      {
      var go = new GameObject("EventsController");
      _Events = go.AddComponent<EventsController>();
      }

      return _Events;
      }


      I'm trying to figure out if Zenject has a built in solution for this. I could use a factory or a method, but the factory has a whole extra class to manage this when Zenject might already handle it. Using a method to generate this still requires the static reference to check if its been created or not, and it feels wrong for that to sit in the installer script. Also, there is the FromComponent Series of bindings that might have something for this, but nothing I've seen so far.



      A sample binding method call I might expect:



      Container.BindComponent<IEventsController>()
      .To<EventsController>()
      .ViaNewGameObject("Events Controller");



      • Thanks.







      unity3d zenject






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 20:06









      Dan Violet SagmillerDan Violet Sagmiller

      1,1192823




      1,1192823
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Since, I can't comment to ask for elaboration I'll do my best to answer completely.



          If you simply want to construct an object that has no dependencies, ie (does not need injection on itself) then:



          Container.Bind<IEventsController>().FromMethod(...)


          If you want to construct an object that has dependencies and required injection then create a Factory class:



          Container.Bind<IEventsController>().FromFactory<EventsControllerFactory>()

          class EventsControllerFactory : IFactory<IEventsController> {
          [Inject]
          public IDep1 dep1;

          public IEventsController Create() {
          return new EventsController(dep1);
          }
          }


          If you want to create a GameObject that is also injected you have several methods that let you choose, check out the FromComponentXXX methods, that let you use a prefab reference, or a prefab stored in Resources folder, and a few others.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400749%2fhow-to-setup-zenject-that-when-an-interface-is-requested-it-setups-up-a-new-gam%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Since, I can't comment to ask for elaboration I'll do my best to answer completely.



            If you simply want to construct an object that has no dependencies, ie (does not need injection on itself) then:



            Container.Bind<IEventsController>().FromMethod(...)


            If you want to construct an object that has dependencies and required injection then create a Factory class:



            Container.Bind<IEventsController>().FromFactory<EventsControllerFactory>()

            class EventsControllerFactory : IFactory<IEventsController> {
            [Inject]
            public IDep1 dep1;

            public IEventsController Create() {
            return new EventsController(dep1);
            }
            }


            If you want to create a GameObject that is also injected you have several methods that let you choose, check out the FromComponentXXX methods, that let you use a prefab reference, or a prefab stored in Resources folder, and a few others.






            share|improve this answer




























              0














              Since, I can't comment to ask for elaboration I'll do my best to answer completely.



              If you simply want to construct an object that has no dependencies, ie (does not need injection on itself) then:



              Container.Bind<IEventsController>().FromMethod(...)


              If you want to construct an object that has dependencies and required injection then create a Factory class:



              Container.Bind<IEventsController>().FromFactory<EventsControllerFactory>()

              class EventsControllerFactory : IFactory<IEventsController> {
              [Inject]
              public IDep1 dep1;

              public IEventsController Create() {
              return new EventsController(dep1);
              }
              }


              If you want to create a GameObject that is also injected you have several methods that let you choose, check out the FromComponentXXX methods, that let you use a prefab reference, or a prefab stored in Resources folder, and a few others.






              share|improve this answer


























                0












                0








                0







                Since, I can't comment to ask for elaboration I'll do my best to answer completely.



                If you simply want to construct an object that has no dependencies, ie (does not need injection on itself) then:



                Container.Bind<IEventsController>().FromMethod(...)


                If you want to construct an object that has dependencies and required injection then create a Factory class:



                Container.Bind<IEventsController>().FromFactory<EventsControllerFactory>()

                class EventsControllerFactory : IFactory<IEventsController> {
                [Inject]
                public IDep1 dep1;

                public IEventsController Create() {
                return new EventsController(dep1);
                }
                }


                If you want to create a GameObject that is also injected you have several methods that let you choose, check out the FromComponentXXX methods, that let you use a prefab reference, or a prefab stored in Resources folder, and a few others.






                share|improve this answer













                Since, I can't comment to ask for elaboration I'll do my best to answer completely.



                If you simply want to construct an object that has no dependencies, ie (does not need injection on itself) then:



                Container.Bind<IEventsController>().FromMethod(...)


                If you want to construct an object that has dependencies and required injection then create a Factory class:



                Container.Bind<IEventsController>().FromFactory<EventsControllerFactory>()

                class EventsControllerFactory : IFactory<IEventsController> {
                [Inject]
                public IDep1 dep1;

                public IEventsController Create() {
                return new EventsController(dep1);
                }
                }


                If you want to create a GameObject that is also injected you have several methods that let you choose, check out the FromComponentXXX methods, that let you use a prefab reference, or a prefab stored in Resources folder, and a few others.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 20:57









                Ian PilipskiIan Pilipski

                454




                454






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400749%2fhow-to-setup-zenject-that-when-an-interface-is-requested-it-setups-up-a-new-gam%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith