1

I have the need to extend Service/IService to allow me to register additional resources like other DB connections and custom classes that each individual service may need to get a handle to.

Is the proper way to do this to subclass Service? Also, it is not clear to me if I have another (say) IDbConnection how Funq figures out which Property to inject the value into.

Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75
LiteWait
  • 584
  • 4
  • 21
  • 42

2 Answers2

2

If you have multiple services with the same type you need to register them in funq with a name. Unfortunatly I don't think funq can autowire the properties correctly so you need to manually resolve them.

    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")));

An alternative approach would be to create a unique interface (even if it has no members) for each type and then use that in funq. This would allow autowiring

    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>()));

If you still really need to extend Service you can just use standard inheritance in 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();
        }
    }
Will Smith
  • 1,905
  • 1
  • 14
  • 19
0

You can configure other DB connections easily without extending the Service , but by just wiring them in the configure method in the AppHost.cs file.

Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79