如何强制Odata与我的自定义DTO对象一起使用?

时间:2020-05-19 08:03:36

标签: c# odata

我想将OData与我的DTO对象一起使用,但是我的DTO对象属性的名称与实体不完全相同。

此外,结构也不同-如下所示:

我的实体:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    implementation 'com.facebook.android:facebook-android-sdk:[4,5)'
    implementation 'com.firebaseui:firebase-ui-auth:4.3.1'
    implementation 'com.firebase:firebase-client-android:2.5.2'
}

我的DTO:

public abstract class BaseEntity<TKey>
{
    public virtual TKey Id { get; set; }
}

public class ContentTranslation : BaseEntity<int>
{
    public int ContentId { get; set; }
    public Content Content { get; set; }
    public string Text { get; set; }
    public Language Language { get; set; }
}

public class Content : BaseEntity<int>
{
    public ContentType ContentType { get; set; }
    public ICollection<ContentTranslation> Translations { get; set; }
    public State State { get; set; }
    public FAQ Faq { get; set; }
    public Announcement Announcement { get; set; }
}

public class FAQ : BaseEntity<int>
{
    public string Tag { get; set; }
    public int ContentId { get; set; }
    public Content Content { get; set; }
}

public class Announcement : BaseEntity<int>
{
    public string Header { get; set; }
    public DateTime ValiUntil { get; set; }
    [ForeignKey("Content")]
    public int ContentId { get; set; }
    public Content Content { get; set; }
}

语言,州是枚举

public class AnnouncementModel
{
    public int ContentId { get; set; }
    [JsonConverter(typeof(StringEnumConverter))]
    public State State { get; set; }
    public string Header { get; set; }
    public DateTime ValiUntil { get; set; }
}

public class ContentDto
{
    public int Id { get; set; }
    public JObject Content { get; set; }
    public Dictionary<Language,TranslationDTO> Translations { get; set; }
}

public class FaqModel
{
    public int ContentId { get; set; }
    [JsonConverter(typeof(StringEnumConverter))]
    public State State { get; set; }
    public string MainTag { get; set; }
}

在JObject中,我想放置具体的内容类型(常见问题解答或公告)

所以我的问题是如何创建将我的DTO映射到实体的自定义映射。

实际上,我认为OData使用某种方法将EDMModel转换为实体。 那么我可以将自定义映射放置在EDMModel与实体之间的中间位置吗?

如果是这样,它将与url查询中的所有功能(例如$ select和$ expand)一起使用。 对于每种OData方法,我都可以添加自定义映射器,以使所有OData功能都可以与DTO一起使用。

0 个答案:

没有答案