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 interface support #70

Merged
merged 4 commits into from Nov 3, 2014
Merged

Generic interface support #70

merged 4 commits into from Nov 3, 2014

Conversation

bennor
Copy link
Contributor

@bennor bennor commented Nov 3, 2014

When using something like Web API, it's a fairly common pattern to have a whole stack of CRUD rest services. Refit now supports these, allowing you to define a single API interface with a generic type:

public interface IReallyExcitingCrudApi<T, in TKey> where T : class
{
    [Post("")]
    Task<T> Create([Body] T paylod);

    [Get("")]
    Task<List<T>> ReadAll();

    [Get("/{key}")]
    Task<T> ReadOne(TKey key);

    [Put("/{key}")]
    Task Update(TKey key, [Body]T payload);

    [Delete("/{key}")]
    Task Delete(TKey key);
}

Which can then be used like this:

// The "/users" part here is kind of important if you want it to work for more 
// than one type (unless you have a different domain for each type)
var api = RestService.For<IReallyExcitingCrudApi<User, string>>("http://api.example.com/users"); 

This fixes #63.

I will update the docs to something like the above if you're happy with the changes.

This is needed (in conjunction with #40) for generic interfaces to be of
any use at all.
Fixes #63.

Now people can have all the boring CRUD Web APIs they want and they only
need one interface. :trollface:
@anaisbetts
Copy link
Member

This looks great! Update the docs and :shipit:

@@ -352,8 +352,10 @@ public RestMethodInfo(Type targetInterface, MethodInfo methodInfo)
}
}

void verifyUrlPathIsSane(string relativePath)
{
void verifyUrlPathIsSane(string relativePath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ ⬇️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. I haven't yet figured out how to get R# to respect your conventions and sometimes I miss mistakes. Do you use it? Might be worth checking in a settings file if you have one.

@bennor
Copy link
Contributor Author

bennor commented Nov 3, 2014

All updated.

(I noticed the code blocks in the readme weren't getting syntax highlighting like the blocks above, so I changed them all to "csharp" rather than "cs" and it seems to be working now. I could have sworn "cs" used to be enough.)

anaisbetts added a commit that referenced this pull request Nov 3, 2014
@anaisbetts anaisbetts merged commit 54a12ee into reactiveui:master Nov 3, 2014
@anaisbetts
Copy link
Member

Thanks @bennor!

@bennor bennor deleted the generic-interfaces branch November 3, 2014 07:27
mattiasnordqvist added a commit to mattiasnordqvist/Web-Anchor that referenced this pull request Apr 21, 2015
Just had to try using a Type Parameterized Api. Could be pretty useful.
Like this: reactiveui/refit#70
@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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generic Interface
2 participants