Autofac 当注入类似于 Grace/Ninject 时

huangapple go评论75阅读模式
英文:

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&lt;Location?&gt; GetByIDAsync(int id);
}

public class LocationRepositoryProxyCache : BaseProxyCache, ILocationRepository
{
	private readonly ICacheProvider&lt;Location&gt; _cacheProvider;

	private readonly ILocationRepository _locationRepository;

	public LocationRepositoryProxyCache(ICacheProvider&lt;Location&gt; cacheProvider, ILocationRepository locationRepository)
	{
		_cacheProvider = cacheProvider;
		_locationRepository = locationRepository;
	}
	public Task&lt;Location?&gt; GetByIDAsync(int id)
	{
		return _cacheProvider.GetOrSetAsync(&quot;CACHE_KEY&quot;, () =&gt; _locationRepository.GetByIDAsync(id));
	}
}

public class LocationRepository : BaseRepository&lt;Location&gt;, ILocationRepository
{
	public LocationRepository(IMyInterface myAppDb) : base(myAppDb) { }

	public Task&lt;Location?&gt; GetByIDAsync(int id)
	{
		return ...;
	}
}

Grace code to migrate to Autofac

registrationBlock.Export&lt;LocationRepository&gt;().As&lt;ILocationRepository&gt;().When.InjectedInto&lt;LocationRepositoryProxyCache&gt;();
registrationBlock.Export&lt;LocationRepositoryProxyCache&gt;().As&lt;ILocationRepository&gt;();

答案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&lt;LocationRepository&gt;().Keyed&lt;ILocationRepository&gt;(SourceType.Fetching);
		builder.RegisterType&lt;LocationRepositoryProxyCache&gt;().Keyed&lt;ILocationRepository&gt;(SourceType.Caching);
		 
		builder.RegisterType&lt;Consumer&gt;().As&lt;IConsumer&gt;().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.

huangapple
  • 本文由 发表于 2023年7月23日 23:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定