StratMN
StratMN

Reputation: 171

ServiceStack's Funq type registration via reflection?

I've used Castle Windsor quite a bit. It has a really handy facility for registering types via reflection. So for example, I would do things like this at application startup:

container.Register(Classes.FromThisAssembly().BasedOn<IMyInterface>().LifestyleTransient());

So, say I had a bunch of providers for data formatting - I could register them all (via interface) with that one line. Even better, when I created new ones (assuming they were in that same assembly, and same interface) they would then get registered as well; I wouldn't have to remember to do this when coding them.

Is there an equivalent in ServiceStack's implementation of the Funq container? I've looked around, and don't seem to see one.

Upvotes: 2

Views: 263

Answers (1)

mythz
mythz

Reputation: 143339

An interface can only have a single implementation, but if you wanted to register all concrete types implementing an interface by scanning to find all types and pre-registering them with:

var fooTypes = assembly.GetTypes().Where(x => x.HasInterface(typeof(IFoo)));
container.RegisterAutoWiredTypes(fooTypes);

Which if needed can all be retrieved with:

 var fooInstances = fooTypes.Select(c.Resolve).Cast<IFoo>();

Which can also be registered as a dependency itself:

container.Register(c => fooTypes.Select(c.Resolve).Cast<IFoo>()); 

That your classes can access using property injection:

public IEnumerable<IFoo> FooInstances { get; set; }

Upvotes: 1

Related Questions