Breeze: EFContextProvider/Breeze Controller and Service Layer

独自空忆成欢 提交于 2019-12-24 01:27:57

问题


When using Breeze, I was wondering how one would go about integrating it with a service layer which handles things such as email notifications, audit logs, business validations (ie Customer must exist) etc..

For example, given the following scenario:

public class SalesAppRepository
{
    private readonly EFContextProvider<SalesAppContext> _contextProvider;

    public SalesAppRepository(EFContextProvider<SalesAppContext> contextProvider)
    {
        _contextProvider = contextProvider;
    }

    private SalesAppContext Context { get { return _contextProvider.Context; } }

    public string MetaData
    {
        get { return _contextProvider.Metadata(); }
    }

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

    public IQueryable<Customer> Customers
    {
        get { return Context.Customers; }
    }

    public IQueryable<Order> Orders
    {
        get { return Context.Orders; }
    }

   /* Other entities */
}

[BreezeController]
public class BreezeController : ApiController
{
    private readonly SalesAppRepository _repository; 

    public BreezeController(SalesAppRepository repository)
    {
        _repository = repository;
    }

    [HttpGet]
    public string Metadata()
    {
        return _repository.MetaData;
    }

    [HttpPost]
    public SaveResult SaveChanges(JObject saveBundle)
    {
        return _repository.SaveChanges(saveBundle);
    }

    [HttpGet]
    public IQueryable<Customer> Customers()
    {
        return _repository.Customers;
    }

    [HttpGet]
    public IQueryable<Order> Orders()
    {
        return _repository.Orders;
    }

    /* Other entities */
}

I have some other business logic when entities are being created, modified etc.. such as when an Order is created; the Customer must be able to place an order (bool value canOrder) and must exist, also an email must be sent and an audit log must be updated to show the newly created order. This is just one scenario, there are other situations which would usually sit in my service layer, but I'm not sure how I would incorporate this with breeze?

I understand I can override the SaveChanges method on the SalesAppContext to generate the audit log, but what about the other situations? I'm not sure where I would put this in regards to above?

Edit

I seem to have found an, albeit, hacky way to get around this problem, I have uploaded a gist which I hope could help someone or be improved upon: https://gist.github.com/CallumVass/8300400


回答1:


i've incorporated my business logic in the beforeSaveEntities(..) Method: http://www.breezejs.com/documentation/efcontextprovider



来源:https://stackoverflow.com/questions/20969743/breeze-efcontextprovider-breeze-controller-and-service-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!