【问题标题】:Extending Service/IService to add common dependencies扩展 Service/IService 以添加公共依赖项
【发布时间】:2013-09-06 12:57:44
【问题描述】:

我需要扩展 Service/IService 以允许我注册其他资源,例如每个单独的服务可能需要处理的其他数据库连接和自定义类。

对服务进行子类化是正确的方法吗?另外,我不清楚我是否还有另一个(比如)IDbConnection,Funq 如何确定将值注入哪个属性。

【问题讨论】:

    标签: servicestack funq


    【解决方案1】:

    如果您有多个相同类型的服务,您需要在 funq 中使用名称注册它们。不幸的是,我认为 funq 不能正确地自动装配属性,因此您需要手动解决它们。

    container.Register("Security", x => new SecurityDataContext()); container.Register("Customers", x => new CustomersDataContext()); container.Register("Reporting", x => new ReportingDataContext()); container.Register(x => new ReportRepositoryImpl(x.ResolveNamed("Reporting")));

    另一种方法是为每种类型创建一个唯一的接口(即使它没有成员),然后在 funq 中使用它。这将允许自动装配

    container.Register(x => new SecurityDataContext()); container.Register(x => new CustomersDataContext()); container.Register(x => new ReportingDataContext()); // 这可能只是自动装配的 container.Register(x => new ReportRepositoryImpl(x.Resolve()));

    如果你仍然真的需要扩展 Service,你可以在 c# 中使用标准继承

    公共抽象类 BaseService : 服务 { // 自定义的东西放在这里 公共字符串示例(){ 返回“你好世界”; } } 公共类 ReportsService : BaseService { 公共字符串获取(ListReports 请求){ 返回示例(); } }

    【讨论】:

      【解决方案2】:

      您可以在不扩展服务的情况下轻松配置其他数据库连接,只需在 AppHost.cs 文件中的配置方法中连接它们即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-06
        • 1970-01-01
        • 2023-04-11
        • 2017-10-23
        • 1970-01-01
        • 1970-01-01
        • 2019-12-19
        • 1970-01-01
        相关资源
        最近更新 更多