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











up vote
0
down vote

favorite












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


























    up vote
    0
    down vote

    favorite












    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
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 6 hours ago









      Dan Violet Sagmiller

      1,0742823




      1,0742823
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          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',
            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








            up vote
            0
            down vote













            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

























              up vote
              0
              down vote













              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























                up vote
                0
                down vote










                up vote
                0
                down vote









                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 5 hours ago









                Ian Pilipski

                454




                454






























                     

                    draft saved


                    draft discarded



















































                     


                    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

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...