Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top