C# ASP.NET MVC 2中基本控制器的实现
我正在使用ASP.NETMVC2。我从Darin(似乎能回答所有MVC问题的人)那里得到了示例应用程序。我不确定是否有其他人有他创建的这个示例项目?他的应用程序中有一个基本控制器,代码如下所示:C# ASP.NET MVC 2中基本控制器的实现,c#,asp.net-mvc,asp.net-mvc-2,dependency-injection,autofac,C#,Asp.net Mvc,Asp.net Mvc 2,Dependency Injection,Autofac,我正在使用ASP.NETMVC2。我从Darin(似乎能回答所有MVC问题的人)那里得到了示例应用程序。我不确定是否有其他人有他创建的这个示例项目?他的应用程序中有一个基本控制器,代码如下所示: public abstract class BaseController<TRepository> : Controller, IModelMapperController { protected BaseController(TRepository repository, IMapp
public abstract class BaseController<TRepository> : Controller, IModelMapperController
{
protected BaseController(TRepository repository, IMapper mapper)
{
Repository = repository;
ModelMapper = mapper;
}
public TRepository Repository { get; private set; }
public IMapper ModelMapper { get; private set; }
}
public class UsersController : BaseController<UserRepository>
{
public UsersController()
: base(new UserRepository(), new UserModelMapper())
{
// do stuff
}
public ActionResult Index()
{
// now you can use syntax like, since "Repository" is type "UserRepository"
return View(Respository.GetAllUsers());
}
public ActionResult Details(int id)
{
return View(Respository.GetUser(id));
}
}
公共抽象类BaseController:Controller,IModelMapperController
{
受保护的BaseController(存储库、IMapper映射器)
{
存储库=存储库;
ModelMapper=mapper;
}
公共存储库{get;private set;}
公共IMapper模型映射器{get;private set;}
}
关于这段代码,我的问题是BaseController是什么意思
如果我需要指定多个存储库,BaseController构造函数代码会是什么样子
每个控制器只有一个存储库是最佳做法吗?我之所以问这个问题,是因为在ApplicationController类中,我使用Create()操作方法从自定义视图模型填充视图。在这个视图模型中,我需要填充两个不同的下拉列表。银行名称和账户类型。每个都有自己的存储库。BankRepository有一个名为GetBanks()的方法,AccountTypeRepository有一个名为GetAccountTpes()的方法。因此,在我的应用程序控制器中,我必须调用这两个方法来填充下拉列表。因此,应用程序控制器实现基本控制器。如果我必须将基本控制器传递给多个存储库,它会是什么样子
请有人解释一下这件事
@达林:谢谢你的示例应用程序,我已经从中学到了很多。所以你的每个控制器都可以指向不同的回购协议。大概是这样的:
public abstract class BaseController<TRepository> : Controller, IModelMapperController
{
protected BaseController(TRepository repository, IMapper mapper)
{
Repository = repository;
ModelMapper = mapper;
}
public TRepository Repository { get; private set; }
public IMapper ModelMapper { get; private set; }
}
public class UsersController : BaseController<UserRepository>
{
public UsersController()
: base(new UserRepository(), new UserModelMapper())
{
// do stuff
}
public ActionResult Index()
{
// now you can use syntax like, since "Repository" is type "UserRepository"
return View(Respository.GetAllUsers());
}
public ActionResult Details(int id)
{
return View(Respository.GetUser(id));
}
}
公共类用户控制器:BaseController
{
公共用户控制器()
:base(新建UserRepository(),新建UserModelMapper())
{
//做事
}
公共行动结果索引()
{
//现在您可以使用如下语法,因为“Repository”的类型是“UserRepository”
返回视图(Respository.GetAllUsers());
}
公共行动结果详细信息(int id)
{
返回视图(Respository.GetUser(id));
}
}
更新地址
public class AddressesController : BaseController<AddressRepository>
{
public AddressesController()
: base(new AddressRepository(), new AddressModelMapper())
{
}
public ActionResult Addresses(int id)
{
return View(Respository.GetAllByUserId(id));
}
}
公共类地址控制器:BaseController
{
公共地址控制器()
:base(新的AddressRepository(),新的AddressModelMapper())
{
}
公共操作结果地址(int id)
{
返回视图(Respository.GetAllByUserId(id));
}
}
为工厂更新
public static class RepositoryFactory
{
internal static Hashtable Repositories = new Hashtable();
internal static T GetRepository<T>() where T : class, new()
{
if (Repositories[typeof(T)] as T == null)
{
Repositories[typeof(T)] = new T();
}
return Repositories[typeof(T)] as T;
}
public static AccountTypeRepository AccountTypeRepository
{
get { return GetRepository<AccountTypeRepository>(); }
}
public static BankRepository BankRepository
{
get { return GetRepository<BankRepository>(); }
}
/* repeat as needed or change accessibility and call GetRepository<> directly */
公共静态类RepositoryFactory
{
内部静态哈希表存储库=新哈希表();
内部静态T GetRepository(),其中T:class,new()
{
if(存储库[typeof(T)]作为T==null)
{
存储库[typeof(T)]=新的T();
}
将存储库[typeof(T)]返回为T;
}
公共静态AccountTypeRepository AccountTypeRepository
{
获取{return GetRepository();}
}
公共静态银行存储库银行存储库
{
获取{return GetRepository();}
}
/*根据需要重复或更改可访问性并直接调用GetRepository*/
现在,您甚至不必使用BaseController,只需编写以下内容:
public class ApplicationModel
{
public Application Application { get; set; }
public IList<Bank> Banks { get; set; }
public IList<AccountType> AccountTypes { get; set; }
}
public class ApplicationController : Controller
{
public ActionResult Index()
{
ApplicationListModel model = new ApplicationListModel()
{
Applications = RespositoryFactory.ApplicationRepository.GetAll();
}
return View(model);
}
public ActionResult Details(int id)
{
ApplicationModel model = new ApplicationModel()
{
Application = RespositoryFactory.ApplicationRepository.Get(id),
Banks = RespositoryFactory.BankRepository.GetAll(),
AccountTypes = RespositoryFactory.AccountTypeRepository.GetAll()
}
return View(model);
}
}
公共类应用程序模型
{
公共应用程序应用程序{get;set;}
公共IList银行{get;set;}
公共IList帐户类型{get;set;}
}
公共类应用程序控制器:控制器
{
公共行动结果索引()
{
ApplicationListModel模型=新的ApplicationListModel()
{
Applications=RespositoryFactory.ApplicationRepository.GetAll();
}
返回视图(模型);
}
公共行动结果详细信息(int id)
{
ApplicationModel model=新的ApplicationModel()
{
Application=RespositoryFactory.ApplicationRepository.Get(id),
Banks=RespositoryFactory.BankRepository.GetAll(),
AccountTypes=RespositoryFactory.AccountTypeRepository.GetAll()
}
返回视图(模型);
}
}
这样,您的每个控制器都可以指向不同的回购。类似于以下内容:
public abstract class BaseController<TRepository> : Controller, IModelMapperController
{
protected BaseController(TRepository repository, IMapper mapper)
{
Repository = repository;
ModelMapper = mapper;
}
public TRepository Repository { get; private set; }
public IMapper ModelMapper { get; private set; }
}
public class UsersController : BaseController<UserRepository>
{
public UsersController()
: base(new UserRepository(), new UserModelMapper())
{
// do stuff
}
public ActionResult Index()
{
// now you can use syntax like, since "Repository" is type "UserRepository"
return View(Respository.GetAllUsers());
}
public ActionResult Details(int id)
{
return View(Respository.GetUser(id));
}
}
公共类用户控制器:BaseController
{
公共用户控制器()
:base(新建UserRepository(),新建UserModelMapper())
{
//做事
}
公共行动结果索引()
{
//现在您可以使用如下语法,因为“Repository”的类型是“UserRepository”
返回视图(Respository.GetAllUsers());
}
公共行动结果详细信息(int id)
{
返回视图(Respository.GetUser(id));
}
}
更新地址
public class AddressesController : BaseController<AddressRepository>
{
public AddressesController()
: base(new AddressRepository(), new AddressModelMapper())
{
}
public ActionResult Addresses(int id)
{
return View(Respository.GetAllByUserId(id));
}
}
公共类地址控制器:BaseController
{
公共地址控制器()
:base(新的AddressRepository(),新的AddressModelMapper())
{
}
公共操作结果地址(int id)
{
返回视图(Respository.GetAllByUserId(id));
}
}
为工厂更新
public static class RepositoryFactory
{
internal static Hashtable Repositories = new Hashtable();
internal static T GetRepository<T>() where T : class, new()
{
if (Repositories[typeof(T)] as T == null)
{
Repositories[typeof(T)] = new T();
}
return Repositories[typeof(T)] as T;
}
public static AccountTypeRepository AccountTypeRepository
{
get { return GetRepository<AccountTypeRepository>(); }
}
public static BankRepository BankRepository
{
get { return GetRepository<BankRepository>(); }
}
/* repeat as needed or change accessibility and call GetRepository<> directly */
公共静态类RepositoryFactory
{
内部静态哈希表存储库=新哈希表();
内部静态T GetRepository(),其中T:class,new()
{
if(存储库[typeof(T)]作为T==null)
{
存储库[typeof(T)]=新的T();
}
将存储库[typeof(T)]返回为T;
}
公共静态AccountTypeRepository AccountTypeRepository
{
获取{return GetRepository();}
}
公共静态银行存储库银行存储库
{
获取{return GetRepository();}
}
/*根据需要重复或更改可访问性并直接调用GetRepository*/
现在,您甚至不必使用BaseController,只需编写以下内容:
public class ApplicationModel
{
public Application Application { get; set; }
public IList<Bank> Banks { get; set; }
public IList<AccountType> AccountTypes { get; set; }
}
public class ApplicationController : Controller
{
public ActionResult Index()
{
ApplicationListModel model = new ApplicationListModel()
{
Applications = RespositoryFactory.ApplicationRepository.GetAll();
}
return View(model);
}
public ActionResult Details(int id)
{
ApplicationModel model = new ApplicationModel()
{
Application = RespositoryFactory.ApplicationRepository.Get(id),
Banks = RespositoryFactory.BankRepository.GetAll(),
AccountTypes = RespositoryFactory.AccountTypeRepository.GetAll()
}
return View(model);
}
}
公共类应用程序模型
{
公共应用程序应用程序{get;set;}
公共IList银行{get;set;}
公共IList帐户类型{get;set;}
}
公共类应用程序控制器:控制器
{
P