Repository.Get C# (CSharp) Method

Get() private method

private Get ( int id ) : T
id int
return T
        T Get<T>(int id) where T : EntityBase    
    	{
    		string[] interfaceList = new string[] 
    			{ "ITeacher", "IStudent"};
    
    		Type interfaceType = null;
    		foreach (string s in interfaceList)
    		{
    			var types = typeof(T).FindInterfaces((x, y) => x.Name == y.ToString(), s);
    
    			if (types.Length > 0)
    				interfaceType = types[0];
    		}
    
    		if (interfaceType == null)
    			throw new Exception("Unknown Interface " + typeof(T).Name);
    
    		MethodInfo method = typeof(Context).GetMethod("Get");
    		MethodInfo generic = method.MakeGenericMethod(interfaceType);
    		generic.Invoke(this, new object [] { id } );
    	}

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            var soapShopDb = new SoapShopDb();
            var component = new Repository(soapShopDb);

            // Adding some entity
            component.Add(new Customer { CustomerName = "Nata", CustomerPhone = "679011198" });
            component.Save();
            component.Add(new SoapProduct { Tittle = "Eucalyptus", Mass = 100, Price = 25.2M });
            component.Save();

            // Delating some entity
            var comp = soapShopDb.Set<Customer>().FirstOrDefault(i => i.CustomerName == "Nata");
            component.Delete(comp);
            component.Save();

            // Reading some entity
            var list = component.Get<Customer>();
            foreach (var item in list)
            {
                Console.WriteLine(item.Id + " " + item.CustomerName + " " + item.CustomerPhone + "\n");
            }

            // Find one
            var comp2 = component.Get<Customer>(3);
            Console.WriteLine(comp2.CustomerName);
        }
All Usage Examples Of Repository::Get