Managed polymorphic references

2 posts, 0 answers
  1. Joe
    Joe avatar
    1 posts
    Member since:
    Aug 2013

    Posted 23 Aug 2013 Link to this post


    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")

    var copyJobMap = new MappingConfiguration<CopyJob>();

    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();
    ProcessJob processJob = new ProcessJob();
    copyJob.NextJob = processJob;


  2. Boris Georgiev
    Boris Georgiev avatar
    190 posts

    Posted 28 Aug 2013 Link to this post

    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?

    Boris Georgiev
    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.
  3. DevCraft banner
Back to Top