0

How to register generic interface as shown below:

public interface ISigQuery<T> where T : SigReadModel, new()

public class SigQuery : ISigtQuery<SigReadModel>
{
}

I tried to register as follows:

.RegisterType(typeof(ISigQuery<>), typeof(SigQuery))(new ContainerControlledLifetimeManager())

I get error:

Method name expected

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Arie
  • 3,041
  • 7
  • 32
  • 63
  • Does this answer your question? [Unity: Register and resolve class with generic type](https://stackoverflow.com/questions/35334240/unity-register-and-resolve-class-with-generic-type) – Selim Yildiz Feb 26 '20 at 10:49
  • @SelimYıldız No, i tried already that: .RegisterType(typeof(ISigQuery<>), typeof(SigQuery<>))(new ContainerControlledLifetimeManager()) then i get: The non-generic type 'SigQuery' cannot be used with type arguments – Arie Feb 26 '20 at 10:58

2 Answers2

1

Since SigQuery implements ISigtQuery<SigReadModel>, you will have to register it by that closed-generic version of ISigtQuery<T>:"

.RegisterType(
    typeof(ISigQuery<SigReadModel>),
    typeof(SigQuery),
    new ContainerControlledLifetimeManager());

Of you can use the generic RegisterType overload:

.RegisterType<ISigQuery<SigReadModel>, SigQuery>(
    new ContainerControlledLifetimeManager());
Steven
  • 166,672
  • 24
  • 332
  • 435
0

According to usage of RegisterType you need to change:

.RegisterType(typeof(ISigQuery<>), typeof(SigQuery))(new ContainerControlledLifetimeManager())

to

.RegisterType(typeof(ISigQuery<>), typeof(SigQuery), new ContainerControlledLifetimeManager())
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28