如何在ASP.NET Core中创建类扩展? [英] How to create a class extension in asp.net core?

查看:221
本文介绍了如何在ASP.NET Core中创建类扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 userManager 创建我的自定义服务.但是我不知道如何访问 dbContext .

I want to create my custom service via using userManager. But I don't know how to access to dbContext.

public static class UserManagerExtensions
{
    public static async Task<IdentityResult> AddProfileAsync(this UserManager<ApplicationUser> userManager, UserProfileModel model)
    {

        // how to access to dbContext here???

        if (await dbContext.SaveChangesAsync() > 0)
        {
            return IdentityResult.Success;
        }

        return IdentityResult.Failed();
    }
}

用法:

public class UserController : Controller
{
    private readonly UserManager<ApplicationUser> _userManager;

    public UserController(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
    }

    public async Task<IActionResult> Update(UserProfileModel model)
    {
        await _userManager.AddProfileAsync(model);

        return View();
    }
}

如何在静态类中访问 DbContext 类?

How to access DbContext class inside a static class?

谢谢!

推荐答案

您应将其注入到控制器中,然后将其传递给您的方法:

You should inject it into your controller and then pass it to your method:

public class UserController : Controller
{
    private readonly DbContext _dbContext;
    private readonly UserManager<ApplicationUser> _userManager;

    public UserController(DbContext dbContext, UserManager<ApplicationUser> userManager)
    {
        _dbContext = dbContext;
        _userManager = userManager;
    }

    public async Task<IActionResult> Update(UserProfileModel model)
    {
        await _userManager.AddProfileAsync(_dbContext, model);

        return View();
    }
}

这样,您就可以完全控制数据库上下文的来源以及数据库上下文的生存时间.

That way, you have full control over where there database context comes from and with what kind of lifetime it exists.

如果您的目标是像您所说的那样实际创建服务,则应该这样做,并且根本不使用扩展方法.您可以这样做:

If your goal is to actually create a service as you say, you should do so and not work with extension methods at all. You could do this:

public class UserController : Controller
{
    private readonly ProfileService _profileService;

    public UserController(ProfileService profileService)
    {
        _profileService = profileService;
    }

    public async Task<IActionResult> Update(UserProfileModel model)
    {
        await _profileService.AddProfileAsync(model);
        return View();
    }
}

public class ProfileService
{
    private readonly DbContext _dbContext;
    private readonly UserManager<ApplicationUser> _userManager;

    public ProfileService(DbContext dbContext, UserManager<ApplicationUser> userManager)
    {
        _dbContext = dbContext;
        _userManager = userManager;
    }

    public async Task<IdentityResult> AddProfileAsync(UserProfileModel model)
    {
        // do stuff
        if (await dbContext.SaveChangesAsync() > 0)
        {
            return IdentityResult.Success;
        }
        return IdentityResult.Failed();
    }
}

请不要忘记使用以下方法在 Startup.ConfigureServices 中注册您的服务:

And don’t forget to register your service in Startup.ConfigureServices using:

services.AddTransient<ProfileService>();

这样,您现在有了一个具有明确依赖关系并且可以正确测试的实际服务,并且还从控制器中移出了更多逻辑,这使得推理起来更加容易.

That way, you now have an actual service which has clear dependencies and which you can test properly, and you are also moving more logic out of your controllers which makes it easier to reason about it.

这篇关于如何在ASP.NET Core中创建类扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆