Autogenerated context interface for strong-typed "service locator"

13 views
Skip to first unread message

Peter

unread,
Nov 17, 2009, 8:50:55 PM11/17/09
to Autofac
Hi,

so I have been exploring an idea for packing dependencies into an
interface specialized for serving up dependencies.

The scenario I've been targeting with this is when I want to isolate
subclasses from changes in the superclass dependencies. So when
subclassing, in addition to any new dependencies the subclass
requires, we have to replicate dependencies from the superclass for
these to be resolved. An example first:

class SuperClass {
protected SuperClass(IServiceX x, IServiceY y) {}
}

class SubClass : SuperClass {
public SubClass(IServiceZ z, IServiceX x, IServiceY y) : base(x,y)
{}
}

With several subclasses and even classes implemented by third parties
using our libraries this becomes vulnerable to change. When we need to
add or change any of the existing dependencies in the superclass, this
will require changing all subclasses. So to alleviate this I introduce
a "dependency" interface:

class SuperClass {
public interface IDependencies {
IServiceX x {get;}
IServiceY y {get;}
}
protected SuperClass(IDependencies dependencies) {}
}

class SubClass : SuperClass {
public SubClass(IServiceZ z, IDependencies dependencies) : base
(dependencies) {}
}

Now with the dependency interface we can make changes to this
interface with no change to the subclasses (other than perhaps a
recompile). This interface now has a feel of being a service locator,
though it is strongly typed.

So far I would like to know if you Autofac folks have any take on this
from a DI perspective. My next take on this is to make using
dependency interfaces really easy by providing a means of
autogenerating implementations for such interfaces so instances can be
provided by an Autofac container.


Best regards
Peter

Nicholas Blumhardt

unread,
Nov 18, 2009, 7:38:36 PM11/18/09
to aut...@googlegroups.com
Auto-generating these is a really interesting idea.

I've taken the same approach as you in the past - something like "IDependencies" to pass from subclasses to their base classes - but I'm always a bit wary of it outside the common case of controllers in MVC.

When the framework is under my control :) I tend to want to get rid of the inheritance and turn the subclasses into Strategies - by tweaking the strategy interface you can usually eliminate the need for any base-class implementation.

Looking forward to seeing where your investigation takes you - might be a good candidate for IRegistrationSource :)

Nick

2009/11/18 Peter <plill...@gmail.com>

--

You received this message because you are subscribed to the Google Groups "Autofac" group.
To post to this group, send email to aut...@googlegroups.com.
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/autofac?hl=.



Peter

unread,
Nov 30, 2009, 7:36:54 AM11/30/09
to Autofac
I have an implementation that uses DynamicProxy2 (http://
www.castleproject.org/dynamicproxy) to generate the IDependency
implementations.
Should be easy to implement a registration source for it too.

If this still sounds as a good idea (I think so :) I would be happy to
contribute!


- Peter

On Nov 19, 1:38 am, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:
> Auto-generating these is a really interesting idea.
>
> I've taken the same approach as you in the past - something like
> "IDependencies" to pass from subclasses to their base classes - but I'm
> always a bit wary of it outside the common case of controllers in MVC.
>
> When the framework is under my control :) I tend to want to get rid of the
> inheritance and turn the subclasses into Strategies - by tweaking the
> strategy interface you can usually eliminate the need for any base-class
> implementation.
>
> Looking forward to seeing where your investigation takes you - might be a
> good candidate for IRegistrationSource :)
>
> Nick
>
> 2009/11/18 Peter <plillev...@gmail.com>
> > autofac+u...@googlegroups.com<autofac%2Bunsu...@googlegroups.com >
> > .

Nicholas Blumhardt

unread,
Dec 9, 2009, 10:09:43 PM12/9/09
to aut...@googlegroups.com
Hi Peter,

Still think it is a funky idea :)

Sorry about the delay getting back to this - if you would like to package it up for AutofacContrib it would be welcome!

Cheers,
Nick

2009/11/30 Peter <plill...@gmail.com>
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/autofac?hl=en.



Peter

unread,
Dec 22, 2009, 7:53:11 AM12/22/09
to Autofac
Hey Nick,

yes, me sorry too for delay on my side :) I'm still on it though,
hoping to complete a package during the Holidays.

That said, I'm a bit unsure on what to name this contribution. I was
thinking of using "GeneratedFactoryServices", since the IDependency
interface could be viewed as a service containing a set of generated
factories.
It doesn't feel quite right though, would like something more
descriptive...

Another approach is to think of the interface as "a strongly typed
view on IContext"... GeneratedTypedContexts, perhaps?

As you can see, I could use some input on what to name the baby :) Any
input is appreciated!


- Peter

On Dec 10, 4:09 am, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:


> Hi Peter,
>
> Still think it is a funky idea :)
>
> Sorry about the delay getting back to this - if you would like to package it
> up for AutofacContrib it would be welcome!
>
> Cheers,
> Nick
>

> 2009/11/30 Peter <plillev...@gmail.com>

> > <autofac%2Bunsu...@googlegroups.com<autofac%252Bunsubscribe@googlegroup s.com>>


> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/autofac?hl=.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "Autofac" group.
> > To post to this group, send email to aut...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > autofac+u...@googlegroups.com<autofac%2Bunsu...@googlegroups.com >
> > .
> > For more options, visit this group at

> >http://groups.google.com/group/autofac?hl=en.

Peter

unread,
Dec 22, 2009, 6:11:21 AM12/22/09
to Autofac
Hey Nick,

yes, me sorry too for late replies. I'm on the case though, it is not
forgotten :) I will prepare a package during the Holidays.

That said, I'm not quite certain what to call this contribution.

I've thought of calling it "GeneratedFactoryServices" since what the
"IDependency" interface really is could be viewed as a service
encapsulating one or more factories, all generated dynamically.

It could also be thought of as "a strongly typed view on the
IContext".... GeneratedTypedContexts perhaps?

Any thoughts on naming the baby would be great! :)


- Peter

On Dec 10, 4:09 am, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:


> Hi Peter,
>
> Still think it is a funky idea :)
>
> Sorry about the delay getting back to this - if you would like to package it
> up for AutofacContrib it would be welcome!
>
> Cheers,
> Nick
>

> 2009/11/30 Peter <plillev...@gmail.com>

> > <autofac%2Bunsu...@googlegroups.com<autofac%252Bunsubscribe@googlegroup s.com>>


> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/autofac?hl=.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "Autofac" group.
> > To post to this group, send email to aut...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > autofac+u...@googlegroups.com<autofac%2Bunsu...@googlegroups.com >
> > .
> > For more options, visit this group at

> >http://groups.google.com/group/autofac?hl=en.

Nicholas Blumhardt

unread,
Dec 22, 2009, 6:35:45 PM12/22/09
to aut...@googlegroups.com
GeneratedTypedContext sounds like a good working name to me.

Cheers,
Nick

2009/12/22 Peter <plill...@gmail.com>
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.

Peter

unread,
Feb 9, 2010, 3:24:17 PM2/9/10
to Autofac
Hey,
a bit later than planned but I've been able to put some more effort
into the GeneratedTypedContext thingy now.
I have two approaches though, on how to register a GTC with a
container. Would love some input on which way is preferred:

1. Using a RegistrationSource

var builder = new ContainerBuilder();
builder.RegisterSource(new
GeneratedTypedContextRegistrationSource());

builder.RegisterType<GeneratedTypedContext<IServiceLocator>>();
var container = builder.Build();

var sl = container.Resolve<IServiceLocator>();

Here we uses a GeneratedTypedContext type to indicate that the
interface should be generated. When resolved for the actual interface,
the registration source
will be consulted and looks for a
GeneratedTypedContext<IServiceLocator> registration. If found, a proxy
for the interface is generated and returned for the registration.
A helper extension method RegisterGeneratedTypedContext will be
implemented to make registration easier.

2. Using "immediate" registration

var builder = new ContainerBuilder();
builder.RegisterGeneratedTypedContext<IServiceLocator>();
var container = builder.Build();

var sl = container.Resolve<IServiceLocator>();

This approach does not rely on a registration source. The extension
method generates the proxy and registers it with the container.


So, both approaches works fine, and is almost equal in use. I find 2)
a bit easier for the end-user though, since no registration source (or
module) is involved.
Perhaps there are other advantages to registration sources?

- Peter

On 23 Des 2009, 00:35, Nicholas Blumhardt


<nicholas.blumha...@gmail.com> wrote:
> GeneratedTypedContext sounds like a good working name to me.
>
> Cheers,
> Nick
>

> 2009/12/22 Peter <plillev...@gmail.com>

> > > > <autofac%2Bunsu...@googlegroups.com<autofac%252Bunsubscribe@googlegroup s.com>


> > <autofac%252Bunsubscribe@googlegroup s.com>>
> > > > > > .
> > > > > > For more options, visit this group at
> > > > > >http://groups.google.com/group/autofac?hl=.
>
> > > > --
>
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "Autofac" group.
> > > > To post to this group, send email to aut...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > autofac+u...@googlegroups.com<autofac%2Bunsu...@googlegroups.com >
> > <autofac%2Bunsu...@googlegroups.com<autofac%252Bunsubscribe@googlegroup s.com>>
> > > > .
> > > > For more options, visit this group at

Nicholas Blumhardt

unread,
Mar 8, 2010, 2:21:51 AM3/8/10
to aut...@googlegroups.com
I think the up-front registration is best, because there really isn't much to distinguish these interfaces from other services.

Perhaps stepping back these call for a name like "aggregate service"? As in, RegisterAggregateService<IAppContext>()?

We started out with something similar on our current project (service aggregation via properties, for controllers) but quickly wanted to add methods and encapsulate some of the behaviour specific to the aggregate. I think I've ended up coming in on the side of Law-of-Demeter here :)

To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages