0

I want to ask something about asp.net mvc 3 dependency injection ninject.

Here is my Interface,

 public interface IRegistration<T>
{
    bool Registration(T Entity); 
}

This is ClsMembers class.

  public class ClsMembers:IRegistration<Member>
    {
        private SmileWorkDbEntities db;

        public ClsMembers()
        {
            db = new SmileWorkDbEntities();
        }

        public bool Registration(Member member)
        {
            db.Members.Add(member);

            if (db.SaveChanges() != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int GetMemberId(string username, string pwd)
        {
            var Mem = (from m in db.Members where m.Member_username == username && m.Member_password == pwd select m).FirstOrDefault();
            return Mem.Member_id;
        }
    }

here is my controller,

public class MembersRegistrationController : Controller
{
    IRegistration<Member> ireg1;        

    public MembersRegistrationController(IRegistration<Member> _ireg1)
    {
        ireg1 = _ireg1;            
    }

    public ActionResult MemberRegistration()
    {
        return View();
    }

    [HttpPost]
    public ActionResult MemberRegistration(Member m)
    {
        if(ireg1.Registration(m))
        {                

            return RedirectToAction("MemberProfileRegistration", new {mId = i });
        }
        else
        {
            return View();
        }
    }       

}

Everything is ok... but i cannot access GetMemberId() method.. pls tell me how can i access GetMemberId() from my controller...

Regard, MinThitTun

1 Answer 1

2

Modify your IRegistration interface by adding int GetMemberId(string username, string pwd) method:

public interface IRegistration<T>
{
    bool Registration(T Entity);
    int GetMemberId(string username, string pwd);
}

After all, I thing you should read Interfaces (C# Programming Guide)

UPDATE:

public interface IMembersRepository
{
    int GetMemberId(string username, string password);
    // Other stuff related to members...
}

public class MembersRepository : IMembersRepository
{
    private SmileWorkDbEntities db = new SmileWorkDbEntities();

    public int GetMemberId(string username, string password)
    {
        var Mem = (from m in db.Members where m.Member_username == username && m.Member_password == pwd select m).FirstOrDefault();
        return Mem.Member_id;
    }
    // Other stuff related to members...
}

public class MembersRegistrationController : Controller
{
    IRegistration<Member> ireg1;        
    IMembersRepository membersRepository;

    public MembersRegistrationController(IRegistration<Member> _ireg1, IMembersRepository memRepository)
    {
        ireg1 = _ireg1;            
        membersRepository = memRepository;
    }

    // ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes thanks... but GetMemberId(string username, string pwd) method will not contain in every class.. so i didn't add in the interface.... so do u have any idea.. ? pls
So simply put GetMemberId in another interface for example IMembersRepository and then, pass it as a constructor parameter to controllers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.