Servicestack with funq - auto install by agreement

I have a service that accepts IMyDependency in its constructor. IMyDependency, MyDependency and all services live in the same assembly. MyDependency has a single, open, parameterless constructor.

To my surprise, this did not work:

container.RegisterAutoWired<IMyDependency>(); 

It throws a "System.NullReferenceException" exception.

It works if I do this:

 container.RegisterAutoWiredAs<MyDependency, IMyDependency>(); 

But then so:

 container.RegisterAs<MyDependency, IMyDependency>(); 

So what is the difference? If "automatic posting" cannot find a specific implementation, and it does not matter if services requiring dependencies can be resolved, then what is automatic posting?

Is Funq supposed to be able to find your specific implementations by convention? If so, what is this agreement, if not the same name-ness?

Thanks.

+6
source share
2 answers

For simple queries like this, it's best to just contact the source, for example. here is the source code of RegisterAutoWired :

 public IRegistration<T> RegisterAutoWired<T>() { var serviceFactory = GenerateAutoWireFn<T>(); return this.Register(serviceFactory); } 

It generates an automatically connected factory to a specific implementation . An interface has no implementation; it must be a specific class.

And the source code for RegisterAs :

 public IRegistration<TAs> RegisterAs<T, TAs>() where T : TAs { return this.RegisterAutoWiredAs<T, TAs>(); } 

This is just a shorter alias that you can use instead of RegisterAutoWiredAs.

+4
source

You mean "how can I implement an assembly search solution and automatically register classes in the ServiceStack IOC based on the agreement?"

If so, I may have a solution for you:

  • Create an interface that implements your injectable classes.
  • Let your injective classes implement this interface.
  • In download-bound code, use reflection to search for your assemblies and get a list of all classes that implement an input-friendly interface.
  • Use reflection to get the class name and interface based on your conventions.
  • Call the ServiceStack IOC RegisterAutoWiredType method and pass the class and interface to register them.

For example, if our naming convention is ClassName IClassName:

 private static void RegisterCustomTypes(Container container) { //Get the Assembly Where the injectable classes are located. var assembly = Assembly.GetAssembly(typeof(IInjectable)); //Get the injectable classes var types =assembly.GetTypes() .Where(m => m.IsClass && m.GetInterface("IInjectable") != null); //loop through the injectable classes foreach (var theType in types) { //set up the naming convention var className = theType.Name; var interfaceName = string.Concat("I", className); //create the interface based on the naming convention var theInterface = theType.GetInterface(interfaceName); //register the type with the convention container.RegisterAutoWiredType(theType, theInterface); } } public interface IInjectable { } //This class can be injected public interface ITestManager : IInjectable { void Execute(int id); } public class TestManager : ITestManager { public void Execute(int id) { throw new System.NotImplementedException(); } } 
+8
source

Source: https://habr.com/ru/post/943669/


All Articles