ایجاد یک Repository در پروژه برای دستورات EF
نویسنده: میثم ثوامری
تاریخ: ۱۳۹۱/۰۵/۱۹ ۱۱:۴۷
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
Public Class Employee
Public Property ID As Integer
Public Property Fname As String
Public Property Bdate As DateTime
End Class
Imports System.Data.Entity
Public Class EmployeeDBContext : Inherits DBContext
Public Property Employees As DbSet(Of Employee)
End Class
Interface EmployeeRepository
ReadOnly Property All As List(Of Employee)
Function Find(id As Integer) As Employee
Sub InsertOrUpdate(p As Employee)
Sub Delete(id As Integer)
Sub Save()
End Interface
Public Class EmployeeClass : Implements EmployeeRepository
Private DB As New EmployeeDBContext
Public ReadOnly Property All As List(Of Employee) Implements EmployeeRepository.All
Get
Return DB.Employees.ToList()
End Get
End Property
Public Sub Delete(id As Integer) Implements EmployeeRepository.Delete
Dim query = DB.Employees.Single(Function(q) q.ID = id)
DB.Employees.Remove(query)
End Sub
Public Function Find(id As Integer) As Employee Implements EmployeeRepository.Find
Return DB.Employees.Where(Function(q) q.ID = id)
End Function
Public Sub InsertOrUpdate(p As Employee) Implements EmployeeRepository.InsertOrUpdate
If p.ID = Nothing Then
DB.Employees.Add(p)
Else
DB.Entry(p).State = Data.EntityState.Modified
End If
End Sub
Public Sub Save() Implements EmployeeRepository.Save
DB.SaveChanges()
End Sub
End Class
Dim cls As New EmployeeClass
Public Sub BindGrid()
GridView1.DataSource = cls.All
GridView1.DataBind()
End Sub
using System;
using System.Collections;
using System.Linq;
namespace Framework.Model
{
public interface IContext
{
T Get<T>(Func<T, bool> prediction) where T : class;
IEnumerable List<T>(Func<T, bool> prediction) where T : class;
void Insert<T>(T entity) where T : class;
int Save();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;
namespace Framework.Model
{
public class Context : IContext
{
private readonly DbContext _dbContext;
public Context(DbContext context)
{
_dbContext = context;
}
public T Get<T>(Func<T,bool> prediction) where T : class
{
var dbSet = _dbContext.Set<T>();
if (dbSet!= null)
return dbSet.Single(prediction);
throw new Exception();
}
public void Insert<T>(T entity) where T : class
{
var dbSet = _dbContext.Set<T>();
if (dbSet != null)
{
_dbContext.Entry(entity).State = EntityState.Added;
}
}
public int Save()
{
return _dbContext.SaveChanges();
}
IEnumerable IContext.List<T>(Func<T, bool> prediction)
{
var dbSet = _dbContext.Set<T>();
if (dbSet != null)
return dbSet.Where(prediction).ToList();
throw new Exception();
}
}
}
using System.Data.Entity;
using DataModel;
namespace Model
{
public class EFContext : DbContext
{
public EFContext(string db): base(db)
{
}
public DbSet<Product> Products { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
namespace Model
{
public class Context : Framework.Model.Context
{
public Context(string db): base(new EFContext(db))
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Biz
{
public class Context : Model.Context
{
public Context(string db) : base(db)
{
}
}
}
using System.Web.Mvc;
using Framework.Model;
namespace ProductionRepository.Controllers
{
public class BaseController : Controller
{
public IContext DataContext { get; set; }
public BaseController()
{
DataContext = new Biz.Context(System.Configuration.ConfigurationManager.ConnectionStrings["Database"].ConnectionString);
}
}
}
using System.Web.Mvc;
using DataModel;
using System.Collections.Generic;
namespace ProductionRepository.Controllers
{
public class ProductController : BaseController
{
public ActionResult Index()
{
var x = DataContext.List<Product>(s => s.Name != null);
return View(x);
}
}
}
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace TestUnit
{
[TestFixture]
public class Test
{
[Test]
public void IndexShouldListProduct()
{
var repo = new Moq.Mock<Framework.Model.IContext>();
var products = new List<DataModel.Product>();
products.Add(new DataModel.Product { Id = 1, Name = "asdasdasd" });
products.Add(new DataModel.Product { Id = 2, Name = "adaqwe" });
products.Add(new DataModel.Product { Id = 4, Name = "qewqw" });
products.Add(new DataModel.Product { Id = 5, Name = "qwe" });
repo.Setup(x => x.List<DataModel.Product>(p => p.Name != null)).Returns(products.AsEnumerable());
var controller = new ProductionRepository.Controllers.ProductController();
controller.DataContext = repo.Object;
var result = controller.Index() as ViewResult;
var model = result.Model as List<DataModel.Product>;
Assert.AreEqual(4, model.Count);
Assert.AreEqual("", result.ViewName);
}
}
}