skip to the main content area of this page
Patterns and Practices

Using Castle Windsor With Repository Factory for IoC


The Repository Factory from Microsoft Patterns & Practices comes with a cool RepositoryFactory Class that maps Repository Interfaces to Repository Classes for a bit of loose coupling in your ASP.NET and Winform Applications. For more robust IoC Services, one could use Castle Windsor to serve the Repository Classes as well as other services in your .NET Applications. For a complete understanding of the new RepositoryFactory Class in the Repository Factory see the following post:

Repository Factory - New and Improved Data Access Guidance Package

 

As mentioned in the above post, interfaces are now created for each Repository generated by the Repository Factory:

 

public class CustomerRepository :
Repository
<Customer>, ICustomerRepository

 

with the interfaces being clean and simple:

 

public interface ICustomerRepository
{
    Customer GetCustomersByCustomerId(System.Int32 customerId);
    List<Customer> GetAllFromCustomers();
    void Add(Customer customer);
    void Remove(System.Int32 customerId);
    void Save(Customer customer);
}

 

The RepositoryFactory Class is generated which allows you to grab your Repositories as follows:

 

class Program
{
    static void Main(string[] args)
    {
        ICustomerRepository repository =
            RepositoryFactory.Create<ICustomerRepository>();

        Customer customer = repository.GetCustomersByCustomerId(1);
    }
}

 

Use of the RepositoryFactory is completely optional but perfect for applications that just need the IoC Services it provides. For applications that need additional IoC services that are out of the scope of the RepositoryFactory Class, one could use Castle Windsor or another 3rd party dependency injection tool.

One could use an external XML configuration to register components or simply add them programmatically to the WindsorContainer as such:

 

WindsorContainer ioc = new WindsorContainer();
ioc.AddComponentWithLifestyle
    <ICustomerRepository, CustomerRepository>(LifestyleType.Singleton);
ioc.AddComponentWithLifestyle
    <IOrderRepository, OrderRepository>(LifestyleType.Singleton);

 

In this case I am registering the services as Singletons so that only 1 instance is created for each repository.

Using the services with Castle Windsor is similar to using the RepositoryFactory Class:

 

ICustomerRepository customerRepository = 
                ioc.Resolve<ICustomerRepository>();
customerRepository.DeleteCustomer(1);

 

The benefit of Castle Windsor is that it is so much more feature rich and extensible than the RepositoryFactory Class. The benefit of the RepositoryFactory Class is that it comes with the Repository Factory and is perfect for simple interface mapping needs as shown here. The key is choosing the right tool for the right job.

As an FYI, you can find an example of using Castle ActiveRecord with the WCSF here:

WCSF and Castle ActiveRecord

Enjoy the diversity of tools and solutions!


Tags: CastleWindsor, IoC


Topics



Popular Tags



Recent Links