Combining assembly scanning with open generic types

493 views
Skip to first unread message

Paul Batum

unread,
Jan 29, 2009, 6:56:32 PM1/29/09
to structure...@googlegroups.com
I have a generic interface : IHbmWriter<T>

I have a bunch of implementors:
HbmClassWriter : IHbmWriter<ClassMapping>
HbmBagWriter : IHbmWriter<BagMapping>
etc

At the moment, all of these writers are unique to the assembly. You won't find two that both implement IHbmWriter<ClassMapping>, for example.

I want to use structuremap to build these writers but I don't want to have to manually register them all. Presumably this means combining assembly scanning with open generic types. I tried the following:

            ObjectFactory.Initialize(x =>
            {
                x.Scan(s =>
                {
                    s.TheCallingAssembly();
                    s.AddAllTypesOf(typeof (IHbmWriter<>));
                });
            });

But that does not seem to work. It still tells me there is no default instance. The resulting WhatDoIHave() for this is attached.

Thanks,

Paul Batum

what_do_i_have.txt

Jeremy D. Miller

unread,
Jan 29, 2009, 7:04:30 PM1/29/09
to structure...@googlegroups.com
Here's an example that I think is in the SM docs somewhere.  You need 2.5.2+ for this.


    /// <summary>
    /// This TypeScanner looks for any concrete class that implements
    /// an IFlattenerFor<T> interface, and registers that type
    /// against the closed IFlattenerFor<T> interface,
    /// i.e. IFlattenerFor<Address> or IFlattenerFor<Site>
    /// </summary>
    public class DtoFlattenerConventionScanner : ITypeScanner
    {
        public void Process(Type type, PluginGraph graph)
        {
            Type interfaceType = type.FindInterfaceThatCloses(typeof (IFlattenerFor<>));
            if (interfaceType != null)
            {
                graph.AddType(interfaceType, type);
            }
        }
    }

Then,

Scan(x => {
    x.TheCallingAssembly();
    x.With<DtoFlattenerConventionScanner>();

});
 
Jeremy D. Miller
The Shade Tree Developer
jeremy...@yahoo.com



From: Paul Batum <paul....@gmail.com>
To: structure...@googlegroups.com
Sent: Thursday, January 29, 2009 5:56:32 PM
Subject: [sm-users] Combining assembly scanning with open generic types

Paul Batum

unread,
Jan 29, 2009, 7:38:36 PM1/29/09
to structuremap-users
Thanks Jeremy, that worked.

I am thinking, couldn't the AddAllTypesOf() method detect that an open
typed had been passed an automagically do the rest?
Would you be interested in accepting a patch to enable this?

Paul Batum

On Jan 30, 11:04 am, "Jeremy D. Miller" <jeremydmil...@yahoo.com>
wrote:
> Here's an example that I think is in the SM docs somewhere.  You need 2.5.2+ for this.
>
>     /// <summary>
>     /// This TypeScanner looks for any concrete class that implements
>     /// an IFlattenerFor<T> interface, and registers that type
>     /// against the closed IFlattenerFor<T> interface,
>     /// i.e. IFlattenerFor<Address> or IFlattenerFor<Site>
>     /// </summary>
>     public class DtoFlattenerConventionScanner : ITypeScanner
>     {
>         public void Process(Type type, PluginGraph graph)
>         {
>             Type interfaceType = type.FindInterfaceThatCloses(typeof (IFlattenerFor<>));
>             if (interfaceType != null)
>             {
>                 graph.AddType(interfaceType, type);
>             }
>         }
>     }
>
> Then,
>
> Scan(x => {
>     x.TheCallingAssembly();
>     x.With<DtoFlattenerConventionScanner>();
>
> });
>
>  Jeremy D. Miller
> The Shade Tree Developer
> jeremydmil...@yahoo.com
>
> ________________________________
> From: Paul Batum <paul.ba...@gmail.com>

Jeremy Miller

unread,
Feb 1, 2009, 1:35:37 PM2/1/09
to structuremap-users
Sure, but I'd prefer you spend your free time on the FNH Semantic
Model overhaul;-)

Paul Batum

unread,
Feb 2, 2009, 12:17:48 AM2/2/09
to structuremap-users
Good point :)
Reply all
Reply to author
Forward
0 new messages