1

We are using ServiceStack in a web hosted API service, and have done so for awhile now. The execution path for any request follows the pattern:

Request comes in:

--> Service (handles request, utilizes IManager injected via constructor)

--> IManager (performs business logic, utilizes IRepository/ies that are injected via constructor)

--> IRepository/ies (SQL Server, NoSQL, utilizes connection factory/ies injected by constructor)

Now that we are entertaining another client, some of these requests need to follow slightly different business logic, and potentially utilize a different repo strategy. However, the API will remain consistent. To this end, I am extracting client specific logic (the concrete IManager and IRepository implementations above) to separate assemblies. I've written a component that inspects the current request context, identifying the client this request is for, which then uses reflection and the Activator to instantiate an instance of the specific implementation I want to execute for any given request.

However, because of this, I can't just register implementations of IManager and IRepository in the container at startup - this needs to be resolved dynamically per request. I'd like to do some type of LazyResolve, but I can't find any solid example of how this is done to get me started here.

Am I thinking crazy here? My API is essentially just that with this - the custom logic that occurs is isolated to client specific assemblies that are called at runtime. This all makes perfect sense to me in theory, but in practice it's proving a challenge. Thoughts? Ideas?

StratMN
  • 161
  • 8

1 Answers1

1

If you only want to resolve adhoc dependencies at runtimes you can just resolve them from the IOC as needed in your Service with:

base.TryResolve<T>();

In any Filter from IRequest with:

req.TryResolve<T>();

Or externally outside ServiceStack with:

HostContext.TryResolve<T>();
mythz
  • 141,670
  • 29
  • 246
  • 390