Showing posts with label ioc. Show all posts
Showing posts with label ioc. Show all posts

Tuesday, March 13, 2007

Guice Boost

Guice "Guice injects constructors, fields and methods (any methods with any number of arguments, not just setters). Guice includes advanced features such as custom scopes, circular dependencies, static member injection, Spring integration, and AOP Alliance method interception, most of which you can ignore until you need it."

The moment I saw IoC wiring in code I thought of one man's wish for better IoC wiring in code (we were using Spring at the time).

Behold:

public class MyModule extends AbstractModule {
protected void configure() {i
bind(Service.class)
.to(ServiceImpl.class)
.in(Scopes.SINGLETON);
}
}


And how to inject the Service implementation:

public class Client {

private final Service service;

@Inject
public Client(Service service) {
this.service = service;
}

public void go() {
service.go();
}
}


Now you can test drive IoC wiring without feeling fruity (test driving XML always felt weird).

Which reminded me of a quote by Victoria Livschitz:

We now have a generation of young programmers who think of software in terms of angle brackets. An enormous mess of XML documents that are now being created by enterprises at an alarming rate will be haunting our industry for decades. With all that excitement, no one seems to have the slightest interest in basic computer science.


She was talking about web services but it seems applicable to IoC wiring too.

Via, Guice 1.0 Released.

Update: Similarly, Configuration in Java - It sure beats XML!. Mentions, using Java 6's hotpatch to reload the configuration too.

Tuesday, January 16, 2007

Spring's ObjectFactory

More fun with Spring scopes "This time I would like to show another interesting application for the custom bean scopes. In this case conversation is bound to the page in web application (or even to each unique set of request parameters for that page). Practical examples include per-page caching of the static data (i.e. for Ajax use), allow page visitors to interact or edit page content together and many others..."

The Springframework reference has another example of using the ObjectFactory, "Knowing who you are". As noted, it doesn't move away from requiring Spring but it is slightly better than being BeanFactoryAware. The limiting factor on this is that if you need to create two types of objects you need two object factories. A generic object factory could do the trick and generics could be used to remove the casting too.

A recent InfoQ article, "Spring 2.0: What's New and Why it Matters", also has a section about the new bean scopes.

Wednesday, June 21, 2006

Spring Best Practice

Leverage Application Framework Integration with Spring "Inversion of Control with Dependency Injection and support for Declarative Transaction are Spring framework features that can greatly reduce the complexity of integrating different application layers as well as fosters better OO design and greatly improves testability."

Wednesday, April 26, 2006

Links


  • On setters, constructors and modelling reality "In my experience there are almost no situations where a setter method is a good idea...I often see setters used where information relating to an object is not known at construction time, and needs to be added later. This is usually a symptom of choosing the wrong class to store that data...Note that just because all the fields are final and there are no setters, this doesn't mean that the object is immutable. It just means that its behaviour more closely models reality, and your interactions with it will be far more meaningful."

  • ABC Video On Demand including all your Chaser needs.

  • Agile Development: Schema Evolution "One of the characteristics of agile development is that designs are often refactored. As result, the data model may evolve during the lifetime of a project. What happens if the data model of the db4o databases is changed?"

  • Smalltalk to Java - the Good, the Bad, and the Unbelievably Ugly "The Smalltalk code was "too Smalltalky" for the translation (again, design is language specific IMHO). They had DNU handlers, descendants from nil, #perform, etc. Then there's the whole utility class issue - final classes in Java (String, Date) that would simply be subclassed in Smalltalk. Joshua Bloch, call your office :/"

  • Running Code Doesn't Lie "This statement pretty much sums up why agile software development practices like eXtreme Programming and Getting Real are so powerful. Instead of business analysts or programmers writing pages of documentation for what they think the system does, the system tells you what it does. I like to call this a Self Describing System."

  • TestDox "TestDox creates simple documentation from the method names in JUnit test cases."

  • Multi-core Processors: transputers reborn "
    In the Eighties, there were only a small number of people who were interested in fast drawing of Mandelbrot sets. The same is true today. But today, everyone is interested in having their web sites scale well in terms of performance and in terms of price. The killer application of parallel computing is all around us. It is called the Web."

Friday, April 21, 2006

Sharing Instances in Spring

So I had a fairly basic problem that I probably have solved before but wasn't able to remember and took some time to work out.

I had three objects A, B and C. Both B and C need to share the same instance of A. But I want to create multiple Bs and Cs – so they are not singletons. I also did not want other Bs and Cs to share the one instance of A so making A a singleton was also out of the question.

There’s several ways I came up with but none of which I was particularly happy with:
1/ Flatten out the dependencies – make C depend on B which depends on A for instance.
2/ Make A a singleton and create bunches Bs and Cs by using multiple BeanFactories.
3/ I could throw away dependency injection and let the objects using B and C construct/set-up B and C using A.

The solution that I finally settled on is creating a BCFactory (interface) that requires A, constructs B and C (in the implementation) and has getB() and getC() methods.