DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Property Injection with StructureMap

Property Injection with StructureMap

Dane Morgridge user avatar by
Dane Morgridge
·
Aug. 06, 10 · Interview
Like (0)
Save
Tweet
Share
7.57K Views

Join the DZone community and get the full member experience.

Join For Free
A few weeks back, I gave an introduction to dependency injection with StructureMap and then showed how to use it to do constructor injection. In most cases, I use constructor injection, but there are some cases where I need a parameter-less constructor so my only option is to use a property.  StructureMap supports this but you have to add a little bit more code to make it work.

Just for review, here is the interface class from previous articles:
public class RealStuff : IStuff
{
public string Name { get; set; }
public void DoStuff()
{
Console.WriteLine("Real Stuff");
}
}
And the class that uses the interface but as a property instead of a constructor:
public class ClassThatUsesStuff
{
public IStuff Stuff { get;set; }
}
I will be using the same initialization logic as I have previously:
ObjectFactory.Initialize(x =>
{
x.For<IStuff>().Use<RealStuff>();
});
It's pretty cool because I don't have to change my initialization logic or even the code that gets the instance of a concrete class:
IStuff classThatUsesStuff = ObjectFactory.GetInstance<ClassThatUsesStuff>();
Now if I run the above code, it won't work because StructureMap doesn't pick up properties quite as easily as it does constructors.  To make it work all we have to do is simply tag the property with a StructureMap [SetterProperty] attribute:
public class ClassThatUsesStuff
{
[SetterAttribute]
public IStuff Stuff { get;set; }
}
If I run the code to get the concrete class, the property will be injected just as I would expect. This does come with a caveat;  The ClassThatUsesStuff, now has to carry a dependency on StructureMap itself, which may not be desired depending on your situation.  It requires us to add a #using statement for StructureMap.Attributes. It is this reason why I tend to lean more towards constructor injection over property injection.  If you don't mind carrying the extra dependency, then this method will work fine and I do have several projects where I use a combination of both.  It really boils down to what your individual needs are and if you asked which method I would recommend, I would have to give the classic developer answer of "It depends".  I do tend to prefer constructor injection, but when the scenario requires, property injection is a perfect alternative.
Dependency injection Property (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A First Look at Neon
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey
  • What Is API-First?
  • 10 Things to Know When Using SHACL With GraphDB

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: