Skip to content
Suremaker edited this page Mar 23, 2013 · 3 revisions

Setter Injection is a common implementation of Dependency Injection pattern. It allows to configure object by injecting its dependencies using property setters. Spring.FluentContext offers two ways of binding properties. First uses lambda expression to determine property name, and second allows to use literal with property name.

The BindProperty() method allows to bind property to:

  • value (like specific text or number)
  • registered definition (other object in context)
  • inline definition (unnamed object, not accessible outside this binding)

The following example presents how to bind property to specific value:

ctx.RegisterDefault<Address>()
	.BindProperty(a => a.City).ToValue("London")
	.BindProperty(a => a.PostCode).ToValue("AB1 12CD")
	.BindProperty(a => a.Street).ToValue("Some Street");

The BindProperty() method is used to determine property name that will be used in binding, while ToValue() method binds it to specific value. It is worth to say that ToValue() method is type safe, so passed value has to be of type assignable to selected property.

Next example shows how to bind property to registered default definition for given class. In following example Person.Address property will be bound to default Address object definition:

ctx.RegisterDefault<Person>()
	.BindProperty(p => p.Name).ToValue("John")
	.BindProperty(p => p.Address).ToRegisteredDefault(); 

ctx.RegisterDefault<Address>();

This example shows how to bind property to default object definition for deriving/ implementing type:

ctx.RegisterDefault<Cat>();
ctx.RegisterDefault<Tortoise>();

ctx.RegisterNamed<PetOwner>("catOwner")
	.BindProperty(n => n.Pet).ToRegisteredDefaultOfType<Cat>();

ctx.RegisterNamed<PetOwner>("tortoiseOwner")
	.BindProperty(n => n.Pet).ToRegisteredDefaultOfType<Tortoise>();

The following one shows how to bind property to named definition:

ctx.RegisterDefault<Person>()
	.BindProperty(p => p.Name).ToValue("John")
	.BindProperty(p => p.Address).ToRegistered("address");

ctx.RegisterNamed<Address>("address");

This one shows how to bind property to uniquely named definition:

var addressReference = ctx.RegisterUniquelyNamed<Address>()
	.GetReference();

ctx.RegisterDefault<Person>()
	.BindProperty(p => p.Name).ToValue("John")
	.BindProperty(p => p.Address).ToRegistered(addressReference);

The final one shows how to bind property to inline definition:

ctx.RegisterDefault<Person>()
	.BindProperty(p => p.Name).ToValue("John")
	.BindProperty(p => p.Address).ToInlineDefinition<Address>(
		def => def.BindProperty(a => a.City).ToValue("London")
		.BindProperty(a => a.PostCode).ToValue("AB1 12CD")
		.BindProperty(a => a.Street).ToValue("Some Street"));

It is worth to say that ToInlineDefinition() method is generic and allows to inject object definition for deriving/ implemented type.

There is also second version of BindProperty() that allows to determine property name by literal. It has been introduced as a workaround for properties with public setter and non-public getter, which cannot be selected using lambda expression.

There is an example usage of it:

ctx.RegisterDefault<Person>()
	.BindPropertyNamed<string>("Name").ToValue("John");

Finally, it is possible to bind property with To() method, described in chapter 15. General definition binding.

Continue reading: 6. Constructor Injection

Clone this wiki locally