扩展Service / IService以添加公共依赖项

时间:2013-09-06 12:57:45

标签: servicestack funq

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

这样做是否可以将子类化为Service?此外,我不清楚我是否有另一个(比方说)IDbConnection,Funq如何确定将值注入哪个属性。

2 个答案:

答案 0 :(得分:2)

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

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

    container.Register<IReportRepository>(x => new ReportRepositoryImpl(x.ResolveNamed<DataContext>("Reporting")));

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

    container.Register<ISecurityDataContext>(x => new SecurityDataContext());
    container.Register<ICustomersDataContext>(x => new CustomersDataContext());
    container.Register<IReportingDataContext>(x => new ReportingDataContext());

    // this could just be autowired
    container.Register<IReportRepository>(x => new ReportRepositoryImpl(x.Resolve<IReportingDataContext>()));

如果您仍然需要扩展服务,则可以在c#

中使用标准继承
    public abstract class BaseService : Service
    {
         // custom things go here
         public string Example() {
             return "Hello World";
         }
    }

    public class ReportsService : BaseService
    {
        public string Get(ListReports request) {
            return Example();
        }
    }

答案 1 :(得分:0)

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

相关问题