简体   繁体   中英

asp.net class library that talks to ef and customizable

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 SomeName Manager - [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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM