0

我想问一些关于 asp.net mvc 3 依赖注入 ninject 的问题。

这是我的界面,

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

这是 ClsMembers 类。

  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;
        }
    }

这是我的控制器,

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();
        }
    }       

}

一切正常...但我无法访问 GetMemberId() 方法。请告诉我如何从我的控制器访问 GetMemberId()...

问候,MinThitTun

4

1 回答 1

2

通过添加方法修改您的IRegistration界面:int GetMemberId(string username, string pwd)

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

毕竟,我认为您应该阅读接口(C# 编程指南)

更新:

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;
    }

    // ...
}
于 2013-05-15T07:21:34.737 回答