Print Page | Close Window

Manually-coded Relations between models

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=868
Printed Date: 11-Jun-2026 at 8:58am


Topic: Manually-coded Relations between models
Posted By: vecs
Subject: Manually-coded Relations between models
Date Posted: 30-Jun-2008 at 5:09am
We have a modular application and each application module (assembly) has its own entity model (.orm file + entity classes).
There is one "Sys" module that contains entity classes required by other modules (other modules projects can reference to Sys).
During entities development within my new application module I`d like to define relations between my entities and Sys entities by not redefining Sys entities in my module with Object Mapper. Since Object Mapper doesn't allow relations between models I need to code such relations by myself (if it is possible at all).
For example, I need to define relation between my Customer entity declared in My module and the Address entity declared in Sys module (i.e. add Customer.Location property which is contains link to Address entity). And PersistenceManager should manage this entities properly (build right save graph e.t.c.).

In other words, is it possible to create entity relations dynamically?

P.S.
It seems that my question addresses the similar problem as in this topic:
http://www.ideablade.com/forum/forum_posts.asp?TID=834 - http://www.ideablade.com/forum/forum_posts.asp?TID=834






-------------



Replies:
Posted By: vecs
Date Posted: 03-Jul-2008 at 6:19am
This is a simplifyed version of the previous question.
Here is my first simple situation again.
(I am using IdeaBlade DevForce 3.6.2.2)

1. State.
There are two objects in two models:

Model1.Location entity
  Id (PK, int)
  City
  Street
  House

Model2.Customer
entity
  Id (PK, int)
  CompanyName
  LocationId (FK)

Model1 and Model2 stored in different modules or projects (say M1 and M2).
M2 project have reference to M1 assembly.

2. Task.
Link Customer entity to Location entity by LocationId field in manner of composition (1-1). It should be possible to easy get value of Customer` location property and make UI binding to it.

3. My first solution.

3.1 Add new Location property to Customer entity class:

        [RelationProperty("Customer_Location", QueryDirection.ParentQuery)]
        [BeforeSetValue]
        [AfterSetValue]
        public Model.Location Location
        {
            get
            {
                Model.Location result_;
                if (GetInterceptor<Location>("Location", GetLocationImpl, out result_)) return result_;
                return GetLocationImpl();
            }
            set
            {
                if (BeforeSetValue("Location", value))
                {
                    if (!SetInterceptor<Location>("Location", value, SetLocationImpl))
                    {
                        SetLocationImpl(value);
                    }
                    AfterSetValue("Location");
                }
            }
        }

        //Here is the main logic of subj.
        private Model.Location GetLocationImpl()
        {
            //Create dynamic relation
            EntityRelation relation = new EntityRelation(
                        new EntityColumn[] { Model.Location.IdEntityColumn },
                        new EntityColumn[] { Customer.LocationIdEntityColumn },
                        false,
                        "Customer_Location");
            Model.Location entity = GetParent<Location>(relation, this.PersistenceManager.DefaultQueryStrategy);
            if (entity.IsNullEntity)
            {
                //Create new Entity when it is not assigned
                entity = PersistenceManager.CreateEntity<Location>();
                //PersistenceManager.GenerateId(entity, Model.Location.IdEntityColumn);
                entity.AddToManager();
            }
            return entity;
        }


        private void SetLocationImpl(Location value)
        {
            if (value == null)
            {
                SetNull(this.LocationIdColumn);
            }
            else
            {
                SetColumnValue(this.LocationIdColumn, value, value.IdColumn);
            }
            OnPropertyChanged(new PropertyChangedEventArgs("Location"));
        }

3.2 Provide deletion of corresponding Location entity when delete Customer entity.
Customer class:
        public override void Delete()
        {
            Location.Delete();
            base.Delete();
        }

3.4. Provide right PersistenceOrder so Location entity will be inserted into DB before Customer.
  This may be done by filling SaveOptions/DefaultSaveOptions.PersistenceOrder for PersistenceManager.

4. Questions.

4.1. Whether my solution correct?
4.2. Where is the best place to implement automatic Customer.Location entity creation when creating new Customer entity (instead of creating on first call in GetLocationImpl())?
4.3. Is it necessary to implement step 3.4 or PersistenceManager will automatically determine right PersistenceOrder with help of RelationProperty attribute or something else?
4.4. Does new DevForce EF 4.0 have any advantages regarding cross-model relations definition?



-------------


Posted By: vecs
Date Posted: 03-Jul-2008 at 11:04pm
Here is my second situation.

There are two objects in two models:

Model1.Customer entity
  Id (PK, int)
  CompanyName

Model2.CustomerExtention
entity
  Id (PK, int)
  PrferredTransport (string)

    There is no relation between this entities on model level, but in the logic of application they have composit relation (1-1) so objects, related the same Customer should have same PK value.
In my situation both objects for particular customer created as independent entities in the same PersistenceManager.
I want to implement required relation at runtime by providing same temporary PK value for both of this objects.
    If there is way to say PersistenceManager (or BOS) that this objects single temporary PK value should be replaced by the single actual PK value during save operation?

-------------


Posted By: davidklitzke
Date Posted: 05-Jul-2008 at 3:52pm
I think that you are making things hard for yourself by creating two models (especially if you have bidirectional relationships between objects in the two different models).
 
Also I wouldn't create a user-defined column or a try to create dynamic relationship.  Do these things with the Object Mapper rather than trying to do it in code.
 
It is fine to do automatic Customer.Location entity creation when creating new Customer entity.
 
You should get the right PersistenceOrder automatically.
 
I don't know of anything in DevForce EF that would help you that is missing in DevForce Classic. 



Print Page | Close Window