Entity Framework Core domain and classes

asp.net-core c# entity-framework-core

Question

I'm currently following a project being developed by csharpfritz (of microsoft). It's called, "corewiki". Some form of "wikipedia" like project. Here's the link to the repository on github: CoreWiki by Jeff Fritz On the domain class for comments he writes this:

I'm trying to understand why he created an entity FromDomain class and ToDomain class:

// Main model

public class CommentDAO
{
    public int Id { get; set; }
    public int ArticleId { get; set; }
    public virtual ArticleDAO Article { get; set; }
    public string DisplayName { get; set; }
    public string Email { get; set; }

    [NotMapped]
    public Instant Submitted { get; set; }
    public string Content { get; set; }
}


public static CommentDAO FromDomain(Core.Domain.Comment comment)
{
    return new CommentDAO
    {
        AuthorId = comment.AuthorId,
        Content = comment.Content,
        DisplayName = comment.DisplayName,
        Email = comment.Email,
        Id = comment.Id,
        ArticleId = comment.ArticleId,
        Submitted = comment.Submitted
    };
}

public Core.Domain.Comment ToDomain()
{
    return new Core.Domain.Comment
    {
        AuthorId = AuthorId,
        Content = Content,
        DisplayName = DisplayName,
        Email = Email,
        Id = Id,
        ArticleId = this.Article.Id,
        Submitted = Submitted
    };
}
1
0
11/8/2018 4:37:51 AM

Popular Answer

That's simply mapping code to map a domain model to a data access object and vice versa. You can implement this in many ways, such as the author showed, or using explicit conversion operators, or using a tool like AutoMapper.

See for example Having the domain model separated from the persistence model (first Google hit for "why separate domain model from dao") for an explanation of why you'd want that.

2
11/7/2018 4:16:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow