(.net5.0)AutoFac和AutoMapper的使用方法


autofac:引用Autofac.Extensions.DependencyInjection

program:


public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
}).UseServiceProviderFactory(new AutofacServiceProviderFactory());


stateup:
public void ConfigureContainer(ContainerBuilder builder)
{
  var assemblysServices = Assembly.Load("Show.Repository");
  var assemblysServices1 = Assembly.Load("Show.Service");
  builder.RegisterAssemblyTypes(assemblysServices, assemblysServices1).AsImplementedInterfaces().InstancePerLifetimeScope();
}


autoMapper:引用AutoMapper.Extensions.Microsoft.DependencyInjection

 

stateup:
  services.AddAutoMapper(x =>x.CreateMap());
  services.AddAutoMapper(x =>x.CreateMap());


repository:
  mapper.Map(deptRoleDto);
  or
  DB.Depatment.ProjectTo(mapper.ConfigurationProvider).ToList();

相关