GithubHelp home page GithubHelp logo

Comments (9)

svermeulen avatar svermeulen commented on July 28, 2024

You can just use ToMethod instead, which is transient.

from zenject-2019.

svermeulen avatar svermeulen commented on July 28, 2024

Oh wait, you're right, only BinderGeneric has ToMethod surprisingly. I'll fix that.

from zenject-2019.

cgarciae avatar cgarciae commented on July 28, 2024

@svermeulen Thanks! I looked at the code inside BinderUntyped and there only seem to be singleton methods so the fix doesn't look that simple.

from zenject-2019.

svermeulen avatar svermeulen commented on July 28, 2024

Ok I pushed a change that adds that method. I tested it in the unit tests but will wait to hear from you whether that solves your problem before closing this item

from zenject-2019.

cgarciae avatar cgarciae commented on July 28, 2024

I am having a small problem that didn't happen in ToSingleMethod, in the new extension method

        public static void ToPrefab(this BinderUntyped binder, String prefabLocation, Type type)
        {
            binder.ToMethod((ctx) => {
                var obj = Resources.Load(prefabLocation) as GameObject;
                return ctx.Container.InstantiatePrefabForComponent(type, obj) as MonoBehaviour;
            });
        }

TContract in ToMethod<TContract> gets set as MonoBehaviour because its the only sensible type I can cast to. However, the actual type of obj is type (obj.GetType() == type) and that type is known to extend MonoBehaviour.

So Zenject complains

ZenjectBindException: Invalid type given during bind command. Expected type 'UnityEngine.MonoBehaviour' to derive from type

I actually commented your code (the part that check types) so it didn't throw that exception but I got

ZenjectException: Assert hit!
ModestTree.Assert.Throw (System.String message) (at Assets/Lib/Zenject/Internal/Assert.cs:195)
ModestTree.Assert.That (Boolean condition) (at Assets/Lib/Zenject/Internal/Assert.cs:29)
Zenject.MethodProvider`1[UnityEngine.MonoBehaviour].GetInstance (Zenject.InjectContext context) (at Assets/Lib/Zenject/Main/Scripts/Providers/MethodProvider.cs:25)
Zenject.DiContainer.Resolve (Zenject.InjectContext context) (at Assets/Lib/Zenject/Main/Scripts/Main/DiContainer.cs:578)
Zenject.DiContainer.Resolve (System.Type contractType) (at Assets/Lib/Zenject/Main/Scripts/Main/DiContainer.cs:1175)

I actually need something that binds the method type as a parameter like this

        public static void ToPrefab(this BinderUntyped binder, String prefabLocation, Type type)
        {
            //Pass "type" into ToMethod
            binder.ToMethod(type, (ctx) => {
                var obj = Resources.Load(prefabLocation) as GameObject;
                return ctx.Container.InstantiatePrefabForComponent(type, obj) as MonoBehaviour;
            });
        }

from zenject-2019.

cgarciae avatar cgarciae commented on July 28, 2024

If passing type as a parameter is not possible, is the exception I get when I comment your code due to the same exception as yesterday (something about not having a CompositionRoot)?

from zenject-2019.

svermeulen avatar svermeulen commented on July 28, 2024

Ok I added a non-generic version of ToMethod, try that. What errors are you getting now?

from zenject-2019.

cgarciae avatar cgarciae commented on July 28, 2024

Works!!!!!!!!!!!!!!!!!!!!!!!!

Thanks man. Now I am able to define Routes like this with my framework

    [Route("some/route")]
    public class SomeRouteView : MonoBehaviour
    {
        [PostInject]
        public void PostConstruct(...) {
            ...
        }
    }

and start and configure like this

    public class Startup : MVCApp
    {
        public override void Configure(IApplication app, DiContainer container)
        {
            //Set view dependencies
        }

        public override void Init(IRouter router)
        {
            router.GoTo(
                url: "some/view",
                body: 42
            );
        }
    }

the framework looks for classes annotated with Route and wires the dependencies with your code!!!

from zenject-2019.

svermeulen avatar svermeulen commented on July 28, 2024

Ok great :)

Sounds like a cool setup

from zenject-2019.

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.