如何使用依赖关系来模拟数据。 MVC5

时间:2015-05-28 16:59:14

标签: c# jquery asp.net-mvc

我是MVC5的新手,我正在尝试搜索'搜索'功能。我的目标是从数据服务中获取数据。(即,我将数据输入到表单中并点击搜索按钮,如果它是显示数据的记录)。

我创建了一个依赖来模拟数据(虚拟数据)。如何将代码连接到控制器以实现我的目的?请指教谢谢。

继承我的控制器和我的模拟器:

public class SearchController : Controller
{
    private readonly ISearchResultsService _resultsService;

    public SearchController() : this(DependencyFactory.NewResultsService())
    {         

    }

    public SearchController(ISearchResultsService resultsService)
    {
        _resultsService = resultsService;
    }

    // GET:Search
    public ActionResult Index()
    {
        return View();
    }

    // GET: Search/Details/5
    public ActionResult Details(int id)
    {
        return View();
    }

    // GET: Search/Create
    public ActionResult Create()
    {
        return View();
    }

    // POST: Search/Create
    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            // TODO: Add insert logic here

            return();
        }
        catch
        {
            return View();
        }
    }

    //[HttpPost]
    //public ActionResult Index(SearchCriteria data)
    //{
    //    var data = this._resultsService.FindClaims(data);
    //    return View(data);
    //}

    [HttpPost]
    public ActionResult Index()
    {

        return View();


    }

    // GET: Search/Edit/5
    public ActionResult Edit(int id)
    {
        return View();
    }

    // POST: Search/Edit/5
    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {
        try
        {
            // TODO: Add update logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    // GET: Search/Delete/5
    public ActionResult Delete(int id)
    {
        return View();
    }

    // POST: Search/Delete/5
    [HttpPost]
    public ActionResult Delete(int id, FormCollection collection)
    {
        try
        {
            // TODO: Add delete logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
}

public static class DependencyFactory
{
    public static ISearchResultsService NewResultsService()
    {
        return new MockSearchResultsService();
    }
}

public interface ISearchResultsService
{
    List<Person> FindClaims(string firstName, string lastName);
}

public class MockSearchResultsService : ISearchResultsService
{
    public List<Person> FindClaims(string firstName, string lastName)
    {
        return new List<Person>(new []{new Person{FirstName = "John", LastName = "Doe"}});//throw new NotImplementedException();
    }
}

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

}

1 个答案:

答案 0 :(得分:0)

我建议您使用http://autofac.org之类的 IoC容器。您可以像这样配置容器并注册服务:

builder.RegisterType<MockSearchResultsService>().As<ISearchResultsService>()

所有依赖项都将自动解决:

public class SearchController : Controller
{
    private readonly ISearchResultsService _resultsService;

    public SearchController(ISearchResultsService resultsService)
    {         
        _resultsService = resultsService;
    }
}

请注意,您可以使用许多IoC容器。尝试更多地搜索 AutoFac Ninject Castle Windsor