I refined this facility a bit and made a blog post:
http://nathan.whiteboard-it.com/archive/2009/02/26/register-repository-implementations-automatically-with-a-castle-facility.aspx

It now allows you to specify assemblies to search and the repository base
type in the ctor.

I'd love some feedback.  If you see other ways it could be improved, let me
know.

On Thu, Feb 26, 2009 at 11:23 AM, Bergius <[email protected]> wrote:

>
> That's good stuff. I have other kinds of services that will benefit
> from automatic registration, so a facility is indeed a better idea.
> Thank you!
>
> As for Binsor, I need to take some time and learn boo syntax. Guessing
> and copying from examples doesn't work in the long run :).
>
> Thanks again.
>
> On Feb 26, 5:49 pm, Nathan Stott <[email protected]> wrote:
> > Well I couldn't find my binsor to do it, but I use this facility in one
> of
> > my projects which will accomplish what you want.  You could convert it to
> > Boo and put it in your binsor if you want or you can just modify this
> > facility and call it from Binsor.  Hope this helps.
> >     public class NHRepositoriesFacility : AbstractFacility
> >     {
> >         protected override void Init()
> >         {
> >          Predicate<Type> isGenericRepositoryInterface =
> >          type =>
> >          type.GetInterfaces().Contains(
> >          x => x.IsGenericType &&
> > x.GetGenericTypeDefinition().IsAssignableFrom(typeof (IRepository<>)));
> >
> >          var types =
> >          Assembly.GetAssembly(typeof (IRepository<>)).GetTypes().Concat(
> >          typeof (ClassifiedAdRepository).Assembly.GetTypes());
> >
> >          var repositoryInterfaces = types.Where(
> >          type => type.IsInterface && !type.IsGenericTypeDefinition &&
> > isGenericRepositoryInterface(type));
> >
> > repositoryInterfaces.ForEach(interfaceType =>
> >                              {
> > var implementor = types.FirstOrDefault(type =>
> > interfaceType.IsAssignableFrom(type) && !type.IsAbstract);
> > if (implementor == null) return;
> >
> > var genericRepositoryInterfaceTypes = implementor.GetInterfaces().Where(
> > x =>
> > x.IsGenericType &&
> > x.GetGenericTypeDefinition().IsAssignableFrom(typeof(IRepository<>)));
> >
> >
> Kernel.Register(Component.For(interfaceType).ImplementedBy(implementor).Forward(genericRepositoryInterfaceTypes));
> >                              });
> >         }
> >     }
> >
> > If I had more time I'd make the conversion to boo for you, but it may be
> > better as a facility anyway :).  The conversion to boo should be
> relatively
> > easy if you spend some time on it if you want to go that route.
> >
> > On Thu, Feb 26, 2009 at 9:56 AM, Nathan Stott <[email protected]> wrote:
> > > The tests are indeed the best source of binsor documentation.
> > > I'll look for that code.  Should be able to find it soon.  It's just a
> > > matter of using reflection appropriately.
> >
> > > On Thu, Feb 26, 2009 at 9:54 AM, Bergius <[email protected]> wrote:
> >
> > >> Thank you for your reply.
> >
> > >> That is exactly what I want to achieve, and I'd really appreciate if
> > >> you would dig up that code.
> >
> > >> Are the tests the best source for Binsor documentation, or is there a
> > >> hidden documentation cache somewhere? :)
> >
> > >> On Feb 26, 4:40 pm, Nathan Stott <[email protected]> wrote:
> > >> > You have to follow conventions and register based upon them if you
> want
> > >> to
> > >> > pull this off.
> > >> > If I am understanding you correctly, then you'll want some algorithm
> > >> like
> > >> > this:
> >
> > >> > 1) retrieve all interface types that inherit from IRepository<T>
> from an
> > >> > assembly
> > >> > 2) find concrete types that implement your specific interface type
> > >> > (ISomethingRepository)
> > >> > 3) register the concrete type as an implementation of ISomething
> > >> Repository
> > >> > and also as an implemntation of IRepository<Something>
> >
> > >> > You can do this in Binsor.
> >
> > >> > If this is the algorithm you want to follow, I can dig up some code
> from
> > >> > where I've done something similar before.
> >
> > >> > Is that what you're trying to accomplish?
> >
> > >> > On Thu, Feb 26, 2009 at 6:04 AM, Bergius <[email protected]> wrote:
> >
> > >> > > Hi. I'm getting tired of wiring up stuff by hand, but I can't
> quite
> > >> > > figure out the Binsor recipe for autoregistration success with
> this
> > >> > > structure:
> >
> > >> > > ARRepository<T> : IRepository<T>
> > >> > > ISomethingRepository : IRepository<Something>
> > >> > > ARSomethingRepository : ARRepository<Something>,
> ISomethingRepository
> >
> > >> > > I want to achieve this for all my repos, but automatically:
> > >> > > component ARSomethingRepository < IRepository of Something,
> > >> > > ISomethingRepository
> >
> > >> > > I've googled around, but all I've managed to find are four-liners
> to
> > >> > > wire up implementations with a single interface.
> >
> > >> > > Any pointers?
> >
> > >> > > / Bergius
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Rhino Tools Dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rhino-tools-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to