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 Video Library
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
View Events Video Library
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
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • SwiftData Dependency Injection in SwiftUI Application
  • Ensuring Reliable Microservice Deployment With Spring Boot Build Info Maven Plugin
  • jQuery vs. Angular: Common Differences You Must Know
  • Angular Best Practices For Developing Efficient and Reliable Web Applications

Trending

  • REST vs. Message Brokers: Choosing the Right Communication
  • Software Verification and Validation With Simple Examples
  • Build a Serverless App Fast With Zipper: Write TypeScript, Offload Everything Else
  • Best Practices for Writing Clean Java Code
  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.72K 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.

Related

  • SwiftData Dependency Injection in SwiftUI Application
  • Ensuring Reliable Microservice Deployment With Spring Boot Build Info Maven Plugin
  • jQuery vs. Angular: Common Differences You Must Know
  • Angular Best Practices For Developing Efficient and Reliable Web Applications

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

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: