Since the new version of StructureMap came out a few weeks
ago I have been playing around with it to see what it can do.
So far I have been very impressed and have
been very happy with the support I have received from Jeremy (the author) when I
had questions/issues.
During my exploring I have been looking at the different
ways to configure StrucutreMap.
There
are 3 different ways to setup PluginGraph 1) Inline in code, 2) Via Attributes
and 3) using the configuration file supported by the tool.
In this post I will go over how to configure
your PluginGraph via attributes.
I plan
on posting on the other ones as well in the future.
Getting started
**** NOTE **** One thing, although we are using attributes
in this example I still had to create a config file for the actual assembly to
be scanned. I hope to figure out a way around
this, but at this time I don’t know of one.
**** UPDATE **** Thanks to i was able to get this to work without the use of any config file. You will need to call StructureMapConfiguration.ScanAssemblies().IncludeTheCallingAssembly() during the startup of your application
Basic Configuration file (StructureMap.config)
Basic setup to use the configuration file
PluginFamily Attribute Usage
Explained:
- “ShoppingCartItem”
– is the key given on the Pluggable attribute. This can be anything. Here I made it represent the concrete
type name
- “SouceType”
– here I used MemoryMentoSource as the default.
- “IsSingleton”
– here I want this object to NOT be a singleton, but I could set this to
true to have the ObjectBuilder return me the same instance very time.
Pluggable Attribute Usage
Explained:
- “ShoppingCartItem”
– is the key for the class. This
can be any thing, here I made it represent the concrete type name. This is the key that the PluginFamily
attribute class will look form.
Calling the object builder and building your instance
There you have it, hopefully a simple example on how to use StructorMap with Attributes