Using StructureMap with the ASP.NET MVC framework
ÔÎĵØÖ·:http://www.bengtbe.com/blog/post/2009/02/27/Using-StructureMap-with-the-ASPNET-MVC-framework.aspx
Using StructureMap with the ASP.NET MVC framework
2009-02-27 ÐÇÆÚÎå ×÷Õß:bengtbe
ÔÚÕâÀïÎÒÒª²ûÊöµÄÊÇÔõôÓÃ.net frameword ¿ò¼ÜϵÄStructureMapÕâ¸öÀà.
ÔÚѧϰÕâ¸öÖ®¼äÄãÐèÒª¶ÔASP.NET MVC¿ò¼Ü£¬ÒÀÀµ×¢ÈëºÍ¿ØÖÆ·´×ªÓлù±¾µÄÁ˽⡣
ÕâЩ¼¼ÊõÒ²²»Ö»ÓÃÓÚ StructureMap £¬Èç¹ûÄãϲ»¶ÕâЩ£¬ÄÇôÄ㵱ȻҲÓÃÆäËüµÄDI/Ioc ¹¤¾ßÁË¡£
ÎÒÃǾÍÓÃÕâ¸öUserController Àý×Ó»¹ËµÆð£¬ËüÓÐÒ»¸öÊôÐÔIUserService À´µ÷ÓÿØÖƲãµÄ·þÎñ£¬µ±
È»IUserService ±¾ÉíÒ²»áµ÷Óà IUserPepository Õâ¸öÔÚÊý¾Ý²ãµÄ·½·¨¡£Õâ¸öUserController ÓÃ
¿ØÖÆ·´×ªÀ´·ÃÎÊIUserService £¬Õâ¾ÍÊÇ˵Õâ¸öUserService Àà¾ÍÊÇÑ¡ÓÿØÖÆ·´×ªÀ´×¢Èë
UserController ʵÀý»¯IUserService;
public class UserController : Controller
{
private readonly IUserService m_UserService;
public UserController(IUserService userService)
{
m_UserService = userService;
}
public ActionResult Edit(int id)
{
return View(m_UserService.GetById(id));
}
}
Õâ¾Í½ÐÈÝÆ÷×¢Èë,Èç¹ûÄã¾ÍÕâÑùд¶øûÓÐдÆäËüµÄÅäÖÃÎļþÄÇôÄ㽫µÃµ½ÈçϵĴíÎó£º
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the error and where
it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined
for this object.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ÆäʵĬÈϵģ¬Õâ¸öASP.NET MVC framework ÊÇÐèÒªÒ»¸ö¹¹Ô캯ÊýµÄ£¬Ò»ÖÖ·½·¨Êǹ¹ÔìÐÎʽÈçÏ£º
public UserController() : this(new UserService(new UserRepository()))
{
}
ÉÏÃæµÄ´úÂëÔËÐÐʱ¾ÍÊÇ
Ïà¹ØÎĵµ£º
ת£ºhttp://www.cnblogs.com/kumaws/articles/web_architecture.html
Ïà¶ÔÓÚ²»Í¬µÄÐèÇó£¬ÍøÕ¾µÄ´óС£¬¿ª·¢¸´ÔӶȣ¬³É±¾£¬ÍøÕ¾µÄ¼Ü¹¹Ò²·ÖÐí¶àÖÖ¡£
´ó¶àÊýСÐÍÍøÕ¾£¬ÐèÇó½ÏΪ¼òµ¥£¬°²È«ÐÔÒªÇ󲻸ߣ¬¿ª·¢Ê±¼ä¶Ì¡£Ò»°ãÕâÑùµÄÍøÕ¾£¬»¹»áÓÐÒ»¸öÓëÊý¾Ý¿âÖ®¼äµÄ½»»¥¡£ÓÃAsp.NetÖÐÌṩµÄ¿Ø¼þ£¬ÍêÈ«¿ÉÒÔÂú×㿪·¢µÄÒªÇó¡£Ò»° ......
ASP.NETÎļþÏÂÔغ¯ÊýʹÓÃÊÇʲôÇé¿öÄØ£¿ÔÚÄãµÄPage_LoadÖÐÌí¼ÓÕâÑùµÄ´úÂë:
Page.Response.Clear();
bool success = ResponseFile(Page.Request, Page.Response, "Ä¿µÄÎļþÃû³Æ", @"Ô´Îļþ·¾¶", 1024000);
if (!success) Response.Write("ÏÂÔØÎļþ³ö´í£¡"); Page.Response.End();
ASP.NETÎļþÏÂÔغ¯Êý´úÂëΪ:
......
——ÓÃForm ±íµ¥ÈÏ֤ʵÏÖµ¥µãµÇ¼£¨Single Sign On£©
×÷Õߣºº®Óð·ã(cityhunter172)
µÚÈý²¿·Ö ʵÏÖµ¥µãµÇ¼£¨Single Sign On£©
“µÈÁ˺þÃÖÕÓڵȵ½½ñÌ죬дÁ˺þÃÖÕÓھͿìÍê½á£¬µ«ÊÇÍøÓѵķ´Ó¦È´ÈÃÎÒÓÐһЩµÄÉËÐÄ¡£ÅÎÁ˺þÃÖÕÓÚÅε½½ñÌ죬ÈÌÁ˺þÃÖÕÓÚ°Ñ´ËÎÄ׫д£¬ÄÇЩÊÜÀäÂäµÄÎÞÄÎÔç¾ÍÎÞËùν£¬ÀÛÒ² ......
1.»ñÈ¡Return·µ»ØÖµ
³ÌÐò´úÂë//´æ´¢¹ý³Ì
//Create PROCEDURE MYSQL
// @a int,
// @b int
//AS
// return @a + @b
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToStri ......
ʲôÊÇMVCģʽ
MVC£¨Model-View-Controller£¬Ä£ÐÍ—ÊÓͼ—¿ØÖÆÆ÷ģʽ£©ÓÃÓÚ±íʾһÖÖÈí¼þ¼Ü¹¹Ä£Ê½¡£Ëü°ÑÈí¼þϵͳ·ÖΪÈý¸ö»ù±¾²¿·Ö£ºÄ£ÐÍ£¨Model£©£¬ÊÓͼ£¨View£©ºÍ¿ØÖÆÆ÷£¨Controller£©¡£
ÄÇôMVCģʽºÍÎÒÃÇÊìϤµÄWebFormģʽÓÐʲô²»Í¬ÄØ£¿ËûµÄ¸÷¸ö²¿·ÖÓÖÊÇÔõÑù·Ö¹¤µÄÄØ£¿
ÎÒÃÇÏÈÀ´¿´Ò»ÏÂÆÕͨµÄWebFormģʽÏ£ ......