我用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时 自带的 不明白啊
在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时 自带的 不明白啊
{ 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);
}
}
我也刚做MVC不长时间,前一阵也研究了一下。
每个类库,都有对应该类库的一个文件
类库名Module.cs 文件,你看看有没有,就只该类库下第一级菜单里。这里边有方法public void RegisterWithContainer()
{
container
.RegisterType<IFilesService, FilesService>(new ContainerControlledLifetimeManager())
.RegisterType<IFilesRepository, FilesRepository>(new ContainerControlledLifetimeManager());
}我感觉,掉接口方法的时候,是通过它,来反射某一个接口对应的类的方法。
我公司是这么个情况。
这是我个人的理解。仅供参考。
首先看这个第一个代码 这个是定义属性 然后返回值 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);
}
}
这个实例化的不是IFormsAuthenticationService ,而是FormsAuthenticationService这个类接口的作用就是修改方便外部的流程是根据IFormsAuthenticationService设计的,所以即使换一个子类也不需要修改
这个实例化的不是IFormsAuthenticationService ,而是FormsAuthenticationService这个类接口的作用就是修改方便
[\\<hr/>Quote]
===========
接口可以实现的嘛!
外部的流程是根据IFormsAuthenti……
{
if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
if (MembershipService == null) { MembershipService = new AccountMembershipService(); } base.Initialize(requestContext);
}