Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Injecting appSettings
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Andrew Davey  
View profile  
 More options Jun 24 2008, 7:19 am
From: Andrew Davey <a.j.da...@gmail.com>
Date: Tue, 24 Jun 2008 04:19:42 -0700 (PDT)
Local: Tues, Jun 24 2008 7:19 am
Subject: Injecting appSettings
Something cool I created today with Ninject...

I created a provider that allows me to do this:
class Foo {
  public Foo(AppSetting bar) { // AppSetting is a simple wrapper
around a string
    Console.Write(bar.Value)
  }

}

where web.config contains:
<appSettings> <add key="bar" value="123" /> </appSettings>

The single line to bind the provider:
Bind<AppSetting>().ToProvider<AppSettingProvider>();

An finally the provider:
class AppSettingProvider : Ninject.Core.Creation.IProvider
{
    public object Create(Ninject.Core.Activation.IContext context)
    {
        return new
AppSetting(WebConfigurationManager.AppSettings[context.Member.Name]);
    }

    public Type GetImplementationType(Ninject.Core.Activation.IContext
context)
    {
        return typeof(AppSetting);
    }

    public bool IsCompatibleWith(Ninject.Core.Activation.IContext
context)
    {
        return
WebConfigurationManager.AppSettings[context.Member.Name] != null;
    }

    public Type Prototype
    {
        get { return typeof(AppSetting); }
    }

}

This was so simple and easy to figure out - beautiful!
I'm more impressed with Ninject each time I use it.
 :)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nate Kohari  
View profile  
 More options Jun 24 2008, 8:57 am
From: "Nate Kohari" <nkoh...@gmail.com>
Date: Tue, 24 Jun 2008 08:57:57 -0400
Local: Tues, Jun 24 2008 8:57 am
Subject: Re: Injecting appSettings

Andrew:

Nice! That's pretty cool stuff. One thing you might want to change is to use
context.Target.Name rather than context.Member.Name. The member is the
member (constructor, property, method, or field) that's being injected,
whereas the target is the actual point that is receiving the value. In the
case of properties and fields, the member and the target are identical, but
for constructors and methods, the target is the *argument* that is receiving
the value. If you use context.Member.Name, you will end up with the name of
the constructor or method rather than the argument.

I'm working on an extension to support configuration injection, and I'll
keep this in mind to see if I can support something like it. Thanks again!

-Nate


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Hinze  
View profile  
 More options Jun 24 2008, 9:05 am
From: "Matt Hinze" <mhi...@gmail.com>
Date: Tue, 24 Jun 2008 08:05:33 -0500
Local: Tues, Jun 24 2008 9:05 am
Subject: Re: Injecting appSettings

Cool. Yeah, I'm now getting into providers too.. very fun.

This is the one I whipped up to allow injecting the current IIdentity into a
service class in an ASP.NET app:

public class WebIdentityProvider : SimpleProvider<IIdentity>
{
    protected override IIdentity CreateInstance(IContext context)
    {
        return HttpContext.Current.User.Identity;
    }

    public override bool IsCompatibleWith(IContext context)
    {
        return HttpContext.Current != null;
    }

}

Using IoC with HttpContext wasn't something I was able to figure out
before...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nate Kohari  
View profile  
 More options Jun 24 2008, 9:15 am
From: "Nate Kohari" <nkoh...@gmail.com>
Date: Tue, 24 Jun 2008 09:15:30 -0400
Local: Tues, Jun 24 2008 9:15 am
Subject: Re: Injecting appSettings

Matt:

Yeah, providers are great, they can act like a factory or you can use them
to create your own customizable resolution mechanism. Nice use of
IsCompatibleWith(), too -- as I remember, I added it to support generics,
and I hadn't considered using it in that way. You just taught me something
about my own code. ;)

Thanks,
Nate


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Davey  
View profile  
 More options Jun 24 2008, 9:37 am
From: Andrew Davey <a.j.da...@gmail.com>
Date: Tue, 24 Jun 2008 06:37:32 -0700 (PDT)
Local: Tues, Jun 24 2008 9:37 am
Subject: Re: Injecting appSettings
Thanks for the heads up - I was using property injection in my
application so hadn't hit that issue. I've changed it now though to be
safe.
Providers (and Ninject in general) seem to offer lots of power. I'm
interested to see what other clever uses people invent.

On Jun 24, 1:57 pm, "Nate Kohari" <nkoh...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Duncan Godwin  
View profile  
 More options Jun 25 2008, 5:14 pm
From: Duncan Godwin <duncangod...@gmail.com>
Date: Wed, 25 Jun 2008 14:14:40 -0700 (PDT)
Local: Wed, Jun 25 2008 5:14 pm
Subject: Re: Injecting appSettings
An alternative for injecting the HttpContext is using the
ToFactoryMethod(() => HttpContext.Current.User.Identity);

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Davey  
View profile  
 More options Jun 27 2008, 5:49 pm
From: Andrew Davey <a.j.da...@gmail.com>
Date: Fri, 27 Jun 2008 14:49:45 -0700 (PDT)
Local: Fri, Jun 27 2008 5:49 pm
Subject: Re: Injecting appSettings
Agreed, ToFactoryMethod is a very handy shortcut.

Something else that might be cool for ASP.NET is injecting request
cookies into a Controller class, using a provider.
This makes for much easier testing as well since there is no need to
create a mock context, request and cookie collection.

On Jun 25, 10:14 pm, Duncan Godwin <duncangod...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google