HELP - with X# syntax - Relay Command ...

Public forum to share code snippets, screen shorts, experiences, etc.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP - with X# syntax - Relay Command ...

Post by Phil Hepburn »

Hi Wolfgang,

I totally understand what you are saying, and its a good solution for you and your customers.

However, there is a need to open up the world of .NET coding to others not quite as experienced and knowledgeable as yourself. And that is part of my role in the community, both local and international.

I for one, am not good enough to write my own Frameworks, and also we need to realise that Frameworks are just another way of extending the Language - in the good old days we would keep getting language updates, with the new functionality built in. And any keen programmer wanted the latest version (XYZ... ) of the language.

Also (before I leave) we need to remember what Franz DeWitt said in Stuttgart a few conferences ago, about us guys NOT re-inventing the wheel, and that we should go and look for what is already out there, tested and tried.

After all, we don't sit about worrying over all the parts of the .NET Framework that we are not currently using! So what's the difference?

Must go, wife calling ;-0)
Cheers,
Phil.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP - with X# syntax - Relay Command ...

Post by Phil Hepburn »

Hi guys,

Here is something I promised you I would post, a few days ago now.

We will examine the more standard type/style of coding behind the 'RelayCommand' response of the "Add New Line" (ANL) button.

Here is the XAML script for this ANL button :-
Pearls_EF6_82.jpg
Pearls_EF6_82.jpg (65.38 KiB) Viewed 533 times
And here is the initialisation for the RC (relay command) :-
Pearls_EF6_83.jpg
Pearls_EF6_83.jpg (60.63 KiB) Viewed 533 times
And finally, we get to the sort of code we all have grown up with, check this out :-
Pearls_EF6_81.jpg
Pearls_EF6_81.jpg (168.99 KiB) Viewed 533 times
Yes, when we click the button for adding a new 'OrderLine' object, we need to do some basic checks of a non-LINQ nature (there's a change).

First of all on line 273 we check for a reasonable 'quantity' - in my case not zero and not double figures, so '11' and '33' can't be used - typo or key bounce.

Line 280 checks to see that we have a genuine Product ID - or a product not selected when we pressed the button.

And then on line 287/288 we check to see if the product has already been selected - in a previous line addition. Probably a more likely 'needed' cross-check.

Okay, checks not perfect, but do some of the basic sort of cross-checking we may need or expect. Then we simply go ahead and add the new line to the growing list for the new order on line 297. Note we first calculate the line price from quantity and product price.

Finally, line 304 updates the running total (or sum) for the value of the order. All the order lines summed up.

This was the process you may have seen in the Video clip Robert made available as an attached file in another (and related) post.

Now then guys, back to my 'sick' office PC. I have issues which I may post you about ;-0((

Regards, and enjoy the rest of your weekend,
Phil.
Wales, UK.

P.S. by the way, the code almost handles OK the case where we delete an entry from the data grid - at the moment all is well except and auto update of the order running total - hence the button to force this. Work in progress !!
Frank Maraite
Posts: 178
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

HELP - with X# syntax - Relay Command ...

Post by Frank Maraite »

Hi Phil,

thanks for sharing. I learned a lot. Yes, my comment on RelayCommand last week was totally wrong. Don't no why, I didn't read carefully enough. So please forgive me.

Do you know this article:
https://msdn.microsoft.com/en-us/magazine/dn237302.aspx
?
He is showing us to use RelayCommand with every event: EventToCommand.

Frank
Frank Maraite
Posts: 178
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

HELP - with X# syntax - Relay Command ...

Post by Frank Maraite »

Hi Phil,

thanks for sharing. I learned a lot. Yes, my comment on RelayCommand last week was totally wrong. Don't no why, I didn't read carefully enough. So please forgive me.

Do you know this article:
https://msdn.microsoft.com/en-us/magazine/dn237302.aspx
?
He is showing us to use RelayCommand with every event: EventToCommand.

Frank
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

HELP - with X# syntax - Relay Command ...

Post by NickFriend »

Hi Phil,

Good stuff, but I'm surprised at 287-292 from a dedicated LINQ man such as yourself!

(In C# in case I do the X# syntax wrong...)

Code: Select all

if (aOrderLines.Where(o => o.oProduct.ProductId == oSelectedProduct.ProductId).FirstOrDefault() != null)
......
;-)

Nick
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP - with X# syntax - Relay Command ...

Post by Phil Hepburn »

Hi Frank,

Thanks for this, I could do with including more events, not just clicks on Buttons ;-0)

I will look at this in the next couple of days, as I would like to somehow respond to rows being deleted from the data grid of new OrderLines. So that I can have the Order value total updated.

Fingers crossed,
Phil.

P.S. note the article you pointed at was by Laurent - the guy who is GalaSoft, and who created MVVMlight.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP - with X# syntax - Relay Command ...

Post by Phil Hepburn »

Nice one Nick,

At least someone is still awake at the back of the class ;-0)

Tomorrow I will steal your LINQ code and insert it into my X# eNotes for Cologne.

I will add your name so that I can blame you if anything related to it was to go wrong.

So me trying to show others some sort of code snippet that wasn't LINQ related, failed. You are now therefore suggesting that everything can, and should be done, in a LINQ fashion.

I am sure you are right.

Speak soo,
and regards,
Phil.
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

HELP - with X# syntax - Relay Command ...

Post by NickFriend »

Hi Phil,

... well the only thing is, as you know wherever you see a foreach, Linq will normally do it neater.

Nick
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP - with X# syntax - Relay Command ...

Post by Phil Hepburn »

Hi Nick,

Well, I have followed through your advice and have now got working some code as shown in the following image :-
PearlsNickLINQ_02.jpg
PearlsNickLINQ_02.jpg (44.13 KiB) Viewed 534 times
I have separated the query line to be formatted similarly to the rest of my LINQ code. Just for easy comparison.

The next image shows it at work, when I try to duplicate a previously entered product :-
PearlsNickLINQ_01.jpg
PearlsNickLINQ_01.jpg (49.32 KiB) Viewed 534 times
Although seemingly simple, this code introduces an important topic, that of when we can (or more likely can't) mix .NET objects with other query code.

By this I mean that when we write a query for use with a back-end SQL data engine, the code needs to be easily translated into standard T-SQL script. We can't use a .NET object which is not then known to the SQL Server. We need to be careful.

I will try to reproduce such an occurrence and post it later after this post. Just as a learning 'point'.

This is really the only difference between LINQ to Objects and 'L2S' and 'L2E' [Linq to SQL and Linq to Entity], when the SQL data engine needs to be considered.

So to summarise, the mental 'trigger' for me, will be if I try to reach for code involving FOREACH, then there must be a LINQ/Extension Method way of coding it.

Thanks for the advice and help,
Phil.
Wales, UK.
Frank Maraite
Posts: 178
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

HELP - with X# syntax - Relay Command ...

Post by Frank Maraite »

Hi Nick, Phil,

beside of different syntax is there any technical difference behind the scene? Meaning: is the compiled result different? Are there performance differences?

The FORAECH way seems very straightforeward for me. The readability depends of the time of use. I would like to see reasons other than 'personal preference'. I see MS did many extension methods to collection to make them work together with LINQ. This makes it similar to use with either SQL or Collections. Is this the main reason?

Frank
Post Reply