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

Syntax sugar for Func/Delegate/Type #11

Closed
HaoK opened this Issue Sep 19, 2014 · 10 comments

Comments

Projects
None yet
5 participants
@HaoK
Member

HaoK commented Sep 19, 2014

Syntax sugar would be specifically to have a value-type that can be used as options.Notification to hold the delegate, and also to hold onto the delegate.

@HaoK

This comment has been minimized.

Show comment
Hide comment
@HaoK

HaoK Sep 19, 2014

Member
struct Thing<T>
{
    void Set(T instance);
    void Set(Type t);
    void Set(Func<IServiceProvider, T>);

    Func<IServiceProvider, T> Factory;

}
Member

HaoK commented Sep 19, 2014

struct Thing<T>
{
    void Set(T instance);
    void Set(Type t);
    void Set(Func<IServiceProvider, T>);

    Func<IServiceProvider, T> Factory;

}
@lodejard

This comment has been minimized.

Show comment
Hide comment
@lodejard

lodejard Sep 19, 2014

plus as a value type it would be nice to have it be implicitly assignable as any of those three.

lodejard commented Sep 19, 2014

plus as a value type it would be nice to have it be implicitly assignable as any of those three.

@lodejard

This comment has been minimized.

Show comment
Hide comment
@lodejard

lodejard Sep 19, 2014

class SomeOptions
{
   Thing<ISomeNotifications> Notifications;
}
// later...
options.Notifications = typeof(MyNotifications);
or
options.Notifications = new MyNotifications();

need to experiment to see if that works out nicely for intellisense and whatnot

lodejard commented Sep 19, 2014

class SomeOptions
{
   Thing<ISomeNotifications> Notifications;
}
// later...
options.Notifications = typeof(MyNotifications);
or
options.Notifications = new MyNotifications();

need to experiment to see if that works out nicely for intellisense and whatnot

@rustd rustd modified the milestone: 1.0.0-beta2 Oct 17, 2014

@PinpointTownes

This comment has been minimized.

Show comment
Hide comment
@PinpointTownes

PinpointTownes Jun 7, 2015

@HaoK any progress concerning this (essential) feature? 😄

PinpointTownes commented Jun 7, 2015

@HaoK any progress concerning this (essential) feature? 😄

@HaoK

This comment has been minimized.

Show comment
Hide comment
@HaoK

HaoK Jul 28, 2015

Member

With named options going away, we could use this in place of IOptions for middleware, maybe something like:

public class Box<TService> {
   public Box(TService instance) { _instance = instance; }
   public Box(Func<TService> factory); { _factory = factory; }

   public void Initialize(IServiceProvider services);

   public TService Value {
     return _instance ?? _factory?.Invoke() ?? _services?.GetRequiredService<TService>();
   }
}

public class XyzMiddleware {
   public XyzMiddleware(Box<XyzOptions> options) { Options = options.Value }
}

app.UseMiddleware<XyzMiddleware>(new Box(xyzOptions)); // use instance
app.UseMiddleware<XyzMiddleware>(new Boxy(() => new XyzOptions())); // factory delegate
app.UseMiddleware<XyzMiddleware>(); // use whatevers in DI 

I'll also have to update the TypeActivator to special case Box parameters, and call Initialize with the ISP

Thoughts? @divega @lodejard @Tratcher @davidfowl

Member

HaoK commented Jul 28, 2015

With named options going away, we could use this in place of IOptions for middleware, maybe something like:

public class Box<TService> {
   public Box(TService instance) { _instance = instance; }
   public Box(Func<TService> factory); { _factory = factory; }

   public void Initialize(IServiceProvider services);

   public TService Value {
     return _instance ?? _factory?.Invoke() ?? _services?.GetRequiredService<TService>();
   }
}

public class XyzMiddleware {
   public XyzMiddleware(Box<XyzOptions> options) { Options = options.Value }
}

app.UseMiddleware<XyzMiddleware>(new Box(xyzOptions)); // use instance
app.UseMiddleware<XyzMiddleware>(new Boxy(() => new XyzOptions())); // factory delegate
app.UseMiddleware<XyzMiddleware>(); // use whatevers in DI 

I'll also have to update the TypeActivator to special case Box parameters, and call Initialize with the ISP

Thoughts? @divega @lodejard @Tratcher @davidfowl

@HaoK

This comment has been minimized.

Show comment
Hide comment
@HaoK

HaoK Jul 28, 2015

Member

Instead we can just make MyOptions : IOptions<MyOptions> and accomplish this without the Box

Member

HaoK commented Jul 28, 2015

Instead we can just make MyOptions : IOptions<MyOptions> and accomplish this without the Box

@divega divega added this to the 1.0.0-beta7 milestone Jul 28, 2015

@HaoK

This comment has been minimized.

Show comment
Hide comment
@HaoK

HaoK Aug 11, 2015

Member

Sounds like this is no longer needed with the direction we went making Options implement IOptions

Member

HaoK commented Aug 11, 2015

Sounds like this is no longer needed with the direction we went making Options implement IOptions

@HaoK HaoK closed this Aug 11, 2015

@PinpointTownes

This comment has been minimized.

Show comment
Hide comment
@PinpointTownes

PinpointTownes Aug 12, 2015

Sounds like this is no longer needed with the direction we went making Options implement IOptions

Do you have a sample demonstrating how it works concretely?

A typical case is when you want to replace the default notifications class with a new class where you need to inject per-scoped things like a DbContext.

public class MyOAuthBearerAuthenticationNotifications : OAuthBearerAuthenticationNotifications {
    public MyOAuthBearerAuthenticationNotifications(DbContext context) {
        SecurityTokenReceived = async notification => { [validate the token with a DB call] };
    }
}

PinpointTownes commented Aug 12, 2015

Sounds like this is no longer needed with the direction we went making Options implement IOptions

Do you have a sample demonstrating how it works concretely?

A typical case is when you want to replace the default notifications class with a new class where you need to inject per-scoped things like a DbContext.

public class MyOAuthBearerAuthenticationNotifications : OAuthBearerAuthenticationNotifications {
    public MyOAuthBearerAuthenticationNotifications(DbContext context) {
        SecurityTokenReceived = async notification => { [validate the token with a DB call] };
    }
}
@PinpointTownes

This comment has been minimized.

Show comment
Hide comment
@PinpointTownes

PinpointTownes May 20, 2016

Still no official plan for DI-enabled events classes?

Related topic: aspnet-contrib/AspNet.Security.OpenIdConnect.Server#275

/cc @ilmax

PinpointTownes commented May 20, 2016

Still no official plan for DI-enabled events classes?

Related topic: aspnet-contrib/AspNet.Security.OpenIdConnect.Server#275

/cc @ilmax

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