Questions about X# definition

This forum is meant for questions and discussions about the X# language and tools
Anonymous

Questions about X# definition

Post by Anonymous »

X# Experts,

I have created this new topic to post several syntax questions related to the VS 17 and SQL use based on topic: https://www.xsharp.eu/forum/public-chit-chat/288-moved-getting-going-with-sql-server-and-x-from-examplesChit-Chat: Getting going with SQL server and X# - from Examples:

I am still trying to understand the example Phil provided. Searching and reading the X# Help file was somewhat unsuccessful.

Q1: "Namespace" - it is used in each of the .Prg files. I assume it defines all prg's that belong to a soultion spread over muliple Progamm files? If I want to add a seperate prg-file I need to add the namespace in the file as well?

Q2: File DataContext.prg
"public class ForumDataContext inherit DataContext" -> where is DataContext defined. I have tried to search for it but could not find it. The X# Help file it was not mentioned. Is it part of .net and how can I the refernece for it?

Q3: Same file

public class ForumDataContext inherit DataContext
//----- properties
public property ourCustomers as Table<Customer> auto -> could find any help for Table<xxx>


Q4: File TableDataClasses
[Table(Name := "dbo.Customer")]; -> see above, "table" is now in brackets no Table<xxx>

Q5 WPF-File
if file.Exists(cFilePath)... -> Function is clear. Is it a .net function? The X# help file only states "file() as boolean" not the "exists" method.

Thanks,
Michael
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Questions about X# definition

Post by wriedmann »

Hi Michael,
Q1: "Namespace" - it is used in each of the .Prg files. I assume it defines all prg's that belong to a soultion spread over muliple Progamm files? If I want to add a seperate prg-file I need to add the namespace in the file as well?
an namespace is a possibility to group your code. If you have two different classes "Michael" in two different namespaces, they are different.

Normally you use one namespace per assembly, or even per assembly and folder.

Of course, you can work without namespaces or put all your code in the same namespace.

To uses classes from different namespaces, you can prefix them:

Code: Select all

System.Data.Linq.DataContext{}
or use "using"

Code: Select all

using System.Data.Linq

DataContext{}
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Questions about X# definition

Post by wriedmann »

Hi Michael,
Q2: File DataContext.prg
"public class ForumDataContext inherit DataContext" -> where is DataContext defined. I have tried to search for it but could not find it. The X# Help file it was not mentioned. Is it part of .net and how can I the refernece for it?
Dr. Google is your best friend in this case:

https://msdn.microsoft.com/en-us/library/system.data.linq.datacontext(v=vs.110).aspx

Or you can use the brand new search facility:

https://docs.microsoft.com/dotnet/api

Currently, X# has no libraries, so all code you see in Phils samples come from the .NET framework (today I have read somewhere that the .NET Framework has about 14.500 classes).

To use the DataContext class, you need to add a reference to System.Data.Linq.dll to your application, and use a "using" statement or write the full path, including the namespace.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Questions about X# definition

Post by wriedmann »

Hi Michael,

Q3: Same file
public class ForumDataContext inherit DataContext
//
properties
public property ourCustomers as Table<Customer> auto -> could find any help for Table<xxx>
Again, the Table class is defined in System.Data.Linq (but there are other classes with the same name in other namespaces).

The expression Table<Customer> is a generic, and says that the Table contains elements of type "Customer". This gives the compiler the possibility to check the elements if they are the right type - imagine that as a type of string typed array.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Questions about X# definition

Post by wriedmann »

Hi Michael,
Q5 WPF-File
if file.Exists(cFilePath)... -> Function is clear. Is it a .net function? The X# help file only states "file() as boolean" not the "exists" method.
the .NET does not have any functions. "Exists" is a static method of the File class, i.e. a method that refers to the class and not to an specific instance of a class.
In X# functions are mapped to a hidden static class.

Again, this is a method and a class from the .NET framework:

https://docs.microsoft.com/en-us/dotnet/api/system.io.file

Wolfgang

P.S. I have left out Q4 for the moment, as I have to check it better and I'm a bit short of time
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Questions about X# definition

Post by Phil Hepburn »

Q4

This is part of the 'ORM' in L2S. [Object Relational Mapping.] It is a way to map the Table name in SQL to the .NET class, here called 'Customer' - I called them both the same name "Customer" but you don't have to.

Also the semi colon ( ; ) is to continue the attribute onto the next line. It is convention to do this and it makes the code more readable.

For a database in SQL to work with L2S and L2E we must have tables and columns mapped. Our .NET code does this for us.

HTH,
Phil.
beerhand

Questions about X# definition

Post by beerhand »

Thanks Wolfgang and Phil,

this was very helpful.

@Phil, please go ahead with the next steps.

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

Questions about X# definition

Post by Phil Hepburn »

Morning Michael,

Yes, I have been busy on the EF6 demo sample, even though I was silent yesterday.

For visual interest, and mental fun, below is an image of what I have in VS 2017 within my Virtual Machine :-
EF6_houseHoldDemo_01.jpg
EF6_houseHoldDemo_01.jpg (48.56 KiB) Viewed 655 times
We are starting with no SQL database at all - that will be made by our .NET X# code when it is run.

The more complex set of classes will define a 'HouseHold' entity - which is built on a House (we call that a property - as in real estate), as well as a bunch of people, (Persons) who may have a bunch of vehicles. Obviously the house has a complex class property of address.

We as coders will keep our minds on only .NET code. We will define the five classes with there members, and also add some code to define a simple mapping between classes, properties and any table columns that the EF system feels right to create in Tables.

Watch this space ..........

Cheers,
Phil.
Wales, UK.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Questions about X# definition

Post by Phil Hepburn »

Hi Michael,

Oh! - just thought ...........

For the Entity Framework project, I think it a good idea if I start a new thread in Pearls, so do look out for it.

Cheers,
Phil.
Wales, UK.
beerhand

Questions about X# definition

Post by beerhand »

For today I have tried to rebuild Phil's example. Something is terrible going wrong.

Step 1: New X# Project
Step 2: Added Textblock and Datagrid in WPF
Step 3: Created new folder: DataContext
Step 3.1: Created DataContext.prg and used Phil's code
Step 3.2: Created TableDataClasses.prg and used Phil's code
Step 4: Created Connections.prg in folder Properties

If I compile i get the following error message(s) that are linked to missing references? Google was not a real help...Absolutely no clue what to do. :(

Error 1: XS0234 The type or namespace name 'SqlClients' does not exist in the namespace 'System.Data' (are you missing an assembly reference?) WpfApplication3

Error 2: XS0234 The type or namespace name 'Services' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)

Error 3: XS0234 The type or namespace name 'Linq' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)

There are a few more but I think lets fix these first maybe the others go away.

Thanks
Michael
Post Reply