views:

114

answers:

1

I need to inject generic repositories (IRepository) into my classes' constructors but I dont know how to do it with the new DSL in structuremap 2.6, does anyone know how?

+3  A: 

This can be done with just one line of code in your configuration. Assuming you have this:

Entities: - Customer - Order

And have a generic repository model like this:

  • Repository : IRepository

And have a app services that look like:

public AppService(IRepository<Customer> custRepo, IRepository<Order> orderRepo)

You would have something like this. Notice the bit about using the scanner to hook up your custom repositories.

public class SmRegistry : Registry
    {
        public SmRegistry()
        {
            For(typeof (IRepository<>))
                .Use(typeof (Repository<>));

            //using this will find any custom repos, like CustomerRepository : Repository<Customer>
            //Scan(scanner =>
            //         {
            //             scanner.TheCallingAssembly();
            //             scanner.ConnectImplementationsToTypesClosing(typeof (IRepository<>));

            //         });
        }
    }

Assuming your Repositories are defined in some other assembly from your application, you can use Registries to hook it all together. Check out this post:

http://blog.coreycoogan.com/2010/05/24/using-structuremap-to-configure-applications-and-components/

Corey Coogan