.AsClosedTypeOf(Generic) followed by .As<IAnotherService>() doesn't work?

283 views
Skip to first unread message

Chakrit

unread,
Apr 10, 2010, 9:19:54 AM4/10/10
to Autofac
I'm not sure wether this a bug or simply that I don't know how to use
the new regsitration methods in autofac properly.

Here's my situation:
* I have a large set of `abstract class Entity` implementations.
* For each implementation of `Entity` I need a
`EntityService<TEntity>` wired in automatically.
* `EntityService<TEntity>` also implements `IService` which is the
common interface for a large chunk of services in my apps
* There are already several other unrelated `IService`
implementations being registered
* Now I need all the `EntityService<TEntity>` to be included when I
resolve an `IEnumerable<IService>`

Is that possible to do in the latest downloadable bits?

Here's what I have so far... which doesn't work

b.RegisterAssemblyTypes(typeof(Entity).Assembly)
.AssignableTo<Entity>()
.AsClosedTypesOf(typeof(EntityService<>))
.As<IService>()
.SingleInstance();

I know that I can do a manual registration by constructing types
manually via Reflection and Type.MakeGenericType but that seems
elaborate. I'm thinking that there must be a proper Autofac-way of
doing this.

..

Thanks for any help!

Alex Meyer-Gleaves

unread,
Apr 11, 2010, 8:51:52 AM4/11/10
to Autofac
Hi Chakrit,

Do you have types in your assembly that close the EntityService<> open
generic type?

e.g.

public class FooService : EntityService<Foo>
{
}

The AsClosedTypesOf method does not create a closing type for you. You
will need to ensure that closing types exist for the registration to
work.

You should also remove the AssignableTo method call because the
closing types you are looking for will not be assignable to Entity.

Cheers,

Alex.

Chakrit

unread,
Apr 11, 2010, 10:52:04 PM4/11/10
to Autofac
Oh, thanks! That clarified a lot of things for me :)

I'm thinking that Autofac would let me generate types on the fly. I'm
being a little too optimistic there it seems. Autofac will only create
closed generic types as they're requested so even if I activates an
IEnumerable<IService> it won't suddenly try to close all generic types
that's assignable to IService, right?


On Apr 11, 7:51 pm, Alex Meyer-Gleaves <alex.meyerglea...@gmail.com>
wrote:

Alex Meyer-Gleaves

unread,
Apr 14, 2010, 9:49:00 AM4/14/10
to aut...@googlegroups.com
That's right Chakrit. Autofac will not generate the types for you on the fly.

The AsClosedTypesOf method simply finds existing types in the assembly that close the open generic type.


--
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=en.


Reply all
Reply to author
Forward
0 new messages