1

我已经能够使用基于 Breeze Temphire 示例和微风.server.net/AspNetCore/ github 示例的 AspNetCore 将 Breeze 实现到 Angular 应用程序中。我一直试图弄清楚如何在工作单元中实现 EFContextProvider。参考Github 上的 Tempire Code Temphire,有人可以告诉我如何使用 AspNetCore 创建 UnitOfWork 吗?

以下微风库中不存在 EFContextProvider:

  • 微风核心
  • 微风.持久
  • Breeze.pEsistence.EFCore
  • Breeze.AspNetCore.NetCore

为了清楚起见,我想使用 AspNetCore/Breeze 库创建以下内容:

public class UnitOfWork
{
    private readonly EFContextProvider<TempHireDbContext> _contextProvider;

    public UnitOfWork()
    {
        _contextProvider = new EFContextProvider<TempHireDbContext>();

        StaffingResources = new Repository<StaffingResource>(_contextProvider.Context);
        Addresses = new Repository<Address>(_contextProvider.Context);
        AddressTypes = new Repository<AddressType>(_contextProvider.Context);
        PhoneNumbers = new Repository<PhoneNumber>(_contextProvider.Context);
        PhoneNumberTypes = new Repository<PhoneNumberType>(_contextProvider.Context);
        Rates = new Repository<Rate>(_contextProvider.Context);
        RateTypes = new Repository<RateType>(_contextProvider.Context);
        Skills = new Repository<Skill>(_contextProvider.Context);
        States = new Repository<State>(_contextProvider.Context);
        WorkExperienceItems = new Repository<WorkExperienceItem>(_contextProvider.Context);

        StaffingResourceListItems = new StaffingResourceListItemRepository(_contextProvider.Context);
    }

    public IRepository<StaffingResource> StaffingResources { get; private set; }
    public IRepository<Address> Addresses { get; private set; }
    public IRepository<AddressType> AddressTypes { get; private set; }
    public IRepository<PhoneNumber> PhoneNumbers { get; private set; }
    public IRepository<PhoneNumberType> PhoneNumberTypes { get; private set; }
    public IRepository<Rate> Rates { get; private set; }
    public IRepository<RateType> RateTypes { get; private set; }
    public IRepository<Skill> Skills { get; private set; }
    public IRepository<State> States { get; private set; }
    public IRepository<WorkExperienceItem> WorkExperienceItems { get; private set; }

    public IStaffingResourceListItemRepository StaffingResourceListItems { get; private set; }

    public SaveResult Commit(JObject changeSet)
    {
        return _contextProvider.SaveChanges(changeSet);
    }
}

}

可以像下面这样简单吗?

 private readonly EFPersistenceManager<DictionaryPortalContext> _contextProvider;

谢谢!

4

1 回答 1

0

很接近了。

我们通常创建一个单独的类,以便您可以在某个地方添加“BeforeSaveEntities”和“AfterSaveEntities”方法。

public class MyPersistenceManager : EFPersistenceManager<DictionaryPortalContext> {
    public MyPersistenceManager (DictionaryPortalContext dbContext) : base(dbContext) { 
   // PM methods here.
 }

接着

  [Route("breeze/[controller]/[action]")]
  [BreezeQueryFilter]
  public class MyController : Controller {
    private MyPersistenceManager PersistenceManager;

    // called via DI 
    public MyController(NorthwindIBContext_CF context) {
      PersistenceManager = new MyPersistenceManager(context);
    }


    [HttpGet]
    public IActionResult Metadata() {
      return Ok(PersistenceManager.Metadata());
    }
    [HttpPost]
    public SaveResult SaveChanges([FromBody] JObject saveBundle) {
      return PersistenceManager.SaveChanges(saveBundle);
    }

    ...
  }

希望这可以帮助。

于 2019-02-05T01:10:24.133 回答