Interfaces for artificial tables

3 posts, 0 answers
  1. ChrisL
    ChrisL avatar
    2 posts
    Member since:
    Jan 2004

    Posted 27 Mar 2015 Link to this post


    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"); = CaseChangeModes.Unchanged;"Spyder");<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);"CreatedAt").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("CreatedAt").IsNullable().HasColumnType("datetime");
    07. mc.HasArtificialStringProperty("CreatedBy").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("CreatedBy").IsNullable().HasColumnType("nvarchar").HasLength(100);"ModifiedAt").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ModifiedAt").IsNullable().HasColumnType("datetime");"ModifiedBy").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ModifiedBy").IsNullable().HasColumnType("nvarchar").HasLength(100);
    10. mappingConfigurations.Add(mc);

    Thanks for your feedback!


  2. Simeon Simeonov
    Simeon Simeonov avatar
    24 posts

    Posted 01 Apr 2015 Link to this post

    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.

    Simeon Simeonov
    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. ChrisL
    ChrisL avatar
    2 posts
    Member since:
    Jan 2004

    Posted 01 Apr 2015 in reply to Simeon Simeonov Link to this post

    Thank you for your answer - this was exactly what I wanted to know ;-).

    Best regards,
Back to Top