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

Managed polymorphic references

1 Answer 44 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Joe
Top achievements
Rank 1
Joe asked on 23 Aug 2013, 10:48 AM
Hi,

I'm currently trying to persist a linked list of polymorphic objects.I chain those objects by using an interface.

public interface IJob
{
    int Id { get; set; }
    IJob NextJob { get; set; }
}
 
public class CopyJob : IJob
{
    public int Id { get; set; }
    public IJob NextJob { get; set; }
}
 
public class ProcessJob : IJob
{
    public int Id { get; set; }
    public IJob NextJob { get; set; }
}

Two questions have come up:

1) I wrote an extension method for making the possible types for IJob NextJob known to all classes. 
Is there this bad in any way or is there a better way to do this? I wanted to reduce the .WithAvailable() clutter which i otherwise had to maintain multiple times on every single class mapping that implements IJob

public static NavigationPropertyConfiguration<TEntity, TInverse> WithIJobMapping<TEntity, TInverse>(this NavigationPropertyConfiguration<TEntity, TInverse> config)
{
    return config.WithAvailable(typeof(CopyJob), "CopyJob")
        .WithAvailable(typeof(ProcessJob), "ProcessJob")
        .WithDiscriminatingColumn("Type")
        .ToColumn("JobId").IsManaged();
}

var copyJobMap = new MappingConfiguration<CopyJob>();

copyJobMap.MapType().WithConcurencyControl(OptimisticConcurrencyControlStrategy.None);
copyJobMap.HasProperty(x => x.Id).IsIdentity(KeyGenerator.Autoinc);
copyJobMap.HasAssociation(x => x.NextJob).WithIJobMapping();


2) Is it correct that interface references are completely unmanaged and .IsManaged() on the association has no effect?
The only way to persist a linked list is by saving every single item of the list and doing the linking afterwards.
Everything else results in exceptions.

CopyJob copyJob = new CopyJob();
 
_dbContext.Add(copyJob);
_dbContext.SaveChanges();
 
ProcessJob processJob = new ProcessJob();
 
_dbContext.Add(processJob);
_dbContext.SaveChanges();
 
copyJob.NextJob = processJob;
 
_dbContext.SaveChanges();

Regards

Joe

1 Answer, 1 is accepted

Sort by
0
Boris Georgiev
Telerik team
answered on 28 Aug 2013, 10:34 AM
Hi Joe,

Writing an extension method is a good way to encapsulate the code that should be called in several places, as it is in your case.

When you are using the .IsManaged() method for configuring one association you are instructing OpenAccess ORM to track additions and removals of items in the navigation collection properties and to issue the proper statements during SaveChanges(). That is why you can manage only one-to-many and many-to-many associations. When you are using Polymorphic References you cannot specify a navigation collection for the association so setting the association to be managed will not trigger any instructions to OpenAccess ORM.

I hope that helps. In case you want to achieve something different, could you please describe us the specific scenario you want to implement?

Regards,
Boris Georgiev
Telerik
OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
Tags
General Discussions
Asked by
Joe
Top achievements
Rank 1
Answers by
Boris Georgiev
Telerik team
Share this question
or