Bloom is a light weight extensibility, discovery and composition framework for Silverlight 2 and the .net Framework. Bloom simplifies the development of component orientated applications and provides simple but flexible Inversion of Control and Dependency Injection.

Bloom has many of the features common to the popular IoC containers and has a similar goal to MEF in that it simplifies the development of extensible component orientated applications.

This will probably be the last release of Bloom – after 3 years of use and several rewrites, I’m happy that it is probably the most near feature complete software I’ve ever written. It is possible that Bloom could be made redundant if MEF makes it into Silverlight for .net 4, but I’m not saddened by this – Bloom has been a great hobby project from which I’ve been able to better architect commercial and hobby projects and also learn a great deal more about .net. I still plan to use Bloom in new projects. It isn’t as powerful as MEF but IMO easier and quicker to use.

You can visit the Bloom wiki on codeplex for more info, and get a taster of some of the things it can do from the following code snippets.

Registering Components

// create a catalog
ICatalog catalog = new Catalog();
// register some components
catalog.Register<Customer>();
catalog.Register(typeof(Account));
// register as a singleton
catalog.Register<User>().SingleInstance();
// register a build function
catalog.Register<AddressBook>.Build(c => new AddressBook(foo));

You can decorate your component classes with the [ComponentAttribute] instead and the Catalog can find and register them. Services do not need to be registered.

Mapping Services to Components

catalog.Map(typeof(ICustomer), typeof(Customer));
catalog.Map<IAddress, Address>();
// perform runtime conditional mapping
catalog.Map<IAccount>(c => condition ? c.Resolve<StandardAccount>() : c.Resolve<GoldAccount>());

Setting the mapping is optional – by default a service will be mapped to the first registered compatible component.

Resolving Components

// resolve a service and perform dependency injection
object instance = catalog.Resolve(typeof(IAccount))
IAccount instance = catalog.Resolve<IAccount>();

Declarations

[Component] // optional
public class User : IUser
{
  // constructor dependency injection
  public User(ILogger logger) {}

  // property dependency injection
  [Dependency]
  public IAddressBook AddressBook { get; set; }

  // factory dependency injection for lazy instantiation
  [Dependency]
  public IFactory<ITransaction> TransactionFactory {get; set; }

  public void Foo()
  {
    // dependency lazy instantiation
    ITransaction t = TransactionFactory.Build();
    t.Do();
  }

  // factory enumeration dependency injection
  [Dependency]
  public IEnumerable<IFactory<IAccount>> AccountFactories { get; set; }

  public void Bar()
  {
    // query and select a factory from the catalog
    foreach (var factory in AccountFactories)
    {
      // query IAccount component type
      if (factory.Type...)
        factory.Build().Do();
    }
  }
}

Bloom v2.1 is open source and can be downloaded from here.



No Responses Yet to “Bloom v2.1 released”  

  1. No Comments Yet

Leave a Reply