????????Asp.net webapi ??????????????????????????????????????????????????????2????????????????????????????????д?????????????????2?????????????IOC????????????????Controller???弰???
public class ArticlesController : ApiController
{
private IArticleService _articleService;
public ArticlesController(IArticleService articleService)
{
_articleService = articleService;
}
// GET: api/Articles
public IEnumerable<Article> GetArticles()
{
return _articleService.GetArticles();
}
// GET: api/Articles/5
[ResponseType(typeof(Article))]
public IHttpActionResult GetArticle(int id)
{
Article article = _articleService.GetArticle(id);
if (article == null)
{
return NotFound();
}
return Ok(article);
}
// PUT: api/Articles/5
[ResponseType(typeof(void))]
public IHttpActionResult PutArticle(int id?? Article article)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != article.ID)
{
return BadRequest();
}
try
{
_articleService.UpdateArticle(article);
}
catch (DbUpdateConcurrencyException)
{
if (!ArticleExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Articles
[ResponseType(typeof(Article))]
public IHttpActionResult PostArticle(Article article)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_articleService.CreateArticle(article);
return CreatedAtRoute("DefaultApi"?? new { id = article.ID }?? article);
}
// DELETE: api/Articles/5
[ResponseType(typeof(Article))]
public IHttpActionResult DeleteArticle(int id)
{
Article article = _articleService.GetArticle(id);
if (article == null)
{
return NotFound();
}
_articleService.DeleteArticle(article);
return Ok(article);
}
private bool ArticleExists(int id)
{
return _articleService.GetArticle(id) != null;
}
}
??????????????? ???IArticleService???????Controller??action?н?????????service??????????????????????controller??????????????????????????????????????????Controller?????????????????????????????????Helper???棬????Helper????????????
????IOC??????
???????????????Unity?? ??????????????Install-Package Unity.WebApi.5.1
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
//Install-Package Unity.WebApi.5.1
IUnityContainer container = new UnityContainer()
.RegisterType<IArticleService?? ArticleService>()
.RegisterType<IBlogService?? BlogService>();
//GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator)?? new UnityHttpControllerActivator(container));
}
}
public class UnityHttpControllerActivator : IHttpControllerActivator
{
public IUnityContainer UnityContainer { get; private set; }
public UnityHttpControllerActivator(IUnityContainer unityContainer)
{
this.UnityContainer = unityContainer;
}
public IHttpController Create(HttpRequestMessage request?? HttpControllerDescriptor controllerDescriptor?? Type controllerType)
{
return (IHttpController)this.UnityContainer.Resolve(controllerType);
}
}
????????????controller?????DependencyResolver??IHttpControllerActivator ????????????
??????????????????Moq ??nunit ????????? Install-Package Moq ?? Install-Package NUnit