リポジトリ インターフェイスとクラスがあります。また、リポジトリ インターフェイスに依存するサービス インターフェイスとクラスもあります。典型的なDI。私の目的は、サービスとリポジトリの間にキャッシングを追加し、サービスやリポジトリには触れないようにすることです。コードは次のとおりです。
public class CachedCustomerRepository : ICustomerRepository
{
private readonly ICustomerRepository _repository;
private readonly ConcurrentDictionary<int, Customer> _cache;
public CachedCustomerRepository(ICustomerRepository repository)
{
if (repository == null)
throw new ArgumentNullException("repository");
this._repository = repository;
this._cache = new ConcurrentDictionary<int, Customer>();
}
}
私はウィンザー城でこれを行いました。クラスを追加しただけで、登録を変更することなく、すぐに機能しました。それは私にとってすごい効果でした!:)今、私はAutofacで同じことをしようとしましたが、失敗しました。循環依存関係について不平を言いますが、登録方法がわかりません。
編集 - 登録は次のとおりです。
builder.RegisterAssemblyTypes(typeof(ICustomerRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.SingleInstance();
編集 - これが今の登録です:
builder.RegisterAssemblyTypes(typeof(ICustomerRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository") && !t.Name.StartsWith("Cached"))
.AsImplementedInterfaces()
.SingleInstance();
この後、キャッシュされた各リポジトリの登録が行われます。