Service Fabric Autofac怎么样?

时间:2018-05-21 11:19:58

标签: autofac azure-service-fabric

我试图在我的SF中配置IoC(我还不熟悉的概念),如下所述:https://www.codeproject.com/Articles/1217885/Azure-Service-Fabric-demo和此处:https://alexmg.com/posts/introducing-the-autofac-integration-for-service-fabric

program.cs中的

- 主要:

var builder = new ContainerBuilder();
builder.RegisterModule(new GlobalAutofacModule());
builder.RegisterServiceFabricSupport();
builder.RegisterStatefulService<Payment>("PaymentType");

using (builder.Build())
{
    ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(Payment).Name);
    Thread.Sleep(Timeout.Infinite);
}

GlobalAutofacModule:

public class GlobalAutofacModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<ChargeRepository>().As<IChargeRepository>().SingleInstance();
        builder.RegisterType<CustomerRepository>().As<ICustomerRepository>().SingleInstance();
        builder.RegisterType<InvoiceItemRepository>().As<IInvoiceItemRepository>().SingleInstance();
        builder.RegisterType<PlanRepository>().As<IPlanRepository>().SingleInstance();
        builder.RegisterType<ProductRepository>().As<IProductRepository>().SingleInstance();
        builder.RegisterType<SourceRepository>().As<ISourceRepository>().SingleInstance();
        builder.RegisterType<SubscriptionRepository>().As<ISubscriptionRepository>().SingleInstance();
        builder.RegisterType<TokenRepository>().As<ITokenRepository>().SingleInstance();
    }
}

无故障地调用服务

        public Payment(StatefulServiceContext context, 
        IChargeRepository chargeRepo, 
        ICustomerRepository customerRepo,
        IInvoiceItemRepository invoiceItemRepo,
        IPlanRepository planRepository,
        IProductRepository productRepo,
        ISourceRepository sourceRepo,
        ISubscriptionRepository subscriptionRepo,
        ITokenRepository tokenRepo)
        : base(context)
    { ... }

在其中一个方法中,它需要调用一个自定义映射器(错误的参数错误)

var test = new Mapper().GetProductsDto(false, false);

这个类定义如下:

    private readonly IChargeRepository _chargeRepo;
    private readonly ICustomerRepository _customerRepo;
    private readonly IInvoiceItemRepository _invoiceItemRepo;
    private readonly IPlanRepository _planRepo;
    private readonly IProductRepository _productRepo;
    private readonly ISourceRepository _sourceRepo;
    private readonly ISubscriptionRepository _subscriptionRepo;
    private readonly ITokenRepository _tokenRepo;

    public Mapper(IChargeRepository chargeRepo,
        ICustomerRepository customerRepo,
        IInvoiceItemRepository invoiceItemRepo,
        IPlanRepository planRepository,
        IProductRepository productRepo,
        ISourceRepository sourceRepo,
        ISubscriptionRepository subscriptionRepo,
        ITokenRepository tokenRepo)
    {
        _chargeRepo = chargeRepo;
        _customerRepo = customerRepo;
        _invoiceItemRepo = invoiceItemRepo;
        _planRepo = planRepository;
        _productRepo = productRepo;
        _sourceRepo = sourceRepo;
        _subscriptionRepo = subscriptionRepo;
        _tokenRepo = tokenRepo;
    }
public IEnumerable<ProductListDto> GetStripeProductsDto(bool isLogged, bool isSubscriber) {...}

那么如何实例化映射器并调用方法而不将每个repo作为params传递?

编辑:tmp解决方案直到approuved / disapprouved

    private static void Main()
    {
        try
        {
            using (ContainerOperations.Container)
            {
                ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(Payment).Name);
                Thread.Sleep(Timeout.Infinite);
            }
        }
        catch (Exception e)
        {
            ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
            throw;
        }
    }
}

public class ContainerOperations
{
    private static readonly Lazy<IContainer> _containerSingleton =
        new Lazy<IContainer>(CreateContainer);

    public static IContainer Container => _containerSingleton.Value;

    private static IContainer CreateContainer()
    {
        var builder = new ContainerBuilder();
        builder.RegisterModule(new GlobalAutofacModule());
        builder.RegisterServiceFabricSupport();
        builder.RegisterStatefulService<Payment>("Inovatic.SF.Windows.PaymentType");
        return builder.Build();
    }
}


public class GlobalAutofacModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        //builder.RegisterType<Mapper>();

        builder.RegisterType<ChargeRepository>().As<IChargeRepository>().SingleInstance();
        builder.RegisterType<CustomerRepository>().As<ICustomerRepository>().SingleInstance();
        builder.RegisterType<InvoiceItemRepository>().As<IInvoiceItemRepository>().SingleInstance();
        builder.RegisterType<PlanRepository>().As<IPlanRepository>().SingleInstance();
        builder.RegisterType<ProductRepository>().As<IProductRepository>().SingleInstance();
        builder.RegisterType<SourceRepository>().As<ISourceRepository>().SingleInstance();
        builder.RegisterType<SubscriptionRepository>().As<ISubscriptionRepository>().SingleInstance();
        builder.RegisterType<TokenRepository>().As<ITokenRepository>().SingleInstance();

    }
}

调用现在是这样的:var productListDto = Mapper.GetStripeProductsDto(isLogged,false); 映射器:

    private static IProductRepository _productRepo => ContainerOperations.Container.Resolve<IProductRepository>();

    public static IEnumerable<ProductListDto> GetStripeProductsDto(bool isLogged, bool isSubscriber)
    {
        var productList = _productRepo.GetAllStripeProducts().ToList();

1 个答案:

答案 0 :(得分:0)

我认为您还应该在IoC容器中注册Mapper类并将其添加到Payment的构造函数中,然后容器将为您创建包含所有必需参数的Mapper。你可以这样做 builder.RegisterType<Mapper>().SingleInstance();