-2

This is a C# Question, using .NET framework built on Asp.NET Boilerplate. Again, to re-emphasis the question being asked is "HOW...", so if an answer that was provided was a url link or a descriptive explanation on how something was supposed to be done, i would very much appreciate it. (Dont answer questions on how to tie shoelace by showing a picture of a tied shoe, nor do you answer "how to fish" by showing a recording of someone fishing...)

Since the question is pretty basic (i don't need to rephrase/repeat the header again), i'll give an example.

If i have a Forum service, and i create a class to load a Thread. Inside that thread class should be some sort of collection, array, list, or even a dbset of Post that is pulled on construct.

[Table("Thread", Schema = "dbo")]
public class ThreadModel
{
    [Key]
    public long Id { get; set; }

    public string Title { get; set; }

    //Idea 1
    //Value should automatically be pulled and cached the moment class connects to database
    public Post[] Posts { get; set; }

    //Idea 2
    //Post has a constructor to return all post that matches a thread id. While new tag keeps the return value constantly refreshed.
    public Post[] Posts { get { return new Post(this.Id) } }

    //Idea 3
    //Not sure how collection is supposed to work. Does it automatically just pull or will i need to make a method to request?
    public virtual ICollection<Post> Posts { get; set; }

    //Example constructor
    //When connected to database key-value pairs that match database labels will automatically get stored in class
    protected ThreadModel()
    {
        //Idea 1-A
        //Should be a value of null or empty if database yields no results
        Posts = new Post(); 
    }

    public ThreadModel(int threadid) : this()
    {
        //Idea 1-A
        Id = threadid;
        //new Post => returns all posts in db
        //Posts default value is all post in db
        Posts = Posts.Select(post => post.threadid == this.id)

        //Idea 3-A
        Posts = Posts.Get(post => post.threadid == this.id)

        //Idea 4
        Posts = new Posts().GetThread(threadid);
    }
}

Side questions

If all entities are created by inheriting Entity then at what point am i exposed to EntityFramework and DbContext?

I love this example here, submitted by a user as they attempt to connect ABP to their database. But their example doesn't show parent/child resources. I'm unable to find the guide they used to create that, and how it relates back to using ABP to fetch EntityFramework's DbContext example

How does this work? I'm unable to find instructions or explanation for this? (What am i to enter into google to get answers on these mechanics?)

[Table("AbpItems")]
public class Item : Entity
{
    [ForeignKey("PostId")]
    public Post Post { get; set; }
    public int PostId { get; set; }
}

How does this integrate into/with abp's EntityFramework?

Where am i supposed to be creating my Database Table/Class? The project follows the Core.csproj, Application.csproj, and EntityFramework.csproj assembly layout. But it seems like every example is creating the classes at different stages or locations of the solution.

user3681384
  • 105
  • 9

1 Answers1

1

use GetAllIncluding. See https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2617

Here's a complete solution ;

namespace EbicogluSoftware.Forum.Threads
{
    [Table("Threads")]
    public class Thread : FullAuditedEntity
    {
        [Required]
        [StringLength(500)]
        public virtual string Title { get; set; }

        [Required]
        [StringLength(2000)]
        public virtual string Text { get; set; }

        public virtual List<Post> Posts { get; set; }

        public Thread()
        {
            Posts = new List<Post>();
        }
    }

    [Table("Posts")]
    public class Post : FullAuditedEntity
    {
        [Required]
        [StringLength(2000)]
        public virtual string Text { get; set; }
    }

    public class ThreadDto
    {
        public string Title { get; set; }

        public string Text { get; set; }

        public List<PostDto> Posts { get; set; }

        public ThreadDto()
        {
            Posts = new List<PostDto>();
        }
    }

    public class PostDto
    {
        public string Text { get; set; }
    }

    public class ThreadAppService : IApplicationService
    {
        private readonly IRepository<Thread> _threadRepository;

        public ThreadAppService(IRepository<Thread> threadRepository)
        {
            _threadRepository = threadRepository;
        }

        public async Task<List<TenantListDto>> GetThreads()
        {
            var threads = await _threadRepository.GetAllIncluding(x => x.Posts).ToListAsync();
            return threads.MapTo<List<TenantListDto>>();
        }
    }
}

Where am i supposed to be creating my Database Table/Class?

You can create them in YourProject.Core.proj

Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55
  • Is it possible that you could add some extra information or resource for me to learn from? I want to be able to replicate this on my own in other projects. But i do not understand the answer you provided. Why is there a `Dto` that looks exactly like the `Entity`? Which class do i fetch from when i write rest of service? Does that mean that i can ignore the `ThreadDomainService`? If other parts of my code are to fetch from the Thread or Post values like `Title` or `(int)Thread.Post.Count`, am i supposed to grab from `ThreadAppService`? I'm really used to direct integration, so i dont understand – user3681384 Oct 05 '18 at 09:12
  • How is Id and ForeignId supposed to work? I still havent received a response to help clarify my lack of understanding, and the link you attached only had 1 sentence replies. No one made an attempt to breakdown and explain their response. Literally exactly like what you did above. I asked a question, and went out of my way to provide an explanation on why i asked, what i was looking for, and how multiple resources contradict each other. You gave me a copy-paste solution, but did not answer the question. – user3681384 Oct 16 '18 at 22:25
  • @user3681384 Where is the contradiction? – aaron Oct 17 '18 at 00:44
  • The contradiction is that ABP is built on top of EntityFramework, but none of the schematics or foundation of EF remain present in ABP. Even if there is a "mapping" tool to translate between different database schemas, the whole "learn EF and you wont have a problem" approach is really a problem. – user3681384 Oct 17 '18 at 01:08
  • There's no contradiction. That's a wrapper for EF `Include`. You can use `GetAll().Include` if you insist. – aaron Oct 17 '18 at 04:39
  • Is there a documentation for that? I really dont understand "how it works" and "how it ties into primary/foreign key (sql management)". I dont mind reading lots of pages and/or articles of information. My issue is that there isnt enough resources to properly clarify what's going on. – user3681384 Oct 17 '18 at 05:11
  • That's EF. How it works is implementation detail, and EF is open source. You can read the entire source code: https://github.com/aspnet/EntityFramework6 – aaron Oct 17 '18 at 12:38
  • Okay, but a wrapper sometimes changes the rules of a framework. Cause as mentioned, i have been learning EF and my concerns are not being addressed. Can you offer me an answer/solution that's geared towards addressing my question? I'm unable to read code with the level of efficiency that you are able to. Without having proper documentation explaining what the code itself is doing, i am unable to fully understand or acknowledge it. In regards to the solution/answer to my post, "how do i entity an entity? (how does the primary/foreign key get addressed when using the `Entity` class)"? – user3681384 Oct 17 '18 at 17:30
  • It's addressed by EF. – aaron Oct 18 '18 at 00:10
  • Again, i'm asking for help, and your response of "figure it out" is very unhelpful... `It's addressed by EF` doesnt tell me anything useful. Not where to look, what to look for, what to do, or how to do it. Why even bother responding if you werent going to offer/provide anything that could benefit the conversation? And i'm not being insultive, or disrespectful, i'm just honestly confused on being led around and not getting anything to benefit the cause. Should i post another SO question repeating the exact same thing we just discussed for a concise response? – user3681384 Oct 18 '18 at 22:30