Open abhishek7911 opened 4 years ago
yes I think it is required major changes for migrating new version. For now, I am waiting .net 5 to upgrade new version because this repository needs to huge refactoring. That's why lets leave it as it is since .net 5 is raised.
ok thanks for your quick response i have migrated every thing login is working fine but getting error in DI. i have take reference from "https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/microservice-application-layer-implementation-web-api" but still it's not fixed. getting 404 while requesting API controller. can you help with this part?
please review below changes.
1) startup
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
var container = new ContainerBuilder();
container.Populate(services);
container.RegisterModule(new MediatorModule());
container.RegisterModule(new InfrastructureModule());
container.RegisterModule(new ApplicationModule());
return new AutofacServiceProvider(container.Build());
2) Mediator Module
public class MediatorModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(typeof(CreateProductCommandHandler).GetTypeInfo().Assembly)
.AsClosedTypesOf(typeof(IRequestHandler<,>));
// Register the Command's Validators (Validators based on FluentValidation library)
builder.RegisterAssemblyTypes(typeof(CreateProductRequestValidator).GetTypeInfo().Assembly)
.Where(t => t.IsClosedTypeOf(typeof(IValidator<>)))
.AsImplementedInterfaces();
}
}
3) InfrastructureModule
public class InfrastructureModule
:Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register<ServiceFactory>(context =>
{
var componentContext = context.Resolve<IComponentContext>();
return t => { object o; return componentContext.TryResolve(t, out o) ? o : null; };
});
builder.RegisterType<ProductRepository>().As<IProductRepository>().InstancePerDependency();
builder.RegisterType<CategoryRepository>().As<ICategoryRepository>().InstancePerDependency();
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerDependency();
builder.RegisterGeneric(typeof(EnumRepository<>)).As(typeof(IEnumRepository<>)).InstancePerDependency();
builder.RegisterGeneric(typeof(RepositoryBase<,>)).As(typeof(IRepositoryBase<,>)).InstancePerDependency();
builder.RegisterGeneric(typeof(LoggerAdapter<>)).As(typeof(IAppLogger<>)).InstancePerDependency();
builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly)
.AsImplementedInterfaces();
builder.RegisterGeneric(typeof(TransactionBehaviour<,>)).As(typeof(IPipelineBehavior<,>));
}
}
4) Application Module
public class ApplicationModule
: Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<ProductService>().As<IProductService>().InstancePerLifetimeScope();
builder.RegisterType<CategoryService>().As<ICategoryService>().InstancePerLifetimeScope();
builder.RegisterType<CustomerService>().As<ICustomerService>().InstancePerLifetimeScope();
}
}
yes the article is very good reference to take for this repo refactoring. Actually I cant see any problem with registering classes into Autofac. When you get the 404 error, is this makes problem to cant resolve any interfaces ? Since I cant simulate the error with existing codebase, its hard to find solution with this way :/
it resolved on the same day. as we discussed it's DI issues. after that i followed below autofac documentation, for configuring DI and it's working fine. https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html
great news. so if you want to send pull request I would like to review and merge it. For frontend part we will check later. Thanks.
I have try to convert in .net Core 3.1 but some how it's not working with existing architecture.