1

I am unsure the right way to do this. here is a simple representation of what I am trying to do, I will explain in a second step why I am doing this.

Two objects A and B need to be injected into class myClass. A can be injected with no parameters, fine. Then B needs to be created with a reference to A. How do I do that in spring?

the reason I am doing this is Entity Framework - I have a context object that is required in my service layer (persistence,..). My repository layers (DAL) need this paraneter passed in constructor to be created so they can access the DB. At the end of the service layer procedure, I need to be able to access my context object to save the transaction. Since I want to be able to mock the whole thing, i want both objects to be injected.

Cœur
  • 37,241
  • 25
  • 195
  • 267
NicolasCOM
  • 11
  • 1
  • I am thinking of creating A with injection, then generate the second object once needed (get) and call the getObject with the onject A as a parameter. public object GetObject( string name, object[] arguments ) – NicolasCOM Aug 16 '13 at 17:28
  • This is supported for NHibernate; transaction management is implemented using spring.net aop. Afaik this has not been implemented for entity framework. Maybe you can look at the NHibernate implementation to get some ideas; although I don't think it is trivial to implement transaction management for entity framework. – Marijn Aug 16 '13 at 19:13
  • There is a spring.net project for EF integration: https://jira.springsource.org/browse/SPRNET/component/11481. However, it does not any activity. – Marijn Aug 21 '13 at 09:37

1 Answers1

0

Works using public object GetObject( string name, object[] arguments)

//_uow is my object containing my CONTEXT (DB ACCESS)
 _configRepo = (ICMConfigurationKVRepo)ContextRegistry.GetContext().GetObject("SLayer_IConfigurationKVRepo",new object[] { _uow });

Spring configuration:

<object name="SLayer_IConfigurationKVRepo" 
        type="xxx.Repository.ConfigurationKVRepo, Repository" 
        autowire="constructor">
Marijn
  • 10,367
  • 5
  • 59
  • 80
NicolasCOM
  • 11
  • 1
  • It's great that you post your solution back here, as it might be helpful to others. The pattern you are using here looks like the service locator: you use the spring context to retrieve an object. Some consider this [an anti-pattern that should be avoided](http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/). – Marijn Aug 24 '13 at 06:57