views:

129

answers:

3

Hello!

As I understand IoC-container is helpful in creation of application-level objects like services and factories. But domain-level objects should be created manually. Spring's manual tells us: "Typically one does not configure fine-grained domain objects in the container, because it is usually the responsibility of DAOs and business logic to create/load domain objects."

Well. But what if my domain "fine-grained" object depends on some application-level object. For example I have an UserViewer(User user, UserConstants constants) class. There user is domain object which cannot be injected, but UserViewer also needs UserConstants which is high-level object injected by IoC-container.

I want to inject UserConstants from the IoC-container, but I also need a transient runtime parameter User here.

What is wrong with the design?

Thanks in advance!

UPDATE

It seems I was not precise enough with my question. What I really need is an example how to do this:

create instance of class UserViewer(User user, UserService service), where user is passed as the parameter and service is injected from IoC.

If I inject UserViewer viewer then how do I pass user to it?

If I create UserViewer viewer manually then how do I pass service to it?

+4  A: 

there's nothing wrong with this design. you use Factories for that, which have one leg in the domain, one leg in infrastructure.

You can either write them manually, or have the container do that for you, by things like TypedFactoryFacility in Windsor.

Also when your domain objects come from persistence layer you can plug your container there to inject the services they require (NHibernate can do that).

Krzysztof Koźmic
+2  A: 
KLE
Well. But what is UserViewer? Is it a domain or application object?It is not POJO, but it seems not to be a real application service.It needs both access to application objects and to POJO object. How do I instantiate it? Or should I only pass POJO as a parameter? This is often inconvenient.
Andrey
@Andrey 1. Any code can use the pojos, application-objects are no exception. 2. For instanciation, I gave a different answer.
KLE
KLE!At first I didn't get what you mean, but now I begin to understand. Thank you!I have another question: this POJO + Services model is nice, but isn't it too procedural and anti-OOP?
Andrey
@Andrey You're welcome :-) Concerning your 'procedural' question, I understand your concern. However, it is not the case. The comment zone is too small to give an analysis of why it is that way ... But you have some explanation in my answer already... ;-)
KLE
A: 

For your update:

create instance of class UserViewer(User user, UserService service), where user is passed as the parameter and service is injected from IoC.

If I inject UserViewer viewer then how do I pass user to it?

If I create UserViewer viewer manually then how do I pass service to it?

In that case, you need a factory method (possibly on a Factory or Locator of yours). It could look at follow, separating the two parts:

public UserViewer createUserViewer(User user) {
   UserViewer viewer = instantiateBean(UserViewer.class); 
   viewer.setUser(user);
   return viewer;
}

private <E> E instantiateBean(Class<E> clazz) {
   // call the IoC container to create and inject a bean
}
KLE