【发布时间】:2018-08-29 14:43:59
【问题描述】:
所以...在我的 .Net Core 2 启动文件中,我将我的存储库添加到 ConfigureServices 方法的范围中,如下所示...
public void ConfigureServices(IServiceCollection services)
{
var config = LoadConfiguration();
services.AddDbContext<DatabaseContext>(options => options.UseSqlServer(config.Connection, x => x.MigrationsAssembly("XXX")));
// Repositories
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<ISecurityFunctionRepository, SecurityFunctionRepository>();
services.AddScoped<IUserSecurityFunctionRepository, UserSecurityFunctionRepository>();
services.AddScoped<ICustomerRepository, CustomerRepository>();
// ... lots more...
// other goodies
}
我知道设置 .Net Core 2 API 的方法有上百万种,但我的具体问题是,将 30 多个存储库添加到范围是否会导致服务器或内存出现任何问题,或者是否有更好的方法定义大量存储库的方法。
在其他项目中,我创建了几个带有自己存储库的 API。从技术上讲,这避免了这个问题,但我想避免这是一个麻烦。
【问题讨论】:
-
最好的回答方法是对此进行基准测试。
-
好吧,`服务器或内存的任何问题`->注册将使用内存。除非在链接回注册的范围内解决依赖项,否则不会创建实例。注册本身使用的 RAM 量相对可以忽略不计。也就是说,如果您在 Raspberry Pi 上运行,您可能需要重新考虑您的方法。
if there is a better way to scope a ton of repositories- 我会使用反射,这样您就不必手动列出它们 -
有一个更好的方法:忘记存储库模式并使用 Entity Framework Core,因为它应该被使用
-
我们正在部署到 Azure 服务器,因此内存不应该像 Rasberry Pi 那样非常昂贵 :) @CamiloTerevinto - 我愿意接受更改,但我需要比“你糟透了”更多的细节。
-
@zaitsman 你能指出我谈论以这种方式使用反射的地方吗?谢谢!
标签: c# asp.net-core