Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefhanni committed Jan 29, 2024
1 parent 4772938 commit 9682833
Show file tree
Hide file tree
Showing 107 changed files with 44,097 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Day03 Demo Solution.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo.PL", "Demo.PL\Demo.PL.csproj", "{0244FE80-08B4-4EEF-8075-8A935AA5B75C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo.DAL", "Demo.DAL\Demo.DAL.csproj", "{F5CC6B65-7E76-4ACE-BE99-7320C1F1103C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo.BLL", "Demo.BLL\Demo.BLL.csproj", "{B9572953-D43F-4ECE-BD1B-5D75E5FAA82C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0244FE80-08B4-4EEF-8075-8A935AA5B75C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0244FE80-08B4-4EEF-8075-8A935AA5B75C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0244FE80-08B4-4EEF-8075-8A935AA5B75C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0244FE80-08B4-4EEF-8075-8A935AA5B75C}.Release|Any CPU.Build.0 = Release|Any CPU
{F5CC6B65-7E76-4ACE-BE99-7320C1F1103C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F5CC6B65-7E76-4ACE-BE99-7320C1F1103C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5CC6B65-7E76-4ACE-BE99-7320C1F1103C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5CC6B65-7E76-4ACE-BE99-7320C1F1103C}.Release|Any CPU.Build.0 = Release|Any CPU
{B9572953-D43F-4ECE-BD1B-5D75E5FAA82C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9572953-D43F-4ECE-BD1B-5D75E5FAA82C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9572953-D43F-4ECE-BD1B-5D75E5FAA82C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9572953-D43F-4ECE-BD1B-5D75E5FAA82C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1B503294-ED7B-4924-A315-70C5E013F91E}
EndGlobalSection
EndGlobal
11 changes: 11 additions & 0 deletions Demo.BLL/Demo.BLL.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Demo.DAL\Demo.DAL.csproj" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions Demo.BLL/Interfaces/IDepartmentRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Demo.DAL;
using Demo.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.BLL.Interfaces
{
public interface IDepartmentRepository : IGenericRepository<Department>
{

}
}
19 changes: 19 additions & 0 deletions Demo.BLL/Interfaces/IEmployeeRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Demo.DAL;
using Demo.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.BLL.Interfaces
{
public interface IEmployeeRepository : IGenericRepository<Employee>
{

//I use IQueryable not IEnumerable because I need make Filteration
IQueryable<Employee> GetEmployeeByaddress(string address);

IQueryable<Employee> SearchByName(string name);
}
}
21 changes: 21 additions & 0 deletions Demo.BLL/Interfaces/IGenericRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Demo.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.BLL.Interfaces
{
public interface IGenericRepository<T> where T : class
{
//Async => Wrapping to each methods at ( Task)
Task<IEnumerable<T>> GetAll();

Task<T> Get(int id);

void Add(T item);
void Update(T item);
void Delete(T item);
}
}
22 changes: 22 additions & 0 deletions Demo.BLL/Interfaces/IUnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.BLL.Interfaces
{
//signature for Property for each Repository
public interface IUnitOfWork : IAsyncDisposable
{
public IEmployeeRepository EmployeeRepository { get; set; }

public IDepartmentRepository DepartmentRepository { get; set; }

//signature for Method => (Complete) that represent SaveChanges at DBContext
//return int => Number of rows that had affect ,
Task< int> Complete();


}
}
19 changes: 19 additions & 0 deletions Demo.BLL/Repositories/DepartmentRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Demo.BLL.Interfaces;
using Demo.DAL.Contexts;
using Demo.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.BLL.Repositories
{
public class DepartmentRepository : GenericRepository<Department>,IDepartmentRepository
{
public DepartmentRepository(MVCAppDbContext dbContext):base(dbContext)
{

}
}
}
25 changes: 25 additions & 0 deletions Demo.BLL/Repositories/EmployeeRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Demo.BLL.Interfaces;
using Demo.DAL;
using Demo.DAL.Contexts;
using Demo.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.BLL.Repositories
{
public class EmployeeRepository : GenericRepository<Employee>, IEmployeeRepository
{
public EmployeeRepository(MVCAppDbContext dbContext) : base(dbContext)
{
}
public IQueryable<Employee> GetEmployeeByaddress(string address)
=> _dbContext.Employees.Where(E => E.Address.ToLower().Contains(address.ToLower()));

//Searching
public IQueryable<Employee> SearchByName(string name)
=> _dbContext.Employees.Where(E => E.Name.ToLower().Contains(name)); //search => Compare Two Names
}
}
54 changes: 54 additions & 0 deletions Demo.BLL/Repositories/GenericRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Demo.BLL.Interfaces;
using Demo.DAL;
using Demo.DAL.Contexts;
using Demo.DAL.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Demo.BLL.Repositories
{
public class GenericRepository<T> : IGenericRepository<T> where T : ModelBase
{
private protected readonly MVCAppDbContext _dbContext; //

public GenericRepository(MVCAppDbContext dbContext) //Ask From ClR Create
//Object from DbContext
{
//dbContext = /*new MVCAppDbContext();*/

_dbContext = dbContext;
}

//Just(Add, Update, Delete) Will change state of object
public void Add(T item)
=> _dbContext.Set<T>().Add(item); //for Example just state will change and is added

public void Update(T item)
=> _dbContext.Set<T>().Update(item);//Modified

public void Delete(T item)
=> _dbContext.Set<T>().Remove(item); //Deleted

public async Task<T> Get(int id)

=> await _dbContext.FindAsync<T>(id);


//After Install package (microsoft.entityframeworkcore.proxies), We make Eager Loading to load Data of Departments at Employee
public async Task<IEnumerable<T>> GetAll()
{
if (typeof(T) == typeof(Employee))
return (IEnumerable<T>) await _dbContext.Employees.Include(E => E.Department).AsNoTracking().ToListAsync();
else
return await _dbContext.Set<T>().AsNoTracking().ToListAsync();
}



}
}
43 changes: 43 additions & 0 deletions Demo.BLL/UnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Demo.BLL.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Demo.DAL.Contexts;
using Demo.BLL.Repositories;

namespace Demo.BLL
{
//UnitOfWork: Unit for each The Work With DbContext
//UnitOfWork Encapsulate DbContext ,PL deal with UnitOfWork and UnitOfWork deal with DbContext
//UnitOfWork => [Represent(deal with) Repositories + Implement Complete Method that Save Changes at DbContext + Dispose Method that dispsose connection]
public class UnitOfWork : IUnitOfWork
{
private readonly MVCAppDbContext _dbContext;

//Automatic properties => Compiler will get and set for Repository at Background
public IEmployeeRepository EmployeeRepository { get; set; }
public IDepartmentRepository DepartmentRepository { get; set; }


//CTOR, Because when create object from UnitOfWork=> Make Initialization to two Repositories
public UnitOfWork(MVCAppDbContext dbContext) //ASk CLR for creating Object DbContext
{
_dbContext = dbContext;
EmployeeRepository = new EmployeeRepository(dbContext);
DepartmentRepository = new DepartmentRepository(dbContext);
}
//Add Dbcontext Save Changes
public async Task<int> Complete()
{
return await _dbContext.SaveChangesAsync();
}


public async ValueTask DisposeAsync()
{
await _dbContext.DisposeAsync();
}
}
}
35 changes: 35 additions & 0 deletions Demo.DAL/Data/Configurations/DepartmentConfigurations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Demo.DAL.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.DAL.Data.Configurations
{
public class DepartmentConfigurations : IEntityTypeConfiguration<Department>
{
public void Configure(EntityTypeBuilder<Department> builder)
{
//Fluent APIs for Department Entity
builder.Property(D => D.Id).UseIdentityColumn(10,10);

//the First 3 Lines(by default بيتنفذوا كده كده)
builder.HasMany(D => D.Employees)
.WithOne(D => D.Department)
.HasForeignKey(E => E.DepartmentId)
.OnDelete(DeleteBehavior.Cascade);
;

builder.Property(D => D.Code)
.IsRequired(true);

builder.Property(E => E.Name)
.IsRequired(true)
.HasMaxLength(50);
}
}
}
26 changes: 26 additions & 0 deletions Demo.DAL/Data/Configurations/EmployeeConfigurations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.DAL.Data.Configurations
{
public class EmployeeConfigurations : IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
//fluent APIs

builder.Property(E => E.Salary)
.HasColumnType("decimal(18,2)");

builder.Property(E => E.Name)
.IsRequired(true)
.HasMaxLength(50);

}
}
}
46 changes: 46 additions & 0 deletions Demo.DAL/Data/Contexts/MVCAppDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Demo.DAL.Data.Configurations;
using Demo.DAL.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.DAL.Contexts
{
public class MVCAppDbContext:IdentityDbContext
{

public MVCAppDbContext(DbContextOptions<MVCAppDbContext>options):base(options)
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Fluent API

modelBuilder.ApplyConfiguration(new DepartmentConfigurations());

modelBuilder.ApplyConfiguration(new EmployeeConfigurations());

base.OnModelCreating(modelBuilder);
}
//protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
// =>optionsBuilder.UseSqlServer("Server = .; Database = MVCAppDb;Trusted_Connection = true;");

public DbSet<Department> Departments { get; set; }

public DbSet<Employee> Employees { get; set; }

///Identity Dbsets(4 Dbsets to Users and 3 Dbsets = 7 Dbsets) inherited from IdentityDbContext
///=> Generate 7 Tables and including tables(Users + Roles + Relationship between them(UserRoles) )
///public DbSet<IdentityUser> Users { get; set; }
///public DbSet<IdentityRole> Roles { get; set; }


}
}
Loading

0 comments on commit 9682833

Please sign in to comment.