开发者

Inject generic parameters with structuremap 2.6

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?


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜