debugger
سه شنبه 20 مرداد 1394, 13:17 عصر
سلام بنده در پروژه ی خود از EF Code First استفاده کردم . حال یک الگوی Repository از اینترنت پیدا کردم کد به شرح زیر است
using System;using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ZabanKadeh.Models
{
public interface IRepository : IDisposable
{
/// <summary>
/// Gets all objects from database
/// </summary>
/// <returns></returns>
IQueryable<T> All<T>() where T : class;
/// <summary>
/// Gets objects from database by filter.
/// </summary>
/// <param name="predicate">Specified a filter</param>
/// <returns></returns>
IQueryable<T> Filter<T>(Expression<Func<T, bool>> predicate) where T : class;
/// <summary>
/// Gets objects from database with filting and paging.
/// </summary>
/// <typeparam name="Key"></typeparam>
/// <param name="filter">Specified a filter</param>
/// <param name="total">Returns the total records count of the filter.</param>
/// <param name="index">Specified the page index.</param>
/// <param name="size">Specified the page size</param>
/// <returns></returns>
IQueryable<T> Filter<T>(Expression<Func<T, bool>> filter, out int total, int index = 0, int size = 50) where T : class;
/// <summary>
/// Gets the object(s) is exists in database by specified filter.
/// </summary>
/// <param name="predicate">Specified the filter expression</param>
/// <returns></returns>
bool Contains<T>(Expression<Func<T, bool>> predicate) where T : class;
/// <summary>
/// Find object by keys.
/// </summary>
/// <param name="keys">Specified the search keys.</param>
/// <returns></returns>
T Find<T>(params object[] keys) where T : class;
/// <summary>
/// Find object by specified expression.
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
T Find<T>(Expression<Func<T, bool>> predicate) where T : class;
/// <summary>
/// Create a new object to database.
/// </summary>
/// <param name="t">Specified a new object to create.</param>
/// <returns></returns>
T Create<T>(T t) where T : class;
/// <summary>
/// Delete the object from database.
/// </summary>
/// <param name="t">Specified a existing object to delete.</param>
int Delete<T>(T t) where T : class;
/// <summary>
/// Delete objects from database by specified filter expression.
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
int Delete<T>(Expression<Func<T, bool>> predicate) where T : class;
/// <summary>
/// Update object changes and save to database.
/// </summary>
/// <param name="t">Specified the object to save.</param>
/// <returns></returns>
int Update<T>(T t) where T : class;
/// <summary>
/// Select Single Item by specified expression.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
T Single<T>(Expression<Func<T, bool>> expression) where T : class;
void SaveChanges();
void ExecuteProcedure(String procedureCommand, params SqlParameter[] sqlParams);
}
public class Repository : IRepository
{
ZabanKadehContext Context;
public Repository()
{
Context = new ZabanKadehContext();
}
public Repository(ZabanKadehContext context)
{
Context = context;
}
public void CommitChanges()
{
Context.SaveChanges();
}
public T Single<T>(Expression<Func<T, bool>> expression) where T : class
{
return All<T>().FirstOrDefault(expression);
}
public IQueryable<T> All<T>() where T : class
{
return Context.Set<T>().AsQueryable();
}
public virtual IQueryable<T> Filter<T>(Expression<Func<T, bool>> predicate) where T : class
{
return Context.Set<T>().Where<T>(predicate).AsQueryable<T>();
}
public virtual IQueryable<T> Filter<T>(Expression<Func<T, bool>> filter, out int total, int index = 0, int size = 50) where T : class
{
int skipCount = index * size;
var _resetSet = filter != null ? Context.Set<T>().Where<T>(filter).AsQueryable() : Context.Set<T>().AsQueryable();
_resetSet = skipCount == 0 ? _resetSet.Take(size) : _resetSet.Skip(skipCount).Take(size);
total = _resetSet.Count();
return _resetSet.AsQueryable();
}
public virtual T Create<T>(T TObject) where T : class
{
var newEntry = Context.Set<T>().Add(TObject);
Context.SaveChanges();
return newEntry;
}
public virtual int Delete<T>(T TObject) where T : class
{
Context.Set<T>().Remove(TObject);
return Context.SaveChanges();
}
public virtual int Update<T>(T TObject) where T : class
{
try
{
var entry = Context.Entry(TObject);
Context.Set<T>().Attach(TObject);
entry.State = EntityState.Modified;
return Context.SaveChanges();
}
catch (OptimisticConcurrencyException ex)
{
throw ex;
}
}
public virtual int Delete<T>(Expression<Func<T, bool>> predicate) where T : class
{
var objects = Filter<T>(predicate);
foreach (var obj in objects)
Context.Set<T>().Remove(obj);
return Context.SaveChanges();
}
public bool Contains<T>(Expression<Func<T, bool>> predicate) where T : class
{
return Context.Set<T>().Count<T>(predicate) > 0;
}
public virtual T Find<T>(params object[] keys) where T : class
{
return (T)Context.Set<T>().Find(keys);
}
public virtual T Find<T>(Expression<Func<T, bool>> predicate) where T : class
{
return Context.Set<T>().FirstOrDefault<T>(predicate);
}
public virtual void ExecuteProcedure(String procedureCommand, params SqlParameter[] sqlParams){
Context.Database.ExecuteSqlCommand(procedureComman d, sqlParams);
}
public virtual void SaveChanges()
{
Context.SaveChanges();
}
public void Dispose()
{
if (Context != null)
Context.Dispose();
}
}
}
حال میخوام دستور sql زیر را اجرا کنم .
SELECT username, pass, idUserType, idFROM tbRegisterUser
WHERE (idUserType = 5)
دو جدول RegisterUser و UserType با هم رابطه دارند .
using System;using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ZabanKadeh.Models
{
public interface IRepository : IDisposable
{
/// <summary>
/// Gets all objects from database
/// </summary>
/// <returns></returns>
IQueryable<T> All<T>() where T : class;
/// <summary>
/// Gets objects from database by filter.
/// </summary>
/// <param name="predicate">Specified a filter</param>
/// <returns></returns>
IQueryable<T> Filter<T>(Expression<Func<T, bool>> predicate) where T : class;
/// <summary>
/// Gets objects from database with filting and paging.
/// </summary>
/// <typeparam name="Key"></typeparam>
/// <param name="filter">Specified a filter</param>
/// <param name="total">Returns the total records count of the filter.</param>
/// <param name="index">Specified the page index.</param>
/// <param name="size">Specified the page size</param>
/// <returns></returns>
IQueryable<T> Filter<T>(Expression<Func<T, bool>> filter, out int total, int index = 0, int size = 50) where T : class;
/// <summary>
/// Gets the object(s) is exists in database by specified filter.
/// </summary>
/// <param name="predicate">Specified the filter expression</param>
/// <returns></returns>
bool Contains<T>(Expression<Func<T, bool>> predicate) where T : class;
/// <summary>
/// Find object by keys.
/// </summary>
/// <param name="keys">Specified the search keys.</param>
/// <returns></returns>
T Find<T>(params object[] keys) where T : class;
/// <summary>
/// Find object by specified expression.
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
T Find<T>(Expression<Func<T, bool>> predicate) where T : class;
/// <summary>
/// Create a new object to database.
/// </summary>
/// <param name="t">Specified a new object to create.</param>
/// <returns></returns>
T Create<T>(T t) where T : class;
/// <summary>
/// Delete the object from database.
/// </summary>
/// <param name="t">Specified a existing object to delete.</param>
int Delete<T>(T t) where T : class;
/// <summary>
/// Delete objects from database by specified filter expression.
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
int Delete<T>(Expression<Func<T, bool>> predicate) where T : class;
/// <summary>
/// Update object changes and save to database.
/// </summary>
/// <param name="t">Specified the object to save.</param>
/// <returns></returns>
int Update<T>(T t) where T : class;
/// <summary>
/// Select Single Item by specified expression.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
T Single<T>(Expression<Func<T, bool>> expression) where T : class;
void SaveChanges();
void ExecuteProcedure(String procedureCommand, params SqlParameter[] sqlParams);
}
public class Repository : IRepository
{
ZabanKadehContext Context;
public Repository()
{
Context = new ZabanKadehContext();
}
public Repository(ZabanKadehContext context)
{
Context = context;
}
public void CommitChanges()
{
Context.SaveChanges();
}
public T Single<T>(Expression<Func<T, bool>> expression) where T : class
{
return All<T>().FirstOrDefault(expression);
}
public IQueryable<T> All<T>() where T : class
{
return Context.Set<T>().AsQueryable();
}
public virtual IQueryable<T> Filter<T>(Expression<Func<T, bool>> predicate) where T : class
{
return Context.Set<T>().Where<T>(predicate).AsQueryable<T>();
}
public virtual IQueryable<T> Filter<T>(Expression<Func<T, bool>> filter, out int total, int index = 0, int size = 50) where T : class
{
int skipCount = index * size;
var _resetSet = filter != null ? Context.Set<T>().Where<T>(filter).AsQueryable() : Context.Set<T>().AsQueryable();
_resetSet = skipCount == 0 ? _resetSet.Take(size) : _resetSet.Skip(skipCount).Take(size);
total = _resetSet.Count();
return _resetSet.AsQueryable();
}
public virtual T Create<T>(T TObject) where T : class
{
var newEntry = Context.Set<T>().Add(TObject);
Context.SaveChanges();
return newEntry;
}
public virtual int Delete<T>(T TObject) where T : class
{
Context.Set<T>().Remove(TObject);
return Context.SaveChanges();
}
public virtual int Update<T>(T TObject) where T : class
{
try
{
var entry = Context.Entry(TObject);
Context.Set<T>().Attach(TObject);
entry.State = EntityState.Modified;
return Context.SaveChanges();
}
catch (OptimisticConcurrencyException ex)
{
throw ex;
}
}
public virtual int Delete<T>(Expression<Func<T, bool>> predicate) where T : class
{
var objects = Filter<T>(predicate);
foreach (var obj in objects)
Context.Set<T>().Remove(obj);
return Context.SaveChanges();
}
public bool Contains<T>(Expression<Func<T, bool>> predicate) where T : class
{
return Context.Set<T>().Count<T>(predicate) > 0;
}
public virtual T Find<T>(params object[] keys) where T : class
{
return (T)Context.Set<T>().Find(keys);
}
public virtual T Find<T>(Expression<Func<T, bool>> predicate) where T : class
{
return Context.Set<T>().FirstOrDefault<T>(predicate);
}
public virtual void ExecuteProcedure(String procedureCommand, params SqlParameter[] sqlParams){
Context.Database.ExecuteSqlCommand(procedureComman d, sqlParams);
}
public virtual void SaveChanges()
{
Context.SaveChanges();
}
public void Dispose()
{
if (Context != null)
Context.Dispose();
}
}
}
حال میخوام دستور sql زیر را اجرا کنم .
SELECT username, pass, idUserType, idFROM tbRegisterUser
WHERE (idUserType = 5)
دو جدول RegisterUser و UserType با هم رابطه دارند .