2

我有一些 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();
}

有没有这样的事情我可以这样做,这样我既可以分离出我的代码,也可以使用尽可能少的数据库连接数?

4

1 回答 1

2

我会首先转向服务类。所以你可以有一个像这样的新类:

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
        }
    }

控制器将迁移到此:

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);
}
于 2013-11-14T15:32:01.683 回答