Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created March 2, 2021 07:26
Show Gist options
  • Save danielplawgo/c359d85dc3f9e402224d2161a1783f4c to your computer and use it in GitHub Desktop.
Save danielplawgo/c359d85dc3f9e402224d2161a1783f4c to your computer and use it in GitHub Desktop.
Scrutor auto rejestracja typów
public interface IRepository
{
}
public interface ICategoryRepository : IRepository
{
Category GetById(Guid id);
}
public class CategoryRepository : ICategoryRepository
{
public Category GetById(Guid id)
{
return new Category()
{
Id = id
};
}
}
public interface IProductRepository : IRepository
{
Product GetById(Guid id);
}
public class ProductRepository : IProductRepository
{
public Product GetById(Guid id)
{
return new Product()
{
Id = id
};
}
}
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddRepositories(this IServiceCollection services)
{
services.Scan(s => s.FromAssemblyOf<IRepository>()
.AddClasses(c => c.AssignableTo<IRepository>())
.AsImplementedInterfaces()
.WithScopedLifetime());
return services;
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IProductRepository, ProductRepository>();
services.AddScoped<ICategoryRepository, CategoryRepository>();
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ScrutorTests", Version = "v1" });
});
}
public void ConfigureServices(IServiceCollection services)
{
services.Scan(s => s.FromAssemblyOf<IRepository>()
.AddClasses(c => c.AssignableTo<IRepository>())
.AsImplementedInterfaces()
.WithScopedLifetime());
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ScrutorTests", Version = "v1" });
});
}
public void ConfigureServices(IServiceCollection services)
{
services.AddRepositories();
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ScrutorTests", Version = "v1" });
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment