我用VS2010 建了个MVC项目 发现个问题
在Controller当中 先定义了 两个对象 而这两个类 IFormsAuthenticationService 和 IMembershipService 是自定义的 public class AccountController : Controller
    {        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }
}
查看这两个类 转到定义 在Models中 居然发现 这两个不是类 是接口   public interface IFormsAuthenticationService
    {
        void SignIn(string userName, bool createPersistentCookie);
        void SignOut();
    } public interface IMembershipService
    {
        int MinPasswordLength { get; }        bool ValidateUser(string userName, string password);
        MembershipCreateStatus CreateUser(string userName, string password, string email);
        bool ChangePassword(string userName, string oldPassword, string newPassword);
    }接着往下看 发现了更奇怪的事 居然用这个接口定义的“对象”来调用ValidateUser函数了
MembershipService.ValidateUser(model.UserName, model.Password))
        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, model.RememberMe);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "提供的用户名或密码不正确。");
                }
            }            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }在往下看 在Models中 定义了一个类来继承此接口 然后 在类中重写了该ValidateUser函数
 public class AccountMembershipService : IMembershipService
    {
  public bool ValidateUser(string userName, string password)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("值不能为 null 或为空。", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("值不能为 null 或为空。", "password");            return _provider.ValidateUser(userName, password);
        }
}
而程序运行正常 那就是说 在Controller中 虽然调用的是 接口IMembershipService中的ValidateUser函数 但实际 运行了
AccountMembershipService 的函数ValidateUser
这是为什么 
这些代码 是VS2010 创建MVC时 自带的  不明白啊

解决方案 »

  1.   

    使用ASP.NET MVC2+PDF.NET 构建一个简单的新闻管理程序 
      

  2.   

    这是什么?。    public class AccountController : Controller
        {        public IFormsAuthenticationService FormsService { get; set; }
            public IMembershipService MembershipService { get; set; }        protected override void Initialize(RequestContext requestContext)
            {
                if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
                if (MembershipService == null) { MembershipService = new AccountMembershipService(); }            base.Initialize(requestContext);
            }
        }
      

  3.   

    只不过是为了SHOW下,多面向对象了几次
      

  4.   

    楼主说的意思,是不是怎么接口能直接调方法?
    我也刚做MVC不长时间,前一阵也研究了一下。
    每个类库,都有对应该类库的一个文件
    类库名Module.cs  文件,你看看有没有,就只该类库下第一级菜单里。这里边有方法public void RegisterWithContainer()
    {
        container
          .RegisterType<IFilesService, FilesService>(new ContainerControlledLifetimeManager())
          .RegisterType<IFilesRepository, FilesRepository>(new ContainerControlledLifetimeManager());
    }我感觉,掉接口方法的时候,是通过它,来反射某一个接口对应的类的方法。
    我公司是这么个情况。
    这是我个人的理解。仅供参考。
      

  5.   

    我刚才又看了一遍  没有发现 override virtual 这样的关键字 多态的话 是在哪里体现的 
      

  6.   

    我又看了一遍 我的理解有误区 好迷茫我把问题 具体分析下 再写一下 
    首先看这个第一个代码  这个是定义属性  然后返回值 IMembershipService 这个接口的“对象”
    不知道我这么理解对不对 public IMembershipService MembershipService { get; set; }我是按照我以前写过的属性 推理出来的 如果是 object 的类话 那返回的 应该是一个object 的对象  所以 上面的代码 返回的应该是IMembershipService 这个接口的对象 可是我记得 接口是不能定义对象的 
      public object HZ
            {
                get
                {
                    return  hz;
                }        }
    第二,他在Controller当中 使用了 这个“属性” 或者是“对象” 来调用了 自己接口里的方法  [HttpPost]
            public ActionResult LogOn(LogOnModel model, string returnUrl)
            {
                if (ModelState.IsValid)
                {
                    if (MembershipService.ValidateUser(model.UserName, model.Password))
                    {
                        FormsService.SignIn(model.UserName, model.RememberMe);
                        if (!String.IsNullOrEmpty(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Home");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "提供的用户名或密码不正确。");
                    }
                }            // 如果我们进行到这一步时某个地方出错,则重新显示表单
                return View(model);
            }第三,此方法是可以实现的 他真实调用的是 继承该接口的一个类 当中的同名方法 
    楼上有的朋友说是多态 可是我记得 多态是类与类之间的 
    母类定义虚方法 派生类定义重写方法  母类定义对象 =new 子类 这样实现的把
    也许是我概念混淆了  希望高手们 指点我一下 这个原理是怎么实现的 或者给我个资料看看 或者 告诉我原因 我去查资料也行 多谢了
    public class AccountMembershipService : IMembershipService
        {
      public bool ValidateUser(string userName, string password)
            {
                if (String.IsNullOrEmpty(userName)) throw new ArgumentException("值不能为 null 或为空。", "userName");
                if (String.IsNullOrEmpty(password)) throw new ArgumentException("值不能为 null 或为空。", "password");            return _provider.ValidateUser(userName, password);
            }
    }
      

  7.   

    我都不明白你在说什么...看一下4楼的代码,然后自己在Controller把那些代码找出来。
      

  8.   

    lz 读死书了吧IFormsAuthenticationService FormsService = new FormsAuthenticationService();
    这个实例化的不是IFormsAuthenticationService ,而是FormsAuthenticationService这个类接口的作用就是修改方便外部的流程是根据IFormsAuthenticationService设计的,所以即使换一个子类也不需要修改
      

  9.   

    [Quote=引用 14 楼 ltcszk 的回复:]lz 读死书了吧IFormsAuthenticationService FormsService = new FormsAuthenticationService();
    这个实例化的不是IFormsAuthenticationService ,而是FormsAuthenticationService这个类接口的作用就是修改方便
    [\\<hr/>Quote]
    ===========
    接口可以实现的嘛!
    外部的流程是根据IFormsAuthenti……
      

  10.   

    楼主没有看到 初始化吗?protected override void Initialize(RequestContext requestContext)
            {
                if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
                if (MembershipService == null) { MembershipService = new AccountMembershipService(); }            base.Initialize(requestContext);
            }