-1

I'm trying to make a class library that contains only the core logic of may app. I've seen many tutorials that do this but they include the EF core inside that library. What I want to do is put only all the logic such, adding a category if it doesn't exist yet by passing a string.

Here 's what I want to do

  1. Create a class library in separated project.
  2. Add a class called SomeNameManager - [contains all the methods I want]
  3. Create models to be modified by this manager but I don't want this to be the class directly used as my entity. I just want this to be the base class of my entity for customization. for example if I have to add a new propery, I'd just change the entity in my main app. not in the library.
  4. The DbContext is in my main app only. Which means all of my classes and lists that used in my library will be just in the memory.

Here's what I got so far

// class library
public interface IBook{
    // some properties here
    ICollection<ICategory> Categories { get; set; }
    // some more properties...
}
public interface ICategory{
    // some properties here
    ICollection<IBook> Books { get; set; }
    // some more properties...
}

public class Book : IBook {
    // implementations...
}
public class Category : ICategory {
    // implementations
}

public class BookManager {
    public void CreateBook(Book book) {
        // some logic
        // I'm not sure if I would pass Book or IBook
    }
    public void AddCategories(List<Category> categories) {
        // some logic
        // I'm not sure if I would pass Category or ICategory
    }
}

// my main app
public class BookInfo {
    // some props...
}
public class BookCategory {
    // some props...
}

public class MyDB : DbContext{
    public DbSet<BookInfo> BookInformations {get; set;}
    public DbSet<BookCategory> BookCategories {get; set;}
}

The problems

  1. The table name in my database is Book. I don't want to use fluent API just to rename this.
  2. I have new column it both database called Discriminator. What is that? I do I remove that?
  3. Categories isn't binding to Book.Categories. It's empty but it inserts to the database.

Additional Question Am I doing this right? Is it a bad idea? Please enlighten me. I'm not beginner but I haven't been in the real development team.

  • have you looked at Usermanger in identity implementation. Identity has something exactly like your scenario. – Zeeshan Aug 16 '18 at 08:10
  • For question 3, I have to set the base class child property as virtual – veejay grateja Aug 16 '18 at 08:15
  • It's fairly working as that but what is that discriminator column? And what do I do with the table names? – veejay grateja Aug 16 '18 at 08:17
  • When you do inheritance there is mainly three ways to represent this in the database structure. As per default EF takes each of the concrete classes in your class hierarchy and map them to ONE big table (TPH). The discriminator is used to describe the specific type of a row in you database table. For instance if you have two types of books in your hierachy like biography and fiction the discriminator will say "fiction" if a row in the table is of that type and "biography" otherwise – i regular Aug 16 '18 at 08:39

1 Answers1

1

1 .The table name in my database is Book. I don't want to use fluent API just to rename this.

To give a class a different table name you can do this with the fluent API but since you don't want to do that you can use the [Table] attribute for this:

[Table("BookTableName")]
public class Book : IBook {
    // implementations...
}

2. I have new column it both database called Discriminator. What is that? I do I remove that?

A discriminator column is used to identify which type each row represents. See the docs

Mike Bovenlander
  • 5,236
  • 5
  • 28
  • 47