gpt4 book ai didi

c# - 我可以将 'using' 上下文传递给 c# 中的另一个方法吗?

转载 作者:太空宇宙 更新时间:2023-11-03 10:54:44 24 4
gpt4 key购买 nike

我有一些 EF 代码来检索 Controller 中的一些对象,但我想拆分我的函数以提高代码重用。

我的代码目前看起来像这样:

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();
}

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

最佳答案

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

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

然后你的 Controller 看起来像这样

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

关于c# - 我可以将 'using' 上下文传递给 c# 中的另一个方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19980477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com