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

Entity base classes in separate assembly?

4 Answers 49 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Joseph Lam
Top achievements
Rank 1
Joseph Lam asked on 25 Jul 2014, 04:16 AM
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

4 Answers, 1 is accepted

Sort by
0
Boris Georgiev
Telerik team
answered on 29 Jul 2014, 07:06 PM
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.
 
0
Adam
Top achievements
Rank 1
answered on 29 Jul 2014, 09:51 PM
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
0
Adam
Top achievements
Rank 1
answered on 29 Jul 2014, 10:11 PM
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)
0
Adam
Top achievements
Rank 1
answered on 30 Jul 2014, 04:13 PM
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;
    }
}
Tags
Getting Started
Asked by
Joseph Lam
Top achievements
Rank 1
Answers by
Boris Georgiev
Telerik team
Adam
Top achievements
Rank 1
Share this question
or