StructureMap Constructor setup with additional parameters

532 views
Skip to first unread message

John Bagagem

unread,
May 28, 2015, 7:22:14 PM5/28/15
to structure...@googlegroups.com
So basically I have the following - a basic IoC setup with Constructor Injection:

public interface IValuesManager
{
     void DoSomething();   
}
 
public class ValuesController : ApiController
{
     private IValuesManager _valuesManager { get; set; }
     public ValuesController(IValuesManager values)
     {
          this._valuesManager = values;
     }
     public void DoSomething()
     {
     }
}

This is being registered like so:
For<IValuesManager>().Use<ValuesManager>();


So far so good, all working OK!

------------------------------------------------------------------------------------------------------------------------

I have some external properties that need to be injected into this class and I will only know the value at runtime. I'll just stick to one property: "UserId". So I will now have the following:

public interface IValuesManager
{
     Guid UserId { get; set; } 
     void DoSomething();   
}

public class ValuesController : ApiController
{
     private IValuesManager _valuesManager { get; set; }
     private int _userId { get; set; }
 
     public ValuesController(IValuesManager values, Guid userId)
     {
          this._valuesManager = values;
this._userId = userId; 
     }

     public void DoSomething()
     {
     }
     ...
     ...
}

How can I get this property to be injected given that I don't have a value for it? I've seen some examples of but always with a known value, like so:
For<IValuesManager>()
   .Use<ValuesManager>()
   .Ctor<Guid>("UserId")
   .Is(<< some Guid value goes here>>); 

------------------------------------------------------------------------------------------------------------------------

Any help guys?

Thanks,
JB
 

Kevin Miller

unread,
May 28, 2015, 7:38:32 PM5/28/15
to structure...@googlegroups.com, structure...@googlegroups.com
Things that you only know at runtime are usually just arguments to methods. What is driving your need for the container to resolve runtime values?



--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/structuremap-users/a451ab40-46e8-4e2a-8f36-ede1a5d0d103%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Bagagem

unread,
May 28, 2015, 7:43:44 PM5/28/15
to structure...@googlegroups.com
Hi Kev,

This is going to be a WebApi project that other systems are going to connect to.
The UserId is essential in every call so we want to just want it "registered" as opposed to having to pass it in for every call.
In my example I'm using 1 param, but in reality there are more.


JB
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.

Kevin Miller

unread,
May 28, 2015, 7:47:26 PM5/28/15
to structure...@googlegroups.com, structure...@googlegroups.com
Sounds like a good use for child containers. Hasn't this already been solved? Another developer at my work is using WebAPI with structure map 3. 



To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.

To post to this group, send email to structure...@googlegroups.com.

John Bagagem

unread,
May 28, 2015, 7:50:45 PM5/28/15
to structure...@googlegroups.com
Any chance you can provide an example of how to solve this?
I've tried a number of approaches but always failing :(
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.

John Bagagem

unread,
May 28, 2015, 8:08:28 PM5/28/15
to structure...@googlegroups.com
Btw, I'm using StructureMap.WebApi2

Kevin Miller

unread,
May 28, 2015, 9:25:55 PM5/28/15
to structure...@googlegroups.com, structure...@googlegroups.com
John,

Are you already using the service resolver? Something like http://www.nuget.org/packages/WebApiContrib.IoC.StructureMap

Where is this Guid coming from? 

Quite a few ways to slice this. Typically for something like this I have a type that wraps the primitive. 

public interface ICurrentUser  
{  
  public Guid Id {get;set;}
public class CurrentUser : ICurrentUser 
{
  public Guid Id {get;set;}
  public CurrentUser(Guid id) {
    Id = id;
  }
}

Then during my container setup I create a factory setup.  This is all sans compiler so forgive me if I mess something up.

public static class IoC 
{
  public IContainer Bootstrap() 
  {
     return new Container(c=>{
       c.For<ICurrentUser>().Use(“user factory”, c=>{
         var id = GetUserGuid(); //your call on how this happens
         return new CurrentUser(id);
       });
      }); 
  }
}

Next you give this container to however you are wiring up StructureMap to WebAPI. Google has lots of answers on this topic. I like the child container implemetations. http://benfoster.io/blog/per-request-dependencies-in-aspnet-web-api-using-structuremap. In this case you could likely inject the instance of your ICurrentUser type into the child container.

// childContainer.Inject(currentUserInstance);

Hope this helps. I have never actually used WebAPI (yet). I have a project coming down the pipe where It may be forced on me.
 




To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.

To post to this group, send email to structure...@googlegroups.com.

John Bagagem

unread,
May 29, 2015, 4:58:33 PM5/29/15
to structure...@googlegroups.com
Hi Kev,

Thanks for your feedback. I think I managed to get this working with a mix of what you mentioned and other things I saw online, mainly here:
http://www.theabsentmindedcoder.com/2010/05/structure-map-26-constructor-arguments.html

Still going to have to wait and see until we integrate all the bits and pieces to be 100% sure.


Thanks once again for your help,

JB
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.

Jeremy D. Miller

unread,
May 30, 2015, 10:11:46 AM5/30/15
to structure...@googlegroups.com
You'd probably wrap that with your own factory/builder interface. I *think* SM will finally include typed factories in a 4.0 release late this year, but that might only be targeting DNX. 
 



On Friday, May 29, 2015 4:40 PM, John Bagagem <john...@gmail.com> wrote:


Hi Kev,
Next you give this container to however you are wiring up StructureMap to WebAPI. Google has lots of answers on this topic. I like the child container implemetations. http://benfoster.io/blog/per- request-dependencies-in- aspnet-web-api-using- structuremap. In this case you could likely inject the instance of your ICurrentUser type into the child container.

// childContainer.Inject( currentUserInstance);

Hope this helps. I have never actually used WebAPI (yet). I have a project coming down the pipe where It may be forced on me.
 




On Thu, May 28, 2015 at 7:09 PM, John Bagagem <john...@gmail.com> wrote:
Btw, I'm using StructureMap.WebApi2


On Thursday, May 28, 2015 at 4:50:45 PM UTC-7, John Bagagem wrote:
Any chance you can provide an example of how to solve this?
I've tried a number of approaches but always failing :(


On Thursday, May 28, 2015 at 4:47:26 PM UTC-7, KevM wrote:
Sounds like a good use for child containers. Hasn't this already been solved? Another developer at my work is using WebAPI with structure map 3. 



On Thu, May 28, 2015 at 6:44 PM, John Bagagem <john...@gmail.com> wrote:
Hi Kev,

This is going to be a WebApi project that other systems are going to connect to.
The UserId is essential in every call so we want to just want it "registered" as opposed to having to pass it in for every call.
In my example I'm using 1 param, but in reality there are more.


JB

On Thursday, May 28, 2015 at 4:38:32 PM UTC-7, KevM wrote:
Things that you only know at runtime are usually just arguments to methods. What is driving your need for the container to resolve runtime values?



On Thu, May 28, 2015 at 6:22 PM, John Bagagem <john...@gmail.com> wrote:
So basically I have the following - a basic IoC setup with Constructor Injection:

public interface IValuesManager
{
     void DoSomething();   
}
 
public class ValuesController : ApiController
{
     private IValuesManager _valuesManager { get; set; }
     public ValuesController( IValuesManager values)

     {
          this._valuesManager = values;
     }
     public void DoSomething()
     {
     }
}

This is being registered like so:
For<IValuesManager>().Use< ValuesManager>();


So far so good, all working OK!

------------------------------ ------------------------------ ------------------------------ ------------------------------

I have some external properties that need to be injected into this class and I will only know the value at runtime. I'll just stick to one property: "UserId". So I will now have the following:

public interface IValuesManager
{
     Guid UserId { get; set; } 
     void DoSomething();   
}

public class ValuesController : ApiController
{
     private IValuesManager _valuesManager { get; set; }
     private int _userId { get; set; }
 
     public ValuesController( IValuesManager values, Guid userId)
     {
          this._valuesManager = values;
this._userId = userId; 
     }

     public void DoSomething()
     {
     }
     ...
     ...
}

How can I get this property to be injected given that I don't have a value for it? I've seen some examples of but always with a known value, like so:
For<IValuesManager>()
   .Use<ValuesManager>()
   .Ctor<Guid>("UserId")
   .Is(<< some Guid value goes here>>); 

------------------------------ ------------------------------ ------------------------------ ------------------------------

Any help guys?

Thanks,
JB
 
--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+ unsub...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/ msgid/structuremap-users/ a451ab40-46e8-4e2a-8f36- ede1a5d0d103%40googlegroups. com.
For more options, visit https://groups.google.com/d/ optout.
--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+ unsub...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.

For more options, visit https://groups.google.com/d/ optout.
--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+ unsub...@googlegroups.com.
To post to this group, send email to structure...@ googlegroups.com.

For more options, visit https://groups.google.com/d/ optout.
--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.

To post to this group, send email to structure...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages