Unit a = new Unit();
BaseRepository<Unit> unitRepository = new BaseRepository<Unit>();
...........
unitRepository.AddItem(a);
unitRepository.ChangeItem(a);
unitRepository.DeleteItem(a.ID);
* This source code was highlighted with Source Code Highlighter .
/// <summary>
/// Base repository for all sets.
/// </summary>
/// <typeparam name="T">Class from Business Model, should be EntityType</typeparam>
public class BaseRepository<T> where T : EntityObject
* This source code was highlighted with Source Code Highlighter .
//DataBase container
DBContainer db = new DBContainer();
* This source code was highlighted with Source Code Highlighter .
/// <summary>
/// Reflection - get name of T.
/// </summary>
private string name
{
get { return typeof (T).Name; }
}
* This source code was highlighted with Source Code Highlighter .
//Simple macros
Type dbT = typeof (DBContainer);
* This source code was highlighted with Source Code Highlighter .
using System;
using System.Collections. Generic ;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
namespace Chib.Lib
{
/// <summary>
/// Base repository for all sets.
/// </summary>
/// <typeparam name="T">Class from Business Model, should be EntityType</typeparam>
public class BaseRepository<T> where T : EntityObject
{
/// <summary>
/// Reflection - get name of T.
/// </summary>
private string name
{
get { return typeof (T).Name; }
}
//DataBase container
DBContainer db = new DBContainer();
//Simple macros
Type dbT = typeof (DBContainer);
/// <summary>
/// Get all items in Set as IQueryable, making easy to operate with data.
/// </summary>
/// <returns>Items in Set</returns>
public IQueryable<T> AllItems
{
get
{
return (IQueryable<T>)AllItemsAsObj;
}
}
/// <summary>
/// Get the items (ObjectSet) as Object. For internal use only.
/// </summary>
/// <returns>Object</returns>
private object AllItemsAsObj
{
get
{
PropertyInfo mi = dbT.GetProperty(name + "Set" );
object obj = mi.GetValue(db, null );
return obj;
}
}
/// <summary>
/// Add item to collection and save changes.
/// </summary>
/// <typeparam name="T">The type of item</typeparam>
/// <param name="item">Added item</param>
/// <returns>True if no errors.</returns>
public bool AddItem(T item)
{
try
{
object obj = AllItemsAsObj;
obj.GetType().GetMethod( "AddObject" ).Invoke(obj, new object [] { item });
db.SaveChanges();
return true ;
}
catch
{ return false ; }
}
/// <summary>
/// Get the single T item by it's ID
/// </summary>
/// <param name="id">Guid ID</param>
/// <returns>Null if nothing found.</returns>
public T GetItem( Guid id)
{
foreach ( var item in AllItems)
{
if ( new Guid (item.GetType().GetProperty( "ID" ).GetValue(item, null ).ToString()) == id)
return (T)item;
}
return null ;
}
/// <summary>
/// Delets an item by it's ID.
/// </summary>
/// <param name="id">ID of item</param>
/// <returns>True if no errors.</returns>
public bool DeleteItem( Guid id)
{
try
{
T item = GetItem(id);
object set = AllItemsAsObj;
set .GetType().GetMethod( "DeleteObject" ).Invoke( set , new object [] { item });
db.SaveChanges();
return true ;
}
catch
{
return false ;
}
}
public bool ChangeItem(T item)
{
try
{
var guid = new Guid (item.GetType().GetProperty( "ID" ).GetValue(item, null ).ToString());
T modyfying = AllItems.Single(x => x.GetType().GetProperty( "ID" ).GetValue( null , null ).ToString() == guid.ToString());
modyfying = item;
db.SaveChanges();
return true ;
}
catch
{
return false ;
}
}
/// <summary>
/// Force save changes to DB.
/// </summary>
/// <returns>True if no errors.</returns>
public bool SaveChanges()
{
try
{
db.SaveChanges();
return true ;
}
catch
{
return false ;
}
}
}
}
* This source code was highlighted with Source Code Highlighter .
/// <summary>
/// Get the single T item by it's ID
/// </summary>
/// <param name="id">Guid ID</param>
/// <returns>Null if nothing found.</returns>
public T GetItem( Guid id)
{
foreach ( var item in AllItems)
{
if ( new Guid (item.GetType().GetProperty( "ID" ).GetValue(item, null ).ToString()) == id)
return (T)item;
}
return null ;
}
* This source code was highlighted with Source Code Highlighter .
db.SaveChanges();
* This source code was highlighted with Source Code Highlighter .
BaseRepository<Unit> unitRepository = new BaseRepository<Unit>();
BaseRepository<City> cityRepository = new BaseRepository<City>();
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/124619/
All Articles