Breeze / breeze.sharp

Breeze for C#, F#, and VB.NET client applications
MIT License
82 stars 33 forks source link

Entity Framework Validation errors not being handled correctly by breeze.sharp client. #3

Open chrismon opened 10 years ago

chrismon commented 10 years ago

This issue is in the 0.5.4 release of Breeze.Sharp. Not sure if it should be fixed in Breeze.Sharp or perhaps Breeze.ContextProvider.EF6, but there seems to be a disconnect between expected behaviors.

Unexpected Behavior: I have a .Net Web API BreezeController that contains an EF6 repository.

If I perform object level validation via BeforeSaveEntities override, where the guidance is to fail validation by throwing an EntityErrorsException, the Breeze.Sharp client catches this as expected and all is well.

If, however, I perform attribute level validation using an Entity Framework ValidationAttribute class, instead of an EntityErrorsException, failures are returned to the Breeze.Sharp client as a SaveResult containing an Errors collection. In this case, the client throws an exception: Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JArray'. This exception occurs when Breeze.Sharp tries to parse the SaveResult's KeyMappings array, which is null.

Workaround: For now, rather than changing all of my Validation Attributes to throw an EntityErrorsException, I resolved this in my BreezeController's SaveChanges method by checking the returning SaveResults for any errors, and if found, wrapping those errors in an exception that I then throw to the client:

[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
    var saveResult = _unitOfWork.Commit(saveBundle);
    if (saveResult.Errors != null && saveResult.Errors.Count > 0)
    {
        var errors = new List<EntityError>();
        foreach (var error in saveResult.Errors)
        {
            var entityError = error as EntityError; //errors are stored as List<object> in the SaveResult
            if (string.IsNullOrEmpty(entityError.ErrorMessage))
            {
                entityError.ErrorName = "Attribute Validation Error";
            }
            errors.Add(entityError);
        }
        throw new EntityErrorsException(errors);
    }
    return saveResult;
}
ponmani commented 10 years ago

Hi chrismon,

Can you please samples breeze.sharp using SPA(single page applications). Samples 1)Controller 2)Views(Html) 3)viewmodel(javascript) 4)Breeze.sharp using examples

chrismon commented 10 years ago

Hello, yes I have reviewed samples and am working with an Angular application as well at the moment without issues. The issue I ran into with this post was while communicating with my Breeze service from a new Breeze.Sharp .Net client I am working on.

If there is something I am doing wrong in the service that you think the samples would help me understand, could you please expand on that?

ponmani commented 10 years ago

Hi chrismon,

I know chirsmon you expected breeze.sharp worked many project . But I am beginner of the breeze.sharp. How do i used breeze.sharp i don't know. So can you help little examples. Please

steveschmitt commented 10 years ago

ponmani, please do not use github to ask for examples. It is for reporting bugs only. StackOverflow is the place for questions.

Also, you have some confusion about breeze.sharp. Breeze.sharp is a client-side framework that runs in the .NET platform. If you were writing a desktop application using Windows Forms or WPF, or a Windows 8 application, you would use breeze.sharp.

If you want to use HTML and JavaScript, you would use breeze.js on the client. There are plenty of samples and tutorials using breeze.js.

If you are using ASP.NET on the server, you would use breeze.server.net. It works with Entity Framework and NHibernate to make it easy to write a backend for your breeze.js or breeze.sharp client.

If you have any further questions, please ask them on StackOverflow

ponmani commented 10 years ago

Oh. Thank u. Yes I already confused breeze.sharp is server side framework. But not now I am not confused. Thank you

On Fri, Jun 6, 2014 at 2:13 AM, Steve Schmitt notifications@github.com wrote:

ponmani, please do not use github to ask for examples. It is for reporting bugs only. StackOverflow http://stackoverflow.com/questions/tagged/breeze is the place for questions.

Also, you have some confusion about breeze.sharp. Breeze.sharp is a client-side framework that runs in the .NET platform. If you were writing a desktop application using Windows Forms or WPF, or a Windows 8 application, you would use breeze.sharp.

If you want to use HTML and JavaScript, you would use breeze.js https://github.com/Breeze/breeze.js on the client. There are plenty of samples http://www.breezejs.com/samples and tutorials https://www.google.com/#q=breeze+js+tutorial using breeze.js.

If you are using ASP.NET on the server, you would use breeze.server.net https://github.com/Breeze/breeze.server.net. It works with Entity Framework and NHibernate to make it easy to write a backend for your breeze.js or breeze.sharp client.

If you have any further questions, please ask them on StackOverflow http://stackoverflow.com/questions/tagged/breeze

— Reply to this email directly or view it on GitHub https://github.com/Breeze/breeze.sharp/issues/3#issuecomment-45272009.

ponmani commented 10 years ago

I already used breeze.js an breeze controller in durandal framework. Because I doubt breeze.sharp is controller. It is not clent side framework. Thank you for your solutions.

On Fri, Jun 6, 2014 at 9:35 AM, Pon Mani ponmani096@gmail.com wrote:

Oh. Thank u. Yes I already confused breeze.sharp is server side framework. But not now I am not confused. Thank you

On Fri, Jun 6, 2014 at 2:13 AM, Steve Schmitt notifications@github.com wrote:

ponmani, please do not use github to ask for examples. It is for reporting bugs only. StackOverflow http://stackoverflow.com/questions/tagged/breeze is the place for questions.

Also, you have some confusion about breeze.sharp. Breeze.sharp is a client-side framework that runs in the .NET platform. If you were writing a desktop application using Windows Forms or WPF, or a Windows 8 application, you would use breeze.sharp.

If you want to use HTML and JavaScript, you would use breeze.js https://github.com/Breeze/breeze.js on the client. There are plenty of samples http://www.breezejs.com/samples and tutorials https://www.google.com/#q=breeze+js+tutorial using breeze.js.

If you are using ASP.NET on the server, you would use breeze.server.net https://github.com/Breeze/breeze.server.net. It works with Entity Framework and NHibernate to make it easy to write a backend for your breeze.js or breeze.sharp client.

If you have any further questions, please ask them on StackOverflow http://stackoverflow.com/questions/tagged/breeze

— Reply to this email directly or view it on GitHub https://github.com/Breeze/breeze.sharp/issues/3#issuecomment-45272009.

ponmani commented 9 years ago

Hi steveschmitt,

I can't undertsand breeze.js and breeze.sharp.But I know that breeze.js and breeze controller using single page application. Can you explain breeze.sharp topic with your simple examples.

steveschmitt commented 9 years ago

Hi ponmani, Please post questions on StackOverflow.
Ideablade also offers support and consulting - contact breeze@ideablade.com