4 Replies - 219 Views - Last Post: 08 March 2019 - 06:30 AM

#1 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,095
  • Joined: 12-December 12

at what level to switch 0 to null

Posted 08 March 2019 - 02:39 AM

I have an ASP.NET Core/Angular application with Kendo UI, although my question is not specific to this.

Kendo controls don't handle nulls very well. I have dropdowns that are presenting me with the value 0 when no item is selected. These dropdowns are for nullable fields.

I have a (Kendo) save event that I use to validate the data before allowing the save. My question is, at what level would you perform conversion of these 0 values into null values? Where does this conversion (business rule?) belong?

1. Wrestle with Kendo to force it to accept null. I don't want to go down this road, it is too painful.

2. In the JS save event. After standard validation, switch these 0's to (JS) nulls.

3. In the Angular service?

4. In the controller that receives the post data?

5. In the database trigger that I use to INSERT/UPDATE the data?

Is This A Good Question/Topic? 0
  • +

Replies To: at what level to switch 0 to null

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7442
  • View blog
  • Posts: 15,438
  • Joined: 16-October 07

Re: at what level to switch 0 to null

Posted 08 March 2019 - 03:05 AM

I was recently using Kendo Grid and it was sucking at a particular non string field, even though it seemed to be formatting it just fine. My ultimate solution was to transform the data fed in into a form that made it happy.

I find "transform data for dumb controls" tends to be my most consistent, if perhaps unsatisfyingly inelegant, solution. In most cases, you can just tack on a display field to your object and then the rest of the app can ignore it. Sometimes you'll want to compute a key as well. Depending on the control, you can use a function to derive your values and not touch storage at all.

Angular kind of annoys me, so I go with React. Recently, on a larger project, I bit the bullet and went with Redux as well. A nice toy from Redux land that suits the "transform all the things" solution are selectors, which optimize the process.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,095
  • Joined: 12-December 12

Re: at what level to switch 0 to null

Posted 08 March 2019 - 03:39 AM

Thank you. There is a lot of hoop jumping with Kendo. I use views to get the text values of dropdowns to display in the grid. Because we also have local versions held of these mini-lists I also use them to get the text value when the choice is changed.

For dropdowns we have also displayed dual inputs/controls; one that "works" and is hidden, and the Kendo version. Seems pointless using Kendo if we have to massage things so much.

[I wrestled with Kendo custom validation for ages. In the end I decided to write my own version. It was a breeze and I have total control over it.]

Anyway, back to the question ;). For the moment I have this easy solution. In save(), after validation, I just do this before posting:

          practiceGroupID: e.model.practiceGroupID ? e.model.practiceGroupID : null,
          countryCode: e.model.countryCode ? e.model.countryCode : null,
          officeID: e.model.officeID ? e.model.officeID : null,

Ignoring Kendo specifics, though, from a design/modelling perspective, where does this coercion belong and why?
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7442
  • View blog
  • Posts: 15,438
  • Joined: 16-October 07

Re: at what level to switch 0 to null

Posted 08 March 2019 - 05:56 AM

View Postandrewsw, on 08 March 2019 - 05:39 AM, said:

from a design/modelling perspective, where does this coercion belong and why?

The answer to this is almost certainly: it depends. :P

Starting at the datasource: if you have a result set that involves a lot of joins, then consider handing off that job to the service that does that best, like an RDBMS. Or the server ( in my case C# ), where an api should be doing checks and validation, anyway. Indeed, if your api is doing it's job, then transforming data to and from the consumer makes sense.

In the JS app: If you're here, then the api is behaving the way you want, or maybe can't be changed, so the app has to deal.

I usually have an abstraction called "service", which reads from and writes to the web api. If you can do transformations here, then the rest of the app needn't care about it, so this is preferred. However, in practices, you'll often have things like numeric or date types that you want to be treated properly here but other parts of the application must deal with differently. This service is a Data Abstraction Layer (DAL), not a controller. Controllers talk to this. (Hmm... look like MS pushed the DAL thing: https://docs.microso...127(v=pandp.10) .)

So, now you're in a controller. This will be reading data from your source and writing data to your source. Here is where I think special cases should happen. My view layer, Angular, Kendo, etc, shouldn't have to worry about such special cases. They should get the data they want and request changes to that data.

So, in my app, I currently have something that looks like:
interface Customer {
    customerId: number;
    name: string;
    contractExpiration: Date;
    ...
}



Now, my view might suck at dates or just format them badly. No worries, I have a controller. So, in my controller I might do something like:
interface CustomerView extends Customer {
    contractExpirationView: string;
    ...
}

// in the controller, sent to the view
customers.map(toCustomerView)

// in the controller, to handle updates
// call to service
const onCustUpdate = (x:Customer) => ... do stuff

// call from view
// take view props and push them back into actual model, if needed
const onCustomerViewUpdate = (x:CustomerView) => onCustUpdate(toCustomer(x));



Again, there are really no definitive answers to this. It depends on the sum total of all architectural choices made. I'd be really interested if someone else were to offer something completely different.

Why this answer? I tend to favor the principle of least knowledge. The more I can protect all the moving parts from themselves, the better. A view receives state and only knows how to notify a controller of changed state. A controller keeps knowledge of the model from the view, so model manipulations belong there. A model might shield the controller from data details, so if stuff can be kept up there, even better.

Burrowing down, each of my view components should know as little as possible. My drop down needn't know the particulars of the data: it need only know a display property, selected item, and how to notify someone when the selection has changed. Such a loosely coupled component is reusable. If it knows more about the data than that, then it's been tightly coupled for a specific task and isn't very reusable.
Was This Post Helpful? 1
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,095
  • Joined: 12-December 12

Re: at what level to switch 0 to null

Posted 08 March 2019 - 06:30 AM

Thank you for your views (pun intended) ;)

Something occurred to me regarding this Kendo issue, rather than the broader question, probably prompted by reading your post.

I used the word coercion above. The issue I have is nothing to do with validation or a business rule. It is simply a flaw (one of many) with the Kendo library. It cannot cope with nulls and is giving me 0 rather than null in response to a user's activity, or lack of activity.

Because Kendo's API yields a save event, this gives me the opportunity to correct the issue with the library. Here I perform some client-side validation and correct 0's to the nulls that the service > API > database layers expect. It is also isolated (I have convinced myself) because it is all within the Kendo configuration and events. [I suppose I could isolate it even further by creating my own kendo.component.ts but I ain't going to go that far.]




I also remain interested in people's opinions on the broader question(s).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1