Skip to content

Provide API for easy service/repo registration #384

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

Closed
jaredcnance opened this issue Aug 24, 2018 · 3 comments
Closed

Provide API for easy service/repo registration #384

jaredcnance opened this issue Aug 24, 2018 · 3 comments

Comments

@jaredcnance
Copy link
Contributor

jaredcnance commented Aug 24, 2018

Description

services.AddResourceService<FooService>();

should register any applicable forms:

services.Add<IResourceService<Foo>, FooService>();
services.Add<IResourceService<Foo, int>, FooService>();
services.Add<IGetAllService<Foo>, FooService>();
services.Add<IGetAllService<Foo, int>, FooService>();
// ... 

It should also handle services with mappings:

services.Add<IResourceService<Foo, Bar, int>, FooService>();

This API may be usable by the service discovery pipeline

@btecu
Copy link
Contributor

btecu commented Sep 11, 2018

I should mention that having services.Add<IResourceService<Foo>, FooService>(); instead of services.Add<IResourceService<Foo, int>, FooService>(); doesn't error out, which might be confusing.

@jaredcnance
Copy link
Contributor Author

jaredcnance commented Sep 11, 2018

Why should it error out? You're simply registering a dependency. The fact that the former doesn't work, should be entirely dependent upon how you're requesting the dependency in your controller/service. For example, if in your controller you request IResourceService<Foo> it should just work:

public class FooController : JsonApiController<Foo>
{
    public PeopleController(
        IJsonApiContext jsonApiContext,
        IResourceService<Foo> resourceService, // <-- id type not specified
        ILoggerFactory loggerFactory) 
        : base(jsonApiContext, resourceService, loggerFactory)
    { }
}

// requires 
services.Add<IResourceService<Foo>, FooService>();

but, if your controller requests the id specific type IResourceService<Foo, int>, then it will fail to activate unless that type has been registered:

public class FooController : JsonApiController<Foo, int>
{
    public PeopleController(
        IJsonApiContext jsonApiContext,
        IResourceService<Foo, int> resourceService, // <-- id type specified
        ILoggerFactory loggerFactory) 
        : base(jsonApiContext, resourceService, loggerFactory)
    { }
}

// requires
services.Add<IResourceService<Foo, int>, FooService>();

That said, we might be able to do something with Roslyn analyzers that would produce a warning if you've registered a dependency that is not used in any constructor arguments 🤷‍♂️

@btecu
Copy link
Contributor

btecu commented Sep 11, 2018

That makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants