dimanche 17 janvier 2016

Initializing objects using this operator in constructors

As I was trying to implement code first approach using repository patterns in MVC, I have come across some difficulties as below:

I have a Interface that have few methods declared as below :

public interface IRepository
    {
        User Getuserdetail(int UserId);
        void Save(User Obj);
        void delete(int Userid);
        void update(User user);
    }

Then I have a Repository class which could be using the above interface to define all the methods and will create a seperate layer of Entity Framework:

public class Repository : IRepository
    {
            RepoDBContext _context;
            Repository(RepoDBContext Context)
            {
                this._context = Context;
            }

        public User Getuserdetail(int Userid)
        {
            var user = _context.User.Where(m => m.id == Userid).FirstOrDefault();
            return user;
        }
        public void Save(User user)
        {
            _context.User.Add(user);
            _context.SaveChanges();
        }
}

Now could you please check the constructor of this class. The variable this constructor is initializing is type of "RepoDBContext" and the reference type it is using to assign it is also of "RepoDBContext". Is it internally performing like below?

RepoDBContext _context=new RepoDBContext();

My RepoDBContext class is below:

  public class RepoDBContext : DbContext
            {
                public DbSet<User> User { get; set; }
            }

Moreover if is the right way to perform then how will i have to call this class in my controller to do some functionality over my user interface. Please guide regarding the same and please don't mind my english. Thanks

Aucun commentaire:

Enregistrer un commentaire