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

Interfaces for artificial tables

2 Answers 49 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
ChrisL
Top achievements
Rank 1
ChrisL asked on 27 Mar 2015, 07:27 AM
Hi,

I'm using DataAccess to generate artificial tables at runtime (see code snippet below). As I understood, artificial types are compiled in memory so artificial types are just "normal" classes. Is it possible to define either a base type or at least an interface for an artifical type? What does the extension method .HasBaseType() exactly do? 

For example my artifical types always have: Id, CreatedAt, ModifiedAt, CreatedBy, ModifiedBy. It would be very helpful if I could cast query results directly into an Interface such as IDataItem. Is there a way to do that?
public interface IDataItem
{
        int Id { get; set; }
        DateTime? CreatedAt { get; set; }
        DateTime? ModifiedAt { get; set; }
        String CreatedBy { get; set; }
        String ModifiedBy { get; set; }
 
}

01.MappingConfiguration mc = new MappingConfiguration("Spyder", "Inventox.Core.Data");
02.mc.FieldNamingRules.CaseMode = CaseChangeModes.Unchanged;
03.mc.MapType().WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed).ToTable("Spyder");
04.mc.HasArtificialIdentity<int>(KeyGenerator.Autoinc).HasFieldName("Id").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Id").IsNotNullable().HasColumnType("int").HasPrecision(0).HasScale(0);
05. mc.HasArtificialStringProperty("Title").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Title").IsNullable().HasColumnType("nvarchar").HasLength(128);
06.mc.HasArtificialDateTimeProperty("CreatedAt").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("CreatedAt").IsNullable().HasColumnType("datetime");
07. mc.HasArtificialStringProperty("CreatedBy").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("CreatedBy").IsNullable().HasColumnType("nvarchar").HasLength(100);
08.mc.HasArtificialDateTimeProperty("ModifiedAt").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ModifiedAt").IsNullable().HasColumnType("datetime");
09.mc.HasArtificialStringProperty("ModifiedBy").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ModifiedBy").IsNullable().HasColumnType("nvarchar").HasLength(100);
10. mappingConfigurations.Add(mc);

Thanks for your feedback!

Chris






2 Answers, 1 is accepted

Sort by
0
Simeon Simeonov
Telerik team
answered on 01 Apr 2015, 02:15 PM
Hello Chris,

You cannot achieve this trough a purely artificial type. What you need is a regular type inherited by an artificial type with artificial properties. I have prepared an example for you, so please find the attached an archive.

In it you will find:
1/ A regular abstract class (DataItem) defining the Id, CreatedAt, CreatedBy, ModifiedAt and ModifiedBy properties.
2/ The ForumFluentModelMetadataSource class which defines the table mappings.
3/ In the MapDataItem method is the mapping for all of the DataItem properties except for the Id. Also you will see that the inheritance strategy is set to Horizontal - this means that for every class a new table will be create. 
3/ In the MapSpyder and MapSpyder2 methods is the mapping for 2 artificial types inheriting from the DataItem class (just to show that multiple inheritance is working)
4/ Every of the artificial classes defines the mapping for the Id column in order to mark it as a primary key of the table.

Also in the TestMain class you will find examples of how to create records in the artificial tables (several different ways, some of which leverage the public fields behind the properties). You will also find example how to cast the Queryable collection of the Spyder items to a Queryable<DataItem> collection.

For further information about topics shown in the sample code you can take a look at these articles:  
I hope you find this information helpful.

Regards,
Simeon Simeonov
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
ChrisL
Top achievements
Rank 1
answered on 01 Apr 2015, 02:34 PM
Thank you for your answer - this was exactly what I wanted to know ;-).

Best regards,
Chris
Tags
Development (API, general questions)
Asked by
ChrisL
Top achievements
Rank 1
Answers by
Simeon Simeonov
Telerik team
ChrisL
Top achievements
Rank 1
Share this question
or