Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic mixins for IMutableDependencyResolver register functions #113

Closed

Conversation

patsissons
Copy link
Contributor

adding three generic mixins for Register, RegisterConstant, and RegisterLazySingleton. These mixins allow registration based on a generic type parameter rather than using typeof(...) as a serviceType parameter.

// Example usage
public class AppBootstrapper : ReactiveObject, IScreen
{
    public AppBootstrapper()
    {
        // old method
        // dependencyResolver.RegisterConstant(this, typeof(IScreen));

        // new method
        dependencyResolver.RegisterConstant<IScreen>(this);
    }
}

New mixin functions simply call their non-generic mixin counterparts, except for Register which is not a mixin to begin with.

@flagbug
Copy link
Member

flagbug commented Jan 15, 2016

Would it make sense to check if the class actually implements the type parameter?

Something like

public static void Register<TInterface, TClass>(
    this IMutableDependencyResolver This, 
    Func<TClass> factory, string contract = null) 
    where TClass : TInterface
{
    This.Register(factory, typeof(TInterface), contract);
}

@kentcb
Copy link
Contributor

kentcb commented Jan 19, 2016

@flagbug problem with that caller is required to specify both types rather than just one (or none in some cases).

locator.Register<ISomething>(() => new Something());

Versus:

locator.Register<ISomething, Something>(() => new Something());

Also, it doesn't add anything that can't be achieved by changing the lambda to Func<T> instead of Func<object>. I'm doing this in a branch at the moment and will submit PR shortly.

@kentcb
Copy link
Contributor

kentcb commented Jan 19, 2016

Superseded by #128.

@kentcb kentcb closed this Jan 19, 2016
@ghuntley ghuntley modified the milestone: 2.0.0 Nov 5, 2016
@lock lock bot locked and limited conversation to collaborators Jun 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants