CodeSmith Community
Your Code. Your Way. Faster!

one-to-many relationship

Latest post 08-26-2005 4:31 PM by Alex. 12 replies.
  • 08-01-2005 4:58 AM

    one-to-many relationship

    Hello
     
    There is something i don't understand in the generated code about the 1-to-many relationship.
    Maybe it's due to a lack of knowledge in this kind of tools, so I hope you could help me.
     
    I have a table A with a primary key a_id, so A(a_id), and a table B with a primary key b_id and a foreign key a_id, so B( b_id, a_id ).
    I think this is the good way to say "A owns several B but each B is owned by only one A".

    So, the first time I looked into the generated code, I thought I would find something as :

    class A { BCollection bs; }
     
    class B { A a; }
     
    I found the A class I expected, but the B class was different :
     
    class B { Guid a_id; }
     
    So, if I want to define a method which required information about B and A, but mainly about B, I would like be able to have only one parameter, which would be B, and get the matching A directly from B.
    But in this case, i need to define two arguments, A and B.
     
    An other sample case : If i retrieve an instance of B from the database, when i need to have the A instance which maps to this B instance, i can't store the instance of A in the B class, so i need to store the B class in the Bcollection of the A class.
    What I would like is to create the link which are in the database in the class structure, so from the A class I would be able to get the B class and from the B class I would be able to get the A class.
    Actually, only one of this two link is here.
     
    So, could explain me the reason of a such class structure when there is a such 1-to-many relationship between two tables please ?
    Why the 'many' part of the relationship is generated but not the '1' part ?
     
    Thanks in advance.
     
    Regards,
     
    Chris
    • Post Points: 305
  • 08-04-2005 12:44 PM In reply to

    RE: one-to-many relationship

    hi

    I really would like some feedback on this subject.
    I'm not able to solve this issue.

    regards,
    Chris
    • Post Points: 5
  • 08-25-2005 8:55 PM In reply to

    • adamh
    • Not Ranked
    • Joined on 06-11-2005
    • Posts 7
    • Points 120

    RE: one-to-many relationship

    Hello guys!

    Well, this fella is right!
    You really need both ways: when you have a Object with only _1_ status (1:N), then you need access to that status /Object.Status.StatusName/ (currently unavailable in .NetTiers), but rarely you need all Objects from a status /Status.Objects[0]/ (rite now available in .NetTiers).
    Why is is this way??

    Thanks,

    Adam

    ps: as far as I remember, the former versions were OK from this perspective.

    Post Edited (AdamH) : 8/25/2005 9:08:15 PM GMT

    • Post Points: 5
  • 08-26-2005 4:58 AM In reply to

    • Alex
    • Top 10 Contributor
    • Joined on 07-26-2005
    • Australia, Canberra
    • Posts 526
    • Points 10,645

    RE: one-to-many relationship

    Agree, entities instead foreign keys properties would be very nice!
    I was trying to find out where in templates collection property is created?
    Best regards,
    Alex.
    • Post Points: 5
  • 08-26-2005 7:29 AM In reply to

    • Alex
    • Top 10 Contributor
    • Joined on 07-26-2005
    • Australia, Canberra
    • Posts 526
    • Points 10,645

    RE: one-to-many relationship

    I've found that EntityBase.cst already handle all relationship cases:


    <script runat="template">
    ///<summary>
    /// Many To Many Relationship Class Template Property String
    ///</summary>
    protected static string MANY_TO_MANY = @"
    private {1} _{0} = new {1}();

    /// <summary>
    ///    Holds a collection of {1} objects
    ///    which are related to this object through the junction table {2}
    /// </summary>    
    public {1} {0}
    {{
        get {{ return _{0}; }}
        set {{ _{0} = value; }}    
    }}
    ";

    ///<summary>
    /// One to One Relationship Class Template Property String
    ///</summary>        
    protected static string ONE_TO_ONE = @"
    private {0} _{0} = new {0}();
        
    /// <summary>
    ///    Holds a {0} object
    ///    which is related to this object through the relation {1}
    /// </summary>
    public {0} {0}
    {{
        get {{ return _{0}; }}
        set {{ _{0} = value; }}    
    }}
    ";
            
    ///<summary>
    /// One To Many Relationship Class Template Property String
    ///</summary>        
    protected static string ONE_TO_MANY = @"
    private {0} _{0} = new {0}();
        
    /// <summary>
    ///    Holds a collection of {2} objects
    ///    which are related to this object through the relation {1}
    /// </summary>    
    public {0} {0}
    {{
        get {{ return _{0}; }}
        set {{ _{0} = value; }}    
    }}
    ";
            
    ///<summary>
    ///   Writes out the property string of all the relationship object collections.
    ///</summary>
    private void WriteRelationshipPropertyString() {
            
            ///Loop Child Collections
            foreach (CollectionInfo item in GetChildrenCollections(SourceTable, SourceTables))
            {
                
                ///Assign by RelationshipType, Then Write to Response
                switch((int)item.CollectionRelationshipType)
                {
                    ///One To One Relationship
                    case (int)RelationshipType.OneToOne:
                        object[] oneToOneParams = {
                                    GetClassName(item.SecondaryTable), 
                                    GetClassName(item.PkIdxName)
                        };
                        item.PropertyString = string.Format(ONE_TO_ONE, oneToOneParams);
                        break;
                        
                    ///One To Many, Many To One
                    case (int)RelationshipType.OneToMany:
                    case (int)RelationshipType.ManyToOne:
                        object[] oneToManyParams = {
                                GetCollectionClassName(item.SecondaryTable)
                                , item.PkIdxName
                                , GetClassName(item.SecondaryTable)
                        };
                    item.PropertyString = string.Format(ONE_TO_MANY, oneToManyParams);
                    break;
                    
                    ///Many To Many
                    case (int)RelationshipType.ManyToMany:
                        object[] manyToManyParams = {
                                //GetCollectionClassName(item.CleanName) + "_From_" + GetClassName(item.JunctionTable), 
                                item.CollectionName,
                                GetCollectionClassName(item.SecondaryTable),
                                item.JunctionTable
                        };
                        item.PropertyString = string.Format(MANY_TO_MANY, manyToManyParams);
                        break;
                    default:
                        break;
                }
                
                if (!RenderedChildren.Contains(item.CleanName))
                {
                    ///Write it out to the resonse stream
                    Response.Write(item.PropertyString);    
                    RenderedChildren.Add(item.CleanName);
                }
            }///End foreach loop
            
            ///enable use for the next template
            RenderedChildren.Clear();
            }    


    </script>


    So, this is right place?
    As we can see ONE-TO-MANY case is already handled but only from one side...
    How we could add support for another side of relationship?
    Best regards,
    Alex.
    • Post Points: 5
  • 08-26-2005 7:57 AM In reply to

    • dSquared
    • Top 10 Contributor
    • Joined on 09-01-2004
    • Paris - France
    • Posts 519
    • Points 260,935

    RE: one-to-many relationship

    actually this is much complex. imagine two entities: Category and product, where one category holds many Products, today you have:

    Category.ProductCollection

    and you want also: Product.ParentCategory, right ?

    but the instance "ParentCategory" will also have a child property ProductCollection (which will also contains the same product), and each of this product will have a property ParentCategory, etc. you see the recursive problem ?

    it will be a major problem, mostly when using DeepLoad methods


    actually, this has been discuted for a while on this forum, but we did not reach to an elegant solution, that's why things have not progressed on this feature :-S

    John Roland
    ------------------------------
     Member of the .netTiers team
      http://en.serialcoder.net
    ------------------------------

    • Post Points: 5
  • 08-26-2005 8:28 AM In reply to

    • Alex
    • Top 10 Contributor
    • Joined on 07-26-2005
    • Australia, Canberra
    • Posts 526
    • Points 10,645

    RE: one-to-many relationship

    Do you use DeepLoad methods?
    Actually there are not much info in manual about Deep Load and Save.
    Several examples of proper use would be great.

    Maybe it is better to have Refresh method for Collection which will load collection this will eluminate problems with recursion?
    Best regards,
    Alex.
    • Post Points: 5
  • 08-26-2005 1:48 PM In reply to

    RE: one-to-many relationship

    Here's a Deep Load/Save document that I wrote A LONG Time ago.  This will clearly show how to use Deep Load/Save, and I tried to quickly update it to reflect the current Beta Data Access Methods.  But there could still be issues in there, as I did not actually run the code, so just try and use it as a guide.  If I get time, I'll spend more time with it.

    Robert
     

    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 5
  • 08-26-2005 1:59 PM In reply to

    • vulgrin
    • Top 50 Contributor
    • Joined on 04-05-2005
    • Posts 100
    • Points 1,770

    RE: one-to-many relationship

    Anyone here like technical writing and want to pull together a comprehensive set of docs? :) I know there's a bunch of stuff in CVS, but its all over the map from what I can tell, and some of it is quite old.

    Don't suppose there is a way to comment the templates to generate XML / CHM?

    Or, maybe someone should generate a reference DB (or use NorthWind) then generate code off of that, then continue to augment the code comments via the templates so that in the end we end up with a CHM about NorthWind as created via NetTiers. Would be a good way to talk about NetTiers too, as then we could all just reference the reference DB.
    -------------------------------------
    Member of the .NetTiers team
    http://www.nettiers.com
    Wiki! http://www.openinnovationsoftware.com/nettierswiki
    -------------------------------------
    • Post Points: 5
  • 08-26-2005 2:44 PM In reply to

    RE: one-to-many relationship

    Nice Idea Vulgrin! We could start out with the DBDocumenter Templates, and work from there to generate a help file that has all the entities. Definately something to consider. I may have time in about a month, because that's when i'll have to turn in my docs to my client. ;-)

    Robert

    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 5
  • 08-26-2005 3:00 PM In reply to

    • Alex
    • Top 10 Contributor
    • Joined on 07-26-2005
    • Australia, Canberra
    • Posts 526
    • Points 10,645

    RE: one-to-many relationship

    The problem that current version of generated Entities assembly could not be compiled.
    I have not managed to do that. DAL and unit tests compiles fine...
    Please could anybody try to run and confirm this:


    nant documentation


    I could not figure out why... :-/
    Best regards,
    Alex.
    • Post Points: 5
  • 08-26-2005 3:41 PM In reply to

    • vulgrin
    • Top 50 Contributor
    • Joined on 04-05-2005
    • Posts 100
    • Points 1,770

    RE: one-to-many relationship

    Yeah, breaks for me too. No clue why. Will have to look at it later. As far as I can tell the XML coming out iok...

    Not a good sign though that the only google hit for the error message points back to your other post about the issue... hmmm.
    -------------------------------------
    Member of the .NetTiers team
    http://www.nettiers.com
    Wiki! http://www.openinnovationsoftware.com/nettierswiki
    -------------------------------------
    • Post Points: 5
  • 08-26-2005 4:31 PM In reply to

    • Alex
    • Top 10 Contributor
    • Joined on 07-26-2005
    • Australia, Canberra
    • Posts 526
    • Points 10,645

    RE: one-to-many relationship

    Yup, very strange, I could not found what is the difference between DAL and Entities assembly...
    Best regards,
    Alex.
    • Post Points: 5
Page 1 of 1 (13 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems