using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft.Practices.Unity; using MapleStory2.IDAL.Account; using MapleStory2.Model; using MapleStory2.Model.Models; namespace MapleStory2.Controllers { public class RegisterController : Controller { [Dependency] public IRegisterService registerService { get; set; } // // GET: /Register/ public ActionResult Index() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Register(UserQueryData user) { if (ModelState.IsValid) { bool flag=registerService.AddNormalUser(user); //添加普通用户 //int count = 0; //using (maplestory2Context mc = new maplestory2Context()) //{ // string userId = Guid.NewGuid().ToString(); // //往用户表里插入数据 // mc.Users.Add(new User // { // ID = userId, // UserName = user.UserName, // Password = user.Password, // NickName = user.UserName, // RegDate = DateTime.Now, // SortCode = 0 // }); // //count = mc.SaveChanges(); // //往用户详情表里插入数据 // mc.UserDetails.Add(new UserDetail() // { // DetailID = Guid.NewGuid().ToString(), // DetailType = 0, // BornLocation = user.BornLocation, // UserID = userId, // UserName = user.UserName // }); // count = mc.SaveChanges(); //} //if (count > 0) //{ // ModelState.Clear(); // user = null; // ViewBag.Message = "注册成功"; //} //if (flag) //{ // ModelState.Clear(); // user = null; // ViewBag.Message = "注册成功"; //} } return View("Index"); } } }
其中UserQueryData有一部分值是空的,有一部分是我前台VIEW中填写的数据。
是说 registerService 为 Null 吧!
这代码不是你写的吧!
是我写的啊。。。。。。
RegisterService怎么会为NULL呢?
这明明就是一个接口啊,而且我们公司的代码都是这么写的啊。
下面是接口层:
namespace MapleStory2.IDAL.Account { public interface IRegisterService:IBaseRepositoryService { bool AddNormalUser(UserQueryData user); } }
下面是服务层
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MapleStory2.IDAL.Account; using MapleStory2.Model; using MapleStory2.Model.Models; namespace MapleStory2.DAL.Account { public class RegisterService:BaseRepositoryService,IRegisterService { public bool AddNormalUser(UserQueryData user) { int count = 0; using (maplestory2Context mc = new maplestory2Context()) { string userId = Guid.NewGuid().ToString(); //往用户表里插入数据 mc.Users.Add(new User { ID=userId, UserName=user.UserName, Password=user.Password, NickName=user.UserName, RegDate=DateTime.Now, SortCode=0 }); //count = mc.SaveChanges(); //往用户详情表里插入数据 mc.UserDetails.Add(new UserDetail() { DetailID=Guid.NewGuid().ToString(), DetailType=0, BornLocation=user.BornLocation, UserID=userId, UserName=user.UserName }); count = mc.SaveChanges(); } if (count > 0) { return true; } else { return false; } } } }
@KMSFan:
[Dependency]
public IRegisterService registerService { get; set; }
知道我为啥说不是你写的了么?因为你不知道 registerService 是一个依赖属性。注意 [Dependency] 特性,你们的项目采用了 DI(依赖注入)。
@Launcher:
原来如此,那我该怎么改呢,谢谢大神!真的感觉有点一头雾水啊
@KMSFan: 看你代码开头的引用 using Microsoft.Practices.Unity,表明你们的项目使用的是 Unity 的依赖注入容器,那么找你们写此项目的人给你讲一下,或者上网搜篇文章学习下。我就不在这里给你讲课了。
@Launcher: 好的,谢谢大神了
where you set the value for property registerService?
不知道啊,公司的项目都是这么写的,请问大侠我该怎么改?= =
@KMSFan:
I found the Dependency Attribute for property registerService, perhaps, you are using the Unity system, you can question to your members, thoes will tell you how to do.
@519740105: sure
@KMSFan: perhaps, you lost the regist for service iregisterservice or you does not config the register for service iregisterservice.
@519740105:
hi >-<,I have already used the DI Container,but my target will use the Dependency annotation to implement the DI. I don't wanna to write the similar code every place when I have to use DI.
I mean just
[Dependency]
public InterfaceName ins{get ;set;}
it is the fact I fingure above out.
thank u so much .