Key points
using ConsoleApp18;
using Employeenamespace;
using System;
using Managernamespace;
namespace Covariance
{
class Program
{
static void Main(string[] args)
{
var employeeRepository = new GenericRepository();
employeeRepository.Add(new Employee { Id = 1, Name = "Adam" });
employeeRepository.Add(new Employee { Id = 2, Name = "Abhishek" });
employeeRepository.Add(new Employee { Id = 3, Name = "Aman" });
employeeRepository.Add(new Employee { Id = 4, Name = "Naveen" });
employeeRepository.Save();
Console.WriteLine("-------------------------------");
var managerRepository = new GenericRepository();
managerRepository.Add(new Manager { Id = 1, Name = "Adam1" });
managerRepository.Add(new Manager { Id = 2, Name = "Abhishek1" });
managerRepository.Add(new Manager { Id = 3, Name = "Aman1" });
managerRepository.Add(new Manager { Id = 4, Name = "Naveen1" });
managerRepository.Save();
/*Inheritence using Generics*/
var managerRepositoryWithRemove = new EmployeeRepositoryWithRemove();
//managerRepositoryWithRemove.Remove(new Manager());
Console.WriteLine("-------------------------------");
var managerRepositoryWithRemove1 = new EmployeeRepositoryWithRemoveMultipleKey();
Console.ReadLine();
}
}
}
namespace Managernamespace
{
class Manager
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString() => $"My Name is {Name} with Id :: {Id}";
}
}
namespace Employeenamespace
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString() => $"My Name is {Name} with Id :: {Id}";
}
}
using System;
using System.Collections.Generic;
namespace ConsoleApp18
{
public class GenericRepository
{
protected readonly List _items = new List();
public void Add(T item)
{
_items.Add(item);
}
public void Save()
{
foreach (var item in _items)
{
Console.WriteLine(item);
}
}
}
/*Inheritence using Generics..*/
public class EmployeeRepositoryWithRemove : GenericRepository
{
public void Remove(T item)
{
_items.Remove(item);
}
}
/*-----------------------------------------------------------------------*/
public class GenericRepositoryMulltipleParameter
{
public TKey Key { get; set; }
protected readonly List _items = new List();
public void Add(T item)
{
_items.Add(item);
}
public void Save()
{
foreach (var item in _items)
{
Console.WriteLine(item);
}
}
}
/*Inheritence using Generics..*/
//public class EmployeeRepositoryWithRemoveMultipleKey : GenericRepositoryMulltipleParameter
//public class EmployeeRepositoryWithRemoveMultipleKey : GenericRepositoryMulltipleParameter
public class EmployeeRepositoryWithRemoveMultipleKey : GenericRepositoryMulltipleParameter
{
public void Remove(T item)
{
_items.Remove(item);
}
}
}