Entity base classes in separate assembly?

5 posts, 0 answers
  1. Joseph Lam
    Joseph Lam avatar
    10 posts
    Member since:
    Jan 2010

    Posted 24 Jul 2014 Link to this post

    I'm evaluating Telerik for a project where I need to have a 'common' project that contains various base class and value types which will be referenced by several applications each with its own entity model.

    For example, in the common project I have an abstract EntityBase class that contains ID and RowVersion properties. And all entities in each application's domain model will inherit this.

    Is that supported and how?

    Joseph
  2. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 29 Jul 2014 Link to this post

    Hi Joseph,

    Telerik Data Access supports different type of inheritance, for which you can find more information here. The only limitation is that if the base class is mapped to a table then all of the derived classes mapped to tables in the same database should be in one project, if the base class is not mapped to a table, there is no limitation in which project is the class. In brief the entire mapping for one database should be in one project.

    If you have any further questions, do not hesitate to contact us again.

    Regards,
    Boris Georgiev
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  3. DevCraft banner
  4. Adam
    Adam avatar
    3 posts
    Member since:
    Jul 2014

    Posted 29 Jul 2014 in reply to Boris Georgiev Link to this post

    Hi Boris -

    I'm also evaluating Data Access and have a similar requirement as Joseph. Would it be possible to split the base / derived classes including the mappings between projects and then aggregate using the AggregateMetaDataSource?

    http://docs.telerik.com/data-access/developers-guide/code-only-mapping/merging-metadatasources/fluent-mapping-merging-models-aggregate-metadata-source
  5. Adam
    Adam avatar
    3 posts
    Member since:
    Jul 2014

    Posted 29 Jul 2014 in reply to Boris Georgiev Link to this post

    Hmm.. apparently not? I made an attempt at this scenario, but got the following error:

    Base class 'Domain.Core.Order' was enhanced into 'Domain.Custom.Order', but not used during relational metadata creation.

    I followed the vertical inheritance guide and used the aggregate metadata source to split the base / derived order classes between projects.
    http://docs.telerik.com/data-access/developers-guide/code-only-mapping/inheritance/fluent-mapping-inheritance-vertical
    http://docs.telerik.com/data-access/developers-guide/code-only-mapping/merging-metadatasources/fluent-mapping-merging-models-aggregate-metadata-source

    Here's my setup:

    Domain.Core

    public class Order
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Amount { get; set; }
        public virtual DateTime OrderDate { get; set; }
    }

    public class OrderConfiguration : MappingConfiguration<Order>
    {
        public OrderConfiguration()
        {
            MapType(o => new
            {
                OrderId = o.Id,
                Name = o.Name,
                Amount = o.Amount,
                OrderDate = o.OrderDate
            }).ToTable("Order");

            HasProperty(o => o.Id).IsIdentity(KeyGenerator.Autoinc);
        }
    }

    Domain.Custom

    public class Order : Core.Order
    {
        public string GiftMessage { get; set; }
    }

    public class OrderConfiguration : MappingConfiguration<Order>
    {
        public OrderConfiguration()
        {
            MapType(o => new
                {
                    GiftMessage = o.GiftMessage
                })
                .Inheritance(Telerik.OpenAccess.InheritanceStrategy.Vertical)
                .ToTable("CustomOrder");
        }
    }

    For the MetadataContainer, I used:
    MetadataContainer metadataContainer = new AggregateMetadataSource(new CoreMetadataSource(), new CustomMetadataSource(), AggregationOptions.Late).GetModel();

    (the CoreMetadataSource and CustomMetadataSource return/prepare the respective order MappingConfigurations)
  6. Adam
    Adam avatar
    3 posts
    Member since:
    Jul 2014

    Posted 30 Jul 2014 in reply to Adam Link to this post

    For those interested... After a bit more digging, I was able to get this scenario working by utilizing the late reference resolution option for the Custom mappings.

    Changing the metadataContainer to:
    MetadataContainer metadataContainer = new AggregateMetadataSource(new CoreMetadataSource(), new CustomMetadataSource(AggregationOptions.Late)).GetModel();
    did the trick.

    For reference, here's the CustomMetadataSource code:
    public class CustomMetadataSource : FluentMetadataSource
    {
        public CustomMetadataSource()
        {
        }
     
        public CustomMetadataSource(AggregationOptions aggregationOptions) : base(aggregationOptions)
        {
        }
     
        protected override IList<MappingConfiguration> PrepareMapping()
        {
            var configurations = new List<MappingConfiguration>();
     
            var configs = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(MappingConfiguration)));
            foreach (var type in configs)
            {
                var map = Activator.CreateInstance(type) as MappingConfiguration;
                configurations.Add(map);
            }
     
            return configurations;
        }
    }
Back to Top
DevCraft banner