Best approach between use and public object?

I would like to know what is the best approach between using an operator and creating an open variable. My example is as follows: I have a manager class that inherits from one-time use, and this class has access to my dbcontext and methods. What I'm doing now is my cs class, which searches for this class and creates and destroys my object for my needs. For example:

public class StudentManager: IDisposable { private ISchoolUnitOfWork _unitOfWork; public StudentManager() { _unitOfWork = new SchoolUnitOfWork(); } public IEnumerable<Student> GetStudents() } 

In my cs class, I:

 private IEnumerable<Stundets> GetStudents() { using (StudentManager manager = new StudentManager()) { return = manager.GetStudents(); } } 

OR

 private StudentManager = new Studentmanager(); 

What is the best way to do this: have an instance of my StudentManager (just create a connection and destroy when to leave the page) or work with using ?

I am a little confused by this. Thanks in advance!

I update my context on the same manager that causes persistence in my context, which is the interface of my unit of work, I do not have direct access to the context, but when I create it, I build one type of my unit of work.

I keep my operations on the manager. So on my update, insert, change manager, I call the save method, for example:

 public class StudentManager.... public Student UpdateStudent(Student student) { IStudentService service = new StudentService(_unitOfWork.StudentRepository); Student student= service.Update(student); _unitOfWork.Save(); return student; } 

In general, I have the IUnitOfWork and UnitOfWork interface, also have an IRepository and a repository. And I just use the manager to not create an instance of my UnitOfWork directly, but with the manager to it ... I think it is legal and useful!

+7
source share
2 answers

The best way is with using , because it automatically calls Dispose . You can guarantee that your deletion logic will take place. This is, in fact, how you should use objects such as SqlConnection and SqlCommand . So you are doing it right with using .

In fact, you stated that you are using DbContext to access data. They should be created on request and wrapped in using . There is no need to distribute an instance of these class types because the connection pool is executed on SQL Server through the connection string.

+1
source share

If you use the use, it may be in one, you need to write such materials more than once. for example

 //To get all students private IEnumerable<Stundets> GetStudents() { using (StudentManager manager = new StudentManager()) { return = manager.GetStudents(); } } //To save Students //To get all students private IEnumerable<Stundets> SaveStudents() { using (StudentManager manager = new StudentManager()) { return = manager.Save(); } } 

etc. But here you do not need to worry about the disposal of the object. He will take care automatically. If you go for a global declaration, you need to do it manually. So what I need to say: if you need to use StudentManager throughout the page, why don't you make it global and use it in all the necessary places, using both. Something like that

 private StudentManager manager; //To get all students private IEnumerable<Stundets> GetStudents() { using (manager = new StudentManager()) { return = manager.GetStudents(); } } //To save Students //To get all students private IEnumerable<Stundets> SaveStudents() { using (manager = new StudentManager()) { return = manager.Save(); } } 
0
source share

All Articles