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

7 posts, 1 answers
  1. Skywalker
    Skywalker avatar
    67 posts
    Member since:
    Sep 2007

    Posted 21 Sep 2009 Link to this post

    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
  2. Skywalker
    Skywalker avatar
    67 posts
    Member since:
    Sep 2007

    Posted 21 Sep 2009 Link to this post

    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
  3. DevCraft banner
  4. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 23 Sep 2009 Link to this post

    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.
  5. Skywalker
    Skywalker avatar
    67 posts
    Member since:
    Sep 2007

    Posted 24 Sep 2009 Link to this post

    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
  6. Skywalker
    Skywalker avatar
    67 posts
    Member since:
    Sep 2007

    Posted 07 Oct 2009 Link to this post

    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.
  7. Answer
    Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 08 Oct 2009 Link to this post

    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.
  8. Skywalker
    Skywalker avatar
    67 posts
    Member since:
    Sep 2007

    Posted 08 Oct 2009 Link to this post

    Hi Alexander,

    That workaround works around the issue, thank you!

    Kind regards,
    Sipke
Back to Top
DevCraft banner