Autofac register assembly types

前端 未结 4 1060
太阳男子
太阳男子 2021-02-05 08:25

In Castle, I used to do the following to register types from a different assembly:

Classes.FromAssemblyNamed(\"MyServer.DAL\")
       .Where(type => type.Name         


        
4条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 08:49

    You can use the As's predicate overload! You can get the all of the interfaces with GetInterfaces from the given types that ends with "Repository" and then select the first interface which they implement and register it.

    var assembly = Assembly.GetExecutingAssembly();
    ContainerBuilder builder = new ContainerBuilder();
    
    builder.RegisterAssemblyTypes(assembly)
        .Where(t => t.Name.EndsWith("Repository"))
        .As(t => t.GetInterfaces()[0]);
    

提交回复
热议问题