Question

In a container, I have an FooFactory registered and another component which needs an Foo.

class FooFactory {
  FooFactory(Bar bar) { ... }
  Foo CreateFoo() { ... }
}

class FooConsumer {
  FooConsumer(Foo foo) { ... }
}

builder.RegisterType<FooProvider>();
builder.RegisterType<FooConsumer>();

Is there a better way for Autofac to wire up the call to CreateFoo than calling c.Resolve during registrations, or does this look pretty much the right way?

builder.Register(c => c.Resolve<FooFactory>().CreateFoo());
Was it helpful?

Solution

There's nothing wrong with that pattern; it's actually quite common.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top