AngryHacker
AngryHacker

Reputation: 61646

What are .NET Core patterns for using middleware objects in downstream projects?

Imagine the following project structure for a .NET Core Web project.

Widget.UI project (MVC) calls Widget.Business calls Widget.Services calls Widget.Repository project.

Let's say Widget.Services project wants to implement Distributed caching with Memcached.

Is the pattern to add Memcached to middleware in Startup.ConfigureServices and .Configure, pass them into controller's constructor, then build constructors with Memcached all the way down the stack?

Or would it be better to better to var cache = new MemcachedClient() in the Widget.Services project?

Upvotes: 2

Views: 280

Answers (1)

Fanda
Fanda

Reputation: 3786

Memcached will be your infrastructure. So according to your design, service layer should define cashing abstraction and the implementation of this abstraction (using Memcached) could be (for example) in the Widget.Infrastructure project.

There may be good reason to cache in the repository layer too. But it depends on your needs. Then you should define cashing abstraction outside of the service layer.

Service configuration will be same as any other services (in the Startup.ConfigureServices) and you inject cashing infrastructure by constructor, where you need it.

Upvotes: 1

Related Questions