为了让我自己更容易对我的应用程序进行单元测试,我试图通过将我的数据访问功能转移到一个单独的存储库类来实现Repository模式。
我的存储库中的数据访问类:
public class ErrorRepository : IErrorRepository
{
public ErrorModel Errors { get; set; }
public List<ErrorModel> ErrorList { get; set; }
public List<ErrorModel> GetErrors()
{
string cs = "some path";
using (SQLiteConnection con = new SQLiteConnection(cs))
{
var listOfErrors = new List<ErrorModel>();
string stm = "SELECT * FROM Error WHERE Checked == 'False'";
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
listOfErrors.Add(new ErrorModel
{
Id = rdr["ID"].ToString()
});
}
rdr.Close();
ErrorList = listOfErrors;
}
}
con.Close();
}
return ErrorList;
}
}
public interface IErrorRepository
{
List<ErrorModel> GetErrors();
}
我的控制器:
public class ErrorController : Controller
{
private IErrorRepository _errorRepository;
public ErrorController(IErrorRepository errorRepository)
{
_errorRepository = errorRepository;
}
public ActionResult Error(int? page)
{
var errors = _errorRepository.GetErrors();
//// stuff for paging
int pageSize = 10;
int pageNumber = (page ?? 1); // if there is no page, return page 1
return View(errors.ToPagedList(pageNumber, pageSize));
}
}
但问题是,控制器永远不会被调用。
我已从先前版本更改了控制器负责所有内容的版本。所以我所做的就是将访问的数据移到一个单独的类中。 Error视图与以前相同。我是否错过了实施中的一些细节?
更新:
我对构造函数进行了一些调整,我有几个问题。为什么我在构造函数中注入它的代码不起作用:
public ErrorController(IErrorRepository _errorRepository)
{
this._errorRepository = _errorRepository;
}
但是,如果我将其更改为此,控制器将被调用,一切似乎都正常工作:
public ErrorController()
{
_errorRepository = new ErrorRepository();
}
但是最后一个例子不是一个糟糕的做法,因为ErrorController仍然与ErrorRepository紧密结合?
更新2:
我制作了这个自定义控制器工厂:
public class ControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
try
{
if (controllerType == null)
{
throw new ArgumentNullException("controllerType");
}
if (!typeof (IController).IsAssignableFrom(controllerType))
{
throw new ArgumentException(string.Format("Type requested is not a controller: {0}", controllerType.Name), "controllerType");
}
return MvcUnityContainer.Container.Resolve(controllerType) as IController;
}
catch (Exception)
{
return null;
}
}
}
public static class MvcUnityContainer
{
public static UnityContainer Container { get; set; }
}
这个bootstrapper类设置所有依赖项:
public class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
container.RegisterType<IErrorRepository, ErrorRepository>();
MvcUnityContainer.Container = container;
return container;
}
}
然后我在Global.asax文件中初始化:
// Initialise IoC container
Bootstrapper.Initialise();
// Register custom controller factory
ControllerBuilder.Current.SetControllerFactory(typeof(ControllerFactory));
现在我可以使用构造函数注入,从而将ErrorController与ErrorRepository分离:
public ErrorController(IErrorRepository errorRepository)
{
this.errorRepository = errorRepository;
}