Skip to content

Developing Core Layer

Mehmet Özkaya edited this page Mar 25, 2019 · 1 revision

Developing Core Layer

The first point of implementation is definition of Entity. Because we are choosing code-first approach of Entity Framework Core and this seperate this entities to Core layer in order to write only one place.

public class Product : BaseEntity
    {        
        public string ProductName { get; set; }
        public string QuantityPerUnit { get; set; }
        public decimal? UnitPrice { get; set; }
        public short? UnitsInStock { get; set; }
        public short? UnitsOnOrder { get; set; }
        public short? ReorderLevel { get; set; }
        public bool Discontinued { get; set; }
        public int CategoryId { get; set; }
        public Category Category { get; set; }        
    }

As you can see that, any entity class should inherit from BaseEntity.cs. This class include Id field with int type. If you want to change Id field type, you should change only BaseEntity.cs.

Also Category entity should be created;

 public class Category : BaseEntity
    {
        public Category()
        {
            Products = new HashSet<Product>();
        }

        public string CategoryName { get; set; }
        public string Description { get; set; }        
        public ICollection<Product> Products { get; private set; }        
    }

Product-Category Interfaces

Core layer has Interfaces folder which basically include abstraction of dependencies. So when we create new 2 entity if we need to operate some custom db operation we should create repository classes ;

public interface IProductRepository : IAsyncRepository<Product>
    {
        Task<IEnumerable<Product>> GetProductListAsync();
        Task<IEnumerable<Product>> GetProductByNameAsync(string productName);
        Task<IEnumerable<Product>> GetProductByCategoryAsync(int categoryId);
    }

  public interface ICategoryRepository : IAsyncRepository<Category>
    {
        Task<Category> GetCategoryWithProductsAsync(int categoryId);
    }
Clone this wiki locally