我有一些EF代码来检索控制器中的某些对象,但我想拆分我的函数来改进代码重用。
我的代码目前看起来像这样:
public ActionResult SentMessages(){
MyModel model = new MyModel();
int user_id = GetCurrentUserId();
using(DataContext db = new DataContext()){
model.Messages =
db.Messages
.Where(x => x.sent == true)
.Where(x => x.user_id == user_id)
.Where(x => x.date_deleted == null)
.OrderBy(x => x.date_sent)
.Take(10)
.ToList();
model.Groups = db.Groups
.Where(x => x.user_id == user_id)
.Where(x => x.date_deleted == null)
.OrderBy(x => x.date_created)
.ToList();
}
return model;
}
我想把它分成可重复使用的代码块,(并让我的控制器更小),就像这样
public ActionResult SentMessages(){
MyModel model = new MyModel();
int user_id = GetCurrentUserId();
model.Messages = GetLastTenMessages(user_id);
model.Groups = GetGroups(user_id);
return model;
}
public static List<Message> GetLastTenMessages(int user_id){
using(DataContext db = new DataContext()){
return db.Messages
.Where(x => x.sent == true)
.Where(x => x.user_id == user_id)
.Where(x => x.date_deleted == null)
.OrderBy(x => x.date_sent)
.Take(10)
.ToList();
}
}
public static List<Group> GetGroups(int user_id){
using(DataContext db = new DataContext()){
return db.Groups
.Where(x => x.user_id == user_id)
.Where(x => x.date_deleted == null)
.OrderBy(x => x.date_created)
.ToList();
}
}
但是,这会导致与数据库的两个独立连接(据我所知)。每个查询都打开和关闭一个。
有没有办法将上下文传递给方法,就像这样
public ActionResult SentMessages(){
MyModel model = new MyModel();
int user_id = GetCurrentUserId();
using(DataContext db = new DataContext()){
model.Messages = GetLastTenMessages(user_id, db);
model.Groups = GetGroups(user_id, db);
}
return model;
}
public static List<Message> GetLastTenMessages(int user_id, DataContext db){
return db.Messages
.Where(x => x.sent == true)
.Where(x => x.user_id == user_id)
.Where(x => x.date_deleted == null)
.OrderBy(x => x.date_sent)
.Take(10)
.ToList();
}
public static List<Group> GetGroups(int user_id, DataContext db){
return db.Groups
.Where(x => x.user_id == user_id)
.Where(x => x.date_deleted == null)
.OrderBy(x => x.date_created)
.ToList();
}
有没有这样的东西我可以这样做,我可以分离出我的代码,也可以使用尽可能少的数据库连接?
答案 0 :(得分:2)
我首先会朝着Service类的方向前进。所以你可以有一个像这样的新课:
public class UserService
{
private DataContext _db;
//private int _user_id
public UserService(DataContext db)
{
_db = db
//perhaps it would be better to get the user id here
//rather than pass it in to the methods as a parameter
//_user_id = GetCurrentUserId();
//or maybe put HttpContext into DataContext and do this:
//_user_id = db.GetCurrentUserId();
}
private List<Message> GetLastTenMessages(int user_id){
return _db.Messages
.Where(x => x.sent == true)
.Where(x => x.user_id == user_id)
.Where(x => x.date_deleted == null)
.OrderBy(x => x.date_sent)
.Take(10)
.ToList();
}
private List<Group> GetGroups(int user_id){
return _db.Groups
.Where(x => x.user_id == user_id)
.Where(x => x.date_deleted == null)
.OrderBy(x => x.date_created)
.ToList();
}
public MyModel GetSentMessages(int user_id)
{
MyModel model = new MyModel();
model.Messages = GetLastTenMessages(user_id, db);
model.Groups = GetGroups(user_id, db);
return model
}
}
}
然后您的控制器将如下所示
public ActionResult SentMessages(){
using(DataContext db = new DataContext()){
var us = new UserService(db);
return View(us.GetSentMessages(GetCurrentUserId()));
}
}
然后我会介绍数据访问的存储库。
然后,UserService
将迁移到以下内容:
public class UserService
{
public UserService(DataContext db)
{
_db = db;
_msgRepo = new MessageRepository(_db.Messages);
_groupsRepo = new GroupsRepository(_db.Groups);
}
public MyModel GetSentMessages()
{
MyModel model = new MyModel();
model.Messages = _msgRepo.GetLastTenMessages(db.user_id);
model.Groups = _groupsRepo.GetGroups(db.user_id);
return model
}
}
}
然后我考虑将DbContext
包装在我自己的UnitOfWork
课程中。
然后,UserService
将迁移到以下内容:
public class UserService
{
private UnitOfWork _uow;
public UserService(UnitOfWork uow)
{
_uow = uow;
}
public MyModel GetSentMessages()
{
MyModel model = new MyModel();
model.Messages = _uow.MessageRepo.GetLastTenMessages();
model.Groups = _uow.GroupRepo.GetGroups();
return model
}
}
Controller将迁移到此:
private UnitOfWork _uow;
public UserController(UnitOfWork uow)
{
_uow = uow;
_us = new UserService(_uow);
}
public ActionResult SentMessages()
{
return View(us.GetSentMessages());
}
protected override void Dispose(bool disposing)
{
if (disposing)
_uow.Dispose();
base.Dispose(disposing);
}