如何注入业务逻辑层

时间:2019-11-19 16:57:10

标签: entity-framework-core razor-pages

我发现我的RazorModel类文件在多个页面上重复了代码。如何使用业务逻辑层或提取PrivateMethod来精简PageModel?

基本页面模型

public class PageModelBase : PageModel {}

RazorPage

public class IndexModel : PageModelBase {

private readonly ILogger<IndexModel> _logger;
private readonly MyDbContext _dbContext;

public IndexModel(ILogger<IndexModel> logger, MyDbContext context) {
            _logger = logger;
            _dbContext = context;
}

 public async Task<IActionResult> OnPostAsync() {
 if(ModelState.IsValid) {
    PrivateMethod(); // This method functionality is being repeated on more than one Page
  }
 }

  PrivateMethod() {
   // Do something with DbContext
  }
}

启动

public void ConfigureServices(IServiceCollection services) {
 // ....
services.AddDbContext<MyDbContext>(options =>
           options.UseSqlServer(
               Configuration.GetConnectionString("MyDbConnection")));
}

0 个答案:

没有答案