英文:
Autofac WhenInjectInto like Grace/Ninject
问题
我有这个使用Grace(在Ninject中也是一样的)的实现,但我需要在Autofac中使用它,但Autofac没有一个"当注入到"的功能。我该如何迁移这段代码?谢谢
public interface ILocationRepository
{
Task<Location?> GetByIDAsync(int id);
}
public class LocationRepositoryProxyCache : BaseProxyCache, ILocationRepository
{
private readonly ICacheProvider<Location> _cacheProvider;
private readonly ILocationRepository _locationRepository;
public LocationRepositoryProxyCache(ICacheProvider<Location> cacheProvider, ILocationRepository locationRepository)
{
_cacheProvider = cacheProvider;
_locationRepository = locationRepository;
}
public Task<Location?> GetByIDAsync(int id)
{
return _cacheProvider.GetOrSetAsync("CACHE_KEY", () => _locationRepository.GetByIDAsync(id));
}
}
public class LocationRepository : BaseRepository<Location>, ILocationRepository
{
public LocationRepository(IMyInterface myAppDb) : base(myAppDb) { }
public Task<Location?> GetByIDAsync(int id)
{
return ...;
}
}
Autofac代码迁移
```csharp
builder.RegisterType<LocationRepository>().As<ILocationRepository>();
builder.RegisterType<LocationRepositoryProxyCache>().As<ILocationRepository>().InstancePerDependency();
这是你的Grace代码迁移到Autofac的方式。
英文:
i have this implementation with Grace (it's the same in Ninject) but i need use then in Autofac but autofac does not have a "WhenInjectInto" functionality. How can i migrate this code? Thx
public interface ILocationRepository
{
Task<Location?> GetByIDAsync(int id);
}
public class LocationRepositoryProxyCache : BaseProxyCache, ILocationRepository
{
private readonly ICacheProvider<Location> _cacheProvider;
private readonly ILocationRepository _locationRepository;
public LocationRepositoryProxyCache(ICacheProvider<Location> cacheProvider, ILocationRepository locationRepository)
{
_cacheProvider = cacheProvider;
_locationRepository = locationRepository;
}
public Task<Location?> GetByIDAsync(int id)
{
return _cacheProvider.GetOrSetAsync("CACHE_KEY", () => _locationRepository.GetByIDAsync(id));
}
}
public class LocationRepository : BaseRepository<Location>, ILocationRepository
{
public LocationRepository(IMyInterface myAppDb) : base(myAppDb) { }
public Task<Location?> GetByIDAsync(int id)
{
return ...;
}
}
Grace code to migrate to Autofac
registrationBlock.Export<LocationRepository>().As<ILocationRepository>().When.InjectedInto<LocationRepositoryProxyCache>();
registrationBlock.Export<LocationRepositoryProxyCache>().As<ILocationRepository>();
答案1
得分: 1
根据这里,我创建了用于按键注册服务的示例代码。
builder.RegisterType<LocationRepository>().Keyed<ILocationRepository>(SourceType.Fetching);
builder.RegisterType<LocationRepositoryProxyCache>().Keyed<ILocationRepository>(SourceType.Caching);
builder.RegisterType<Consumer>().As<IConsumer>().WithAttributeFiltering();
这是LocationRepository和LocationRepositoryProxyCache的代码:
public class Consumer : IConsumer
{
private ILocationRepository cacheRepo;
private ILocationRepository fetchRepo;
public Consumer([KeyFilter(SourceType.Fetching)] ILocationRepository fetchService,
[KeyFilter(SourceType.Caching)] ILocationRepository cacheService)
{
cacheRepo = cacheService;
fetchRepo = fetchService;
}
public void Test()
{
cacheRepo.GetByIDAsync(1);
fetchRepo.GetByIDAsync(2);
}
}
public interface IConsumer
{
void Test();
}
希望这可以帮助您。
英文:
Based on [here](https://autofac.readthedocs.io/en/latest/advanced/keyed-services.html#resolving-with-attributes "Named and Keyed Services")
, I made the sample code to register the Service by Keyed
builder.RegisterType<LocationRepository>().Keyed<ILocationRepository>(SourceType.Fetching);
builder.RegisterType<LocationRepositoryProxyCache>().Keyed<ILocationRepository>(SourceType.Caching);
builder.RegisterType<Consumer>().As<IConsumer>().WithAttributeFiltering();
Here is the LocationRepository and LocationRepositoryProxyCache
public class Consumer : IConsumer
{
private ILocationRepository cacheRepo;
private ILocationRepository fetchRepo;
public Consumer([KeyFilter(SourceType.Fetching)] ILocationRepository fetchService ,
[KeyFilter(SourceType.Caching)] ILocationRepository cacheService)
{
cacheRepo = cacheService;
fetchRepo = fetchService;
}
public void Test()
{
cacheRepo.GetByIDAsync(1);
fetchRepo.GetByIDAsync(2);
}
}
public interface IConsumer {
void Test();
}
I hope, it can help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论