Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach between Using and public object?

Tags:

c#

asp.net

I would like to know what is the best approach between using statement and creating a public variable. My example is the following: I have a manager class which inherits from disposable, and this class has access to my dbcontext and methods to it. What I am doing now is on my cs class doing a ussing to that class and create and destroy my object to my needs. For example:

public class StudentManager:  IDisposable
{
    private ISchoolUnitOfWork _unitOfWork;

    public StudentManager()
    {
        _unitOfWork = new SchoolUnitOfWork();
    }

    public IEnumerable<Student> GetStudents()
}

On my cs class I do:

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

OR

private StudentManager = new Studentmanager();

What is the best way to do it: having the instance of my StudentManager (just create a connection and destroy when leave page) or working with the using?

I am a bit confused about that. Thanks in advance!

I do update my context on the same manager calling the save at my context which is an interface of my unit of work, I do not access directly to the context, but when I construct it I construct one type my unit of work.

I do save on my crud operations on the manager. So on my manager on update, insert, modify 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 an Interface IUnitOfWork and a UnitOfWork, also have a IRepository and a repository. And I just use a manager to not instantiate my UnitOfWork directly, but with a manager to it... I think that's legal and useful!

like image 832
user2528557 Avatar asked Jul 22 '13 11:07

user2528557


People also ask

Which methods should be public or private?

Generally you should expose as little as possible and make everything private that is possible. If you make a mistake and hide something you should be exposing, no problem, just make it public.

When you would use public and when you would use private in programming?

Which one we should use? We should use public access modifier if we want to make the method or property visible from anywhere, other classes, and instances of the object. Use the private access modifier if you want to make the method or property visible in its own class only.

What is the difference between public and private methods?

A public method can be invoked from anywhere—there are no restrictions on its use. A private method is internal to the implementation of a class, and it can only be called by other instance methods of the class (or, as we'll see later, its subclasses).

What is the approach of object-oriented programming?

Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.


1 Answers

The best way is the using statement because it calls Dispose automatically. You can guarantee that your disposal logic will occur. This is, in fact, how you should use objects like SqlConnection and SqlCommand as well. So you're doing it right with the using.

In fact, you stated that you're using a DbContext to access the data. Those should be instantiated on demand and wrapped in a using as well. There is no need to share an instance of these types of classes because connection pooling is done at the SQL Server via the connection string.

like image 95
Mike Perrenoud Avatar answered Sep 17 '22 10:09

Mike Perrenoud