这是我的班级:
public class UnitOfWork : IUnitOfWork
{
private readonly ApplicationDbContext _context;
public IProducts Products{ get; private set; }
public IGenreRepository Genres { get; private set; }
public IFollowingRepository Followings { get; private set; }
public IApplicationUserRepository Users { get; private set; }
public INotificationRepository Notifications { get; private set; }
public IUserNotificationRepository UserNotifications { get; private set; }
public UnitOfWork(ApplicationDbContext context)
{
_context = context;
Products = new ProductRepository(context);
Genres = new GenreRepository(context);
Followings = new FollowingRepository(context);
Users = new ApplicationUserRepository(context);
Notifications = new NotificationRepository(context);
UserNotifications = new UserNotificationRepository(context);
}
public void Complete()
{
_context.SaveChanges();
}
}
这是来自控制器的示例:
public class HomeController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public HomeController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
}
我的问题是:它会导致内存泄漏吗?想象一下,我有200个存储库,每次请求将创建200个对象。或者我应该将每个存储库显式传递给控制器?