You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@reef.apache.org by Andrey Meleshko <an...@microsoft.com> on 2016/10/07 14:59:05 UTC

how to use BindImplementation with named parameters

Hi ,
I have a question on how to create app dependency with default behavior overridable by app client.
The dependency can be defined as interface and client can use BindImplementation to provide specific implementation for the interface.
Etc, this works:

In App constructor:
        [Inject]
        public FooClass(IBarHandler barHandler)
{
...
}

Client:
var barHandlerConfig = TangFactory.GetTang().NewConfigurationBuilder()
                .BindImplementation(
                    GenericType<IBarHandler >.Class,
                    GenericType<MyBarHandler>.Class)
                .Build();


But I am not sure how to define default implementation for the interface (in case if client did not provide any).
I know default implementation can be defined if the dependency is declared as named parameter,
but then BindImplemetation as declared above doesn't seem to work
(client implementation is ignored and default implementation is invoked all the time)
Any help appreciated.

Thank you,
Andrey

RE: how to use BindImplementation with named parameters

Posted by Andrey Meleshko <an...@microsoft.com>.
That worked.
Thank you
/a

> -----Original Message-----
> From: Markus Weimer [mailto:markus@weimo.de]
> Sent: Friday, October 7, 2016 9:00 AM
> To: REEF Developers Mailinglist <de...@reef.apache.org>
> Subject: Re: how to use BindImplementation with named parameters
> 
> Hi,
> 
> you can annotate `IBarHandler` with
> `Org.Apache.REEF.Tang.Annotations.DefaultImplementationAttribute` to tell
> Tang which default implementation to use for an interface. For an example,
> see `Org.Apache.REEF.IO.TempFileCreation.ITempFileCreator`:
> 
> ```
>     [DefaultImplementation(typeof(TempFileCreator))]
>     public interface ITempFileCreator{...} ```
> 
> Hope this helps,
> 
> Markus
> 
> On Fri, Oct 7, 2016 at 7:59 AM, Andrey Meleshko
> <an...@microsoft.com> wrote:
> > Hi ,
> > I have a question on how to create app dependency with default behavior
> overridable by app client.
> > The dependency can be defined as interface and client can use
> BindImplementation to provide specific implementation for the interface.
> > Etc, this works:
> >
> > In App constructor:
> >         [Inject]
> >         public FooClass(IBarHandler barHandler) { ...
> > }
> >
> > Client:
> > var barHandlerConfig = TangFactory.GetTang().NewConfigurationBuilder()
> >                 .BindImplementation(
> >                     GenericType<IBarHandler >.Class,
> >                     GenericType<MyBarHandler>.Class)
> >                 .Build();
> >
> >
> > But I am not sure how to define default implementation for the interface
> (in case if client did not provide any).
> > I know default implementation can be defined if the dependency is
> > declared as named parameter, but then BindImplemetation as declared
> > above doesn't seem to work (client implementation is ignored and
> > default implementation is invoked all the time) Any help appreciated.
> >
> > Thank you,
> > Andrey

Re: how to use BindImplementation with named parameters

Posted by Markus Weimer <ma...@weimo.de>.
Hi,

you can annotate `IBarHandler` with
`Org.Apache.REEF.Tang.Annotations.DefaultImplementationAttribute` to
tell Tang which default implementation to use for an interface. For an
example, see `Org.Apache.REEF.IO.TempFileCreation.ITempFileCreator`:

```
    [DefaultImplementation(typeof(TempFileCreator))]
    public interface ITempFileCreator{...}
```

Hope this helps,

Markus

On Fri, Oct 7, 2016 at 7:59 AM, Andrey Meleshko <an...@microsoft.com> wrote:
> Hi ,
> I have a question on how to create app dependency with default behavior overridable by app client.
> The dependency can be defined as interface and client can use BindImplementation to provide specific implementation for the interface.
> Etc, this works:
>
> In App constructor:
>         [Inject]
>         public FooClass(IBarHandler barHandler)
> {
> ...
> }
>
> Client:
> var barHandlerConfig = TangFactory.GetTang().NewConfigurationBuilder()
>                 .BindImplementation(
>                     GenericType<IBarHandler >.Class,
>                     GenericType<MyBarHandler>.Class)
>                 .Build();
>
>
> But I am not sure how to define default implementation for the interface (in case if client did not provide any).
> I know default implementation can be defined if the dependency is declared as named parameter,
> but then BindImplemetation as declared above doesn't seem to work
> (client implementation is ignored and default implementation is invoked all the time)
> Any help appreciated.
>
> Thank you,
> Andrey