如何使用BreezeJS for .net core实现EFContextProvider?

时间:2018-05-07 17:30:12

标签: breeze breeze-sharp

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

以下breeze libs中不存在EFContextProvider:

  • Breeze.Core
  • Breeze.Persistence
  • Breeze.pErsistence.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;

谢谢!

1 个答案:

答案 0 :(得分: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);
    }

    ...
  }

希望这会有所帮助。