This is a migrated thread and some comments may be shown as answers.

Best Practice Request: Storing an object with a property of a Custom Type

6 Answers 97 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Skywalker
Top achievements
Rank 1
Skywalker asked on 21 Sep 2009, 11:52 PM
I have a class named User (marked as Persistent).
the User class contains 3 properties: UserId of type int, Name of type string and Address of my custom type Address.
Address contains 4 string properties: Streetname, Housenumber, Zipcode and City.

Now I would like the forward mapping engine to generate a table like:

UserId (int NOT NULL)
Name (nvarchar(100))
Streetname (nvarchar(100))
Housenumber (nvarchar(50))
Zipcode (nvarchar(20))
City (nvarchar(100))

What would be considered 'best practice' to forward map this model to a SQL Server table in this manner?
I saw a thread about Type Conversion here.

Implementing the INotifyPropertyChanged interface, would that be recommended in this case as well?

Thanks,
Sipke

6 Answers, 1 is accepted

Sort by
0
Skywalker
Top achievements
Rank 1
answered on 22 Sep 2009, 01:01 AM
Is it then also possible to use OQL? So that I might query: from u in ObjectScope.Extent<User> where u.Address.City == "Seattle" select u

Thanks,
Sipke
0
Zoran
Telerik team
answered on 23 Sep 2009, 06:24 AM
Hi Sipke,

Mapping two classes to a single table is not supported by default. A workaround for your case would be to declare the Address class as a persistent struct. Fields of structs are added as columns to the table of the persistent class. That way you would reach the desired effect of having the User and Address fields in one table.

Regards,
Zoran
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Skywalker
Top achievements
Rank 1
answered on 24 Sep 2009, 06:24 PM
Hi Zoran,

That sounds like exactly what I need!
So I just need to change the Address class to a persisten struct then.
Makes sense for my domain model, because Address is simply a structure for holding related data together.

I'll give it a try!

Thanks,

Sipke
0
Skywalker
Top achievements
Rank 1
answered on 08 Oct 2009, 02:15 AM

Well, that works as espected, great!

However, now I have something more sophisticated, like this:

class User  
{  
   Profile Profile;  
}  
 
class Profile  
{  
  PersonalInfo PersonalInfo;  
}  
 
struct PersonalInfo PersonalInfo  
{  
  PersonName Name;  
  string FavouriteFood;
}  
 
struct PersonName  
{  
   string Firstname;  
   string Lastname;  

(for brevity, I ommited all FieldAlias attributes etc. I made sure I didn't screw up there)

This works great when saving data into the database and even when retrieving Users.
But when I try querying on the Firstname property of the Name property of the PersonalInfo property of the Profile property of a User, I get a runtime error: 'Field 'Name' not found on class 'Profile''

For example, take this query:

var myUsers = ( from x in scope.Extent<User> where x.Profile.PersonalInfo.Name.Firstname == "john" ).ToList();    
 

It simply throws up, while the next query:

var myUsers = ( from x in scope.Extent<User> where x.Profile.PersonalInfo.FavouriteFood == "pizza" ).ToList();   
 

works without any problems.

My guess is that it has something to do with having nested complex struct  types.
If that's the case, how can I work around this? Transforming the structs into classes is not an option, because I want everything stored in just two tables: Users and Profiles.

You may download the sample project here.

Please advise.
0
Accepted
Alexander
Telerik team
answered on 08 Oct 2009, 02:41 PM
Hi Sipke,

As I answered in the support ticket that you sent to us, the problem is related to resolving the FieldAlias attribute of the Name property. The workaround is to expose the name field as public and use it directly in the Linq query:
from x in scope.Extent<User>
where x.Profile.PersonalInfo.name.Firstname == "john"
select x
While the problem is being fixed, this could be helpful if someone faces the same issue.

Kind regards,
Alexander
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Skywalker
Top achievements
Rank 1
answered on 08 Oct 2009, 07:44 PM
Hi Alexander,

That workaround works around the issue, thank you!

Kind regards,
Sipke
Tags
Development (API, general questions)
Asked by
Skywalker
Top achievements
Rank 1
Answers by
Skywalker
Top achievements
Rank 1
Zoran
Telerik team
Alexander
Telerik team
Share this question
or