Archive for February, 2008

Windsor: Component Burden described as a (bad) side effect

February 26th, 2008

This blog entry is fantastic. It explains how Windsor (well, MicroKernel) deals with non-singletons that are disposable. In the end, the author makes an interesting test:

public class DisposableCon : IDisposable {
 
   public ITest ITest;
 
   public DisposableCon(ITest iTest) {
      ITest = iTest;
   }
 
   public void Dispose() {
 
   }
 }

This class simply declare a Constructor dependency, and implements IDisposable, but in the dispose does not do any stuff. The following test shows you that the container does not keeps a reference nor dispose the inner ITest object.

DisposableCon tran;
using (WindsorContainer ioc = new WindsorContainer(new XmlInterpreter(“config1.xml”))) {
   tran = ioc.Resolve(“TransientDisposableCon”);
}
Assert.IsFalse(tran.ITest.AProperty);

So it is your duty to be sure that the inner objects injected by the container will be disposed correctly.

So this behavior is wrong! WRONG! You should not have to care about releasing the dependencies yourself, the container should keep track of them.

Explaining again. Suppose you have the following components:

DependencyA
  DependencyB
Service

Services depends on DependencyA, which depends on DependencyB. Let’s consider that all of them are transients.

When the “Service” is requested from the container, Windsor creates the dependencies in the bottom-top order:

1. Create DependencyB
2. Create DependencyA
3. Create Service

Service can be returned.

In an ideal world, when Service is released (container.Release(service) or container.Dispose()), Windsor needs to release all of them in the inverse order:

1. Dispose Service
2. Dispose DependencyA
3. Dispose DependencyB

DependencyA is the “burden” of Service. DependencyB is the “burden” of DependencyA. That’s what I call Component Burden. The container being able to keep track of these things that need to be release.

Of course, for 90% of the container usage, you’d use singletons, so that’s why the component burden – which is missing on Windsor and maybe on every other IoC container out there – is not missed that much. It’s only required for non-singletons components that have a decommission phase (for example, Disposable ones).

Anders has started working on this back in March of 2007. He renamed it to component responsability. The implementation wasn’t ideal, so I started reviewing it before applying. Never found the time to finish.

So, if you really want to dig into the internals of the MicroKernel, figure out an optimal strategy that will keep the “burden” only when necessary, make it beautiful, test it, license it as ASL 2.0 and send it to us, we would greatly appreciate it :-)

Let’s evangelize good architecture, everyone!

February 26th, 2008

Yeah, and by that I mean that you shouldn’t publish bad code. And if you do, dont forget to mention that this is just example code, and you’d do it differently in a real world app.

Not following? Let’s get practical:

[ControllerAction]
public void Find(string query, string format)
{ 
     var flickr = new Flickr(ConfigurationManager.AppSettings["flickr.api.key"]);
     var photos = flickr.PhotosSearch(query, TagMode.AnyTag, query, 40, 1);

     ViewData["query"] = query;
     ViewData["photos"] = photos;

     RenderView("results");
}

Nothing wrong with this code? Easily testable? No tight coupled dependencies? How would you test it?

Categories: General | Top Of Page | 25 Comments » |

Sometimes it pays off

February 26th, 2008

So last Friday we had our third progress meeting with our client. I confess I’ve never heard so many “this is spectacular” in my life. Really pays all the hard work we have been doing for a project that in the beginning I felt would be ugly, too UI intensive and not to mention impossible due to the close deadline.

Somehow we’ve managed to overcome the UI challenges and show them something better than what they have and exceed expectation.

Things like that make up for all the time I wish I have had become a journalist, or a chef, or a butcher, or a travel salesman.

Registering components on Windsor, the cool way

February 25th, 2008

Craig Neuwirt (no blog), one of the oldest Castle committers (I mean, he has been involved for a long time!) created a sweet way to register components on Windsor. I’ve been planning to blog about it for a long time, but I wanted to get some experience with it before.

So we basically provide this old fashioned family of methods:

AddComponent
AddComponentLifeStyle
AddComponentWithProperties

The AddComponentWithProperties is mislead. It means that you’re going to provide the custom properties for the components, those that are used by facilities and/or subsystems. Not the actual properties of the implementation.

Now, Craig added a simple Register method. The magic happens on the build you pass to the Register method:

container.Register( Component.For<IMyServiceContract>().ImplementedBy<MyServiceImpl>() );

As the Register accepts an array (with ParamArray attribute), you can use to make multiple registrations:

container.Register( 
    Component.For<IMyServiceContract>().ImplementedBy<MyServiceImpl>(), 
    Component.For<IMyOtherServiceContract>().ImplementedBy<MyServiceImpl>(), 
    Component.For<IMyKewlServiceContract>().ImplementedBy<MyServiceImpl>(), 
    Component.For<IMyNeatServiceContract>().ImplementedBy<MyServiceImpl>(), 
    Component.For<IMySweetServiceContract>().ImplementedBy<MyServiceImpl>()
);

I’m sure you want more. And you got it. The Component.For creates a ComponentRegistration instance. See its source to see that it does everything you can do through external configuration.
http://svn.castleproject.org:8080/svn/castle/trunk/InversionOfControl/Castle.MicroKernel/Registration/ComponentRegiatration.cs

More examples:

Specifying the name and a lifestyle:

container.Register( Component.For<IMyServiceContract>().
	Named("my.component").
	ImplementedBy<MyServiceImpl>().
	LifeStyle.PerThread
);

Adding a custom instance

container.Register( Component.For<IMyServiceContract>().
	Instance(new MyServiceImplementation() )
);

Setting up parameters:

container.Register( Component.For<IEmailSender>().
	ImplementedBy<SmtpEmailSender>().
	Parameters(
		Parameter.ForKey("host").Eq("my.smtp.host.name"), 
		Parameter.ForKey("port").Eq("155") )
);

Setting up service overrides:

container.Register( Component.For<ISpamSender>().
	ImplementedBy<MySpamSender>().
	ServiceOverrides(
		ServiceOverride.ForKey("emailSender").Eq("smtpsender") )
);

Now, the only weakness left to cover on Windsor is the damn Component Burden. Once it’s done, we’re ready to 1.0.

Yeah, right

February 25th, 2008

Quote from Pallermo

I don’t consider myself as an evangelist of the ASP.NET MVC Framework. I’m an evangelism of good architecture, test coverage and maintainability.

Castle’s mission which is untouched since the project started back in 2004:

To provide a simple set of tools to speed up the development of common enterprise and web applications while promoting good architecture.

Funny enough, Pallermo never seem to care about Castle. Suddenly he – and others – became the world’s expert in MVC, testability, maintainability and good architecture. I wonder, what have been using to develop web apps before the ASP.Net MVC? WebForms? If so, I’m not that keen to believe that they are what they claim.

Categories: General, wtf? | Top Of Page | 18 Comments » |

MonoRail – Return Binders

February 20th, 2008

I got tired of seeing actions like:

public void GetStates(string countryCode)
{
     ... get the states ...

    string js = Context.Services.JSONSerializer.Serialize(states);

    Response.ContentType = "application/json";
    RenderText(js);
}

They just don’t make sense. So I spent 20 minutes adding support for return binders. Now my code looks like:

[return: JSONReturnBinder]
public State[] GetStates(string countryCode)
{
     return stateRepository.FetchAllByCountry(new Country(countryCode));
}

I know, I know, the attributes associated with return looks weird, but take your complain to MS/C# team. You have to play with the cards you’re dealt.

A return binder, much like a parameter binder, is a class/attribute that implements the IReturnBinder interface, which has a single method: Bind. There you can go wild and do whatever you want with the return value. You could add to the property bag, example, or use it to figure out to where the user should be redirected (struts like), or like me, you just want to serialize some data in JSON format.

I’ve also added the ability to serialize only the properties I want:

[return: JSONReturnBinder(Properties="Id,Name")]
public State[] GetStates(string countryCode)
{
     return stateRepository.FetchAllByCountry(new Country(countryCode));
}

That saves bandwidth, not to mention that I dont expose the whole objects through the wire.

The code, if you’re wondering, is on my branch playground. I’m experimenting with more things, which I will blog soon.

Avalon Castle

February 20th, 2008

This is really old stuff. Just bumped into it:

https://issues.apache.org/jira/secure/IssueNavigator.jspa?reset=true&pid=10426&status=1

Categories: Castle | Top Of Page | No Comments » |

A breakthrough for music, its industry and artists?

February 19th, 2008

Few months ago Radiohead released their new album with a pay-if-you-want-and-how-much-you-want model. Nice, indeed – the album sucks by the way, I hate what they did with Big Ideas. A few weeks ago the “band” The Flashbulb released a new album on p2p networks before the “official” release, and included a very revealing Read me sort of file:

“Hello listener…downloader…pirate…pseudo-criminal…

If you can read this, then you’ve more than likely downloaded this album from a peer to peer network or torrent.

You probably expect the rest of this message to tell you that you’re hurting musicians and breaking just about every copyright law in the book. Well, it won’t tell you that.

What I would like to tell you is that my record label understands that a large portion of people pirate music because it is easier than buying it. CDs scratch easily, most pay-per-download sites have poor quality and shitty DRM protection, and vinyl is near impossible to find or ship without hassle.
In many cases I wonder why people buy CDs at all anymore. A few like the tangible artwork, some haven’t adapted to MP3s yet, but most do it because they have a profound love for music and want to support the artists making it. Kind of restores your faith in humanity for a moment eh?

So, now what?
Like the album? About to go “support the artist” on iTunes?
Well, don’t.
Alphabasic is currently in a legal battle against Apple because NONE of our material (Sublight Records included) receives a dime of royalty from the vast amount of sales iTunes has generated using our material.

Want to buy a CD just to show your support?
If you don’t particularly like CDs, don’t bother.
Retailers like Best Buy and Amazon spike the price so high that their cut is often 8 times higher than the artist’s. Besides, most CDs are made out of unrecyclable plastic and leave a nasty footprint in your environment.

If you do particularly like CDs, buy them from the label (in our case, alphabasic.com). After manufacturing costs are recuperated, our artists usually receive over 90% of the actual money coming out of your wallet.
In addition, all of our physical products are made out of 100% recycled material.

Want to show your support?
Go here and browse our library of lossless, DRM-free downloads.
Already have that?
Then feel free to donate whatever you want to your favorite artist. 100% will go directly to them.
Hell, you can even donate a penny just to thank the artist.

If you really like ‘The Flashbulb – Soundtrack To A Vacant Life’ and want to show your support without it going to greedy retailers, distributors, and coked-up label reps, then click the button below.
If you send us your mailing address, Alphabasic may occasionally send you various goodies (overstocks, stickers, even rare CDs) in appreciation and encouragement for your support.

Thanks for reading.
Who knows if my little business plan here will work to fund new releases, but even failure is better than the crappy label/distributor/retailer system musicians have suffered from for over 50 years.
We hope you enjoy the music as much as we do releasing it.
Finally, if you plan on sharing this release, please include this file. The only reason it is here is to show the listener where he can support his favorite artists!

Benn Jordan
CEO – Alphabasic Records”

I’ve never heard of The Flashbulb before, so I went and download the album. it’s good stuff, kinda resembles one of my favorite bands. And I _am_ going to donate some bucks to them.

Hope this can be a modest first step to a new business model for music.

Categories: Music | Top Of Page | 6 Comments » |

MS Pex

February 19th, 2008

The intention might be good, but IMHO a computer will never replace a human for some things.

http://research.microsoft.com/pex/

Categories: General | Top Of Page | 9 Comments » |

A realistic impression about MonoRail

February 15th, 2008

I keep following what people say about Castle through technorati. Most hail, some bashings – for the wrong reasons – and from time to time I read something that is really cool. Last one was 0 to 60 in … 3 months.

MonoRail is hard, don’t let anyone tell you any different. You have to download a bunch of stuff, set it up on your machine, learn some weird new syntax for writing your web pages, you don’t get intellisense in your webpages, there’s no nice server controls, no drag and dropping to build the UI, the documentation doesn’t always show you how to get things done and much much more.

Very very true.

Update: I’m not being ironic. I agree with the paragraph I quoted, and I suggest go reading the whole post.