Initialize Object Properties

49 views
Skip to first unread message

Shapper

unread,
Mar 27, 2013, 9:19:12 PM3/27/13
to structure...@googlegroups.com

Hello,

I am injecting in my services a global Settings interface, as Singleton, using StructureMap:

public interface ISettings {
  LoggerSettings Logger { get; }
} // ISettings

public class LoggerSettings {
  public String Levels { get { return ConfigurationManager.AppSettings["Logger.Levels"]; } }
  public const String Report = "te...@xyz.com";
} // LoggerSettings

public class Settings : ISettings {
  public LoggerSettings Logger { get; private set; }
} // Settings

And as SM configuration I have:

For<ISettings>().Singleton().Use<Settings>();

I am able to inject this object but when I check the injected object its property Logger is null ... How can I have SM to initialize the object properties?

Am I missing something?

Thank you,
Miguel

Jeremy D. Miller

unread,
Mar 28, 2013, 6:58:21 AM3/28/13
to structure...@googlegroups.com
This is still relevant to StructureMap 2.6 (and 3 for that matter):  It wouldn't help you even it was injected because it gets involved before the behavior is pulled from the container.

StructureMap does not do setter injection automatically.  You'll have to explicitly enable setter injection, but in this case it won't work for you because the setter in question is private.

Honestly?  If I were you I'd just inject LoggerSettings directly as a singleton and not worry about anything else.
 
Jeremy D. Miller
The Shade Tree Developer
jeremy...@yahoo.com



From: Shapper <mdm...@gmail.com>
To: structure...@googlegroups.com
Sent: Wed, March 27, 2013 8:19:14 PM
Subject: [sm-users] Initialize Object Properties
--
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/msg/structuremap-users/-/vkw2xAqCiKcJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Shapper

unread,
Mar 28, 2013, 8:28:02 AM3/28/13
to structure...@googlegroups.com
Yes, that was one of my ideas:

To have a Settings : ISettings with a constructor that takes LoggerSettings, MailerSettings and so on, and initialize the properties.

I would inject LoggerSettings and MailerSettings as Singletons. And Settings also as Singleton in all my services.

But do I need to created interfaces for LoggerSettings and MailerSettings or can I simply use it as follows:


     For<ISettings>().Singleton().Use<Settings>();
    
     For<LoggerSettings>().Singleton().Use<LoggerSettings>();
     For<MailerSettings>().Singleton().Use<MailerSettings>();

I am asking this because it seems to working even if not using interfaces.


A side question: any ETA on StructureMap 3?

Thank You,
Miguel



On Thursday, March 28, 2013 10:58:21 AM UTC, Jeremy Miller wrote:
This is still relevant to StructureMap 2.6 (and 3 for that matter):  It wouldn't help you even it was injected because it gets involved before the behavior is pulled from the container.

StructureMap does not do setter injection automatically.  You'll have to explicitly enable setter injection, but in this case it won't work for you because the setter in question is private.

Honestly?  If I were you I'd just inject LoggerSettings directly as a singleton and not worry about anything else.
 
Jeremy D. Miller



From: Shapper

To: structure...@googlegroups.com
Sent: Wed, March 27, 2013 8:19:14 PM
Subject: [sm-users] Initialize Object Properties

Hello,

I am injecting in my services a global Settings interface, as Singleton, using StructureMap:

public interface ISettings {
  LoggerSettings Logger { get; }
} // ISettings

public class LoggerSettings {
  public String Levels { get { return ConfigurationManager.AppSettings["Logger.Levels"]; } }
  public const String Report = "te...@xyz.com";
} // LoggerSettings

public class Settings : ISettings {
  public LoggerSettings Logger { get; private set; }
} // Settings

And as SM configuration I have:

For<ISettings>().Singleton().Use<Settings>();

I am able to inject this object but when I check the injected object its property Logger is null ... How can I have SM to initialize the object properties?

Am I missing something?

Thank you,
Miguel

--
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,
Mar 28, 2013, 8:53:52 AM3/28/13
to structure...@googlegroups.com
See the link I meant to send you the first time:  http://docs.structuremap.net/ConstructorAndSetterInjection.htm

Setter injection is NOT a first class citizen in SM and never will be.  It will take you just a bit more effort to enable the setters.

SM 3 ETA?  I had a head of steam, but I'm having to shift to fubumvc & storyteller documentation efforts for a little while.  May/June sounds about right.  Most of the hard work is done though.

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