Autofac如何使用工厂模式?

问题描述 投票:0回答:1

目前我有以下内容:

public interface IService
{
    void Start();
}

public class FirstService : IService
{ }

我通过以下方式解决服务:

using (var scope = lifetimeScope.BeginLifetimeScope())
using (var service = scope.Resolve<IService>()) 
{ }

这很好用。不过,现在我要介绍第二个基于

IService
的服务。所以我添加了以下内容:

public enum ServiceType {
   First = 0, Second
}

public class SecondService: IService
{ }

如何引入带

ServiceType
参数并解析正确服务的工厂? (我可以使用
switch
但也许这不是最佳做法)

c# autofac factory
1个回答
1
投票

很久以前(很久以前我不完全记得它是如何工作的:-) Func<TKey, TDep>

© www.soinside.com 2019 - 2024. All rights reserved.