在 ASP.net MVC 中实现 N 层架构的问题
Issue on Implementing of N-Tier Architecture in ASP.net MVC
我想在 asp.net mvc 中实现正确的 n 层架构,但在 [staffController
] 上有一些问题。
这是我的代码:
域
public interface IEntity {
int Id { set; get; } // all entities have a ID property
}
public interface IStaff : IEntity {
string Name { set; get; } // staff entity has NANE
}
public class Staff : IStaff {
public int Id { get; set; }
public string Name { get; set; }
}
视图模型
public interface IViewModel {
int Id { set; get; } // based on IEntity , all ViewModel have ID too
}
public interface IStaffViewModel : IViewModel {
string Name { set; get; }
string NameId { get; }
}
public class StaffViewModel : IStaffViewModel {
public int Id { get; set; }
public string Name { get; set; }
public string NameId
{
get { return "Name" + Id;}
}
}
通用存储库
public interface IRepository<TEntity, out TViewModel>
where TEntity : IEntity
where TViewModel : IViewModel {
TViewModel Load(int id);
}
public class Repository : IRepository<IEntity,IViewModel>
{
public IViewModel Load(int id)
{
// load from database -> Map entity to Viewmodel and post ViewModel
}
}
}
服务
public interface IService<in TEntity, out TViewModel>
where TEntity : IEntity
where TViewModel : IViewModel {
TViewModel LoadEntity(int id);
}
public class Service<TEntity, TViewModel>
: IService<TEntity , TViewModel>
where TEntity : IEntity
where TViewModel : IViewModel {
private readonly IRepository<TEntity, TViewModel> _repository;
public Service(IRepository<TEntity,TViewModel> repository )
{
_repository = repository;
}
public TViewModel LoadEntity(int id)
{
return _repository.Load(id);
}
}
员工服务
public interface IStaffService : IService<IStaff,IStaffViewModel> { }
public class StaffService : Service<IStaff,IStaffViewModel>, IStaffService
{
private readonly IRepository<IStaff, IStaffViewModel> _repository;
public StaffService(IRepository<IStaff, IStaffViewModel> repository) : base(repository)
{
_repository = repository;
}
}
基地控制器
public class BaseControlle
{
private readonly IService<IEntity, IViewModel> _service;
public BaseControlle(IService<IEntity,IViewModel> service )
{
_service = service;
}
}
Staff Controller 基地(服务)有问题
public class StaffController : BaseControlle
{
public StaffController(IStaffService service) : base(service)
{
// **I have Problem here with base(service)
}
}
我的代码还有其他问题吗?我可以信任这种用于开发企业解决方案的架构吗?
回答您代码中的问题:
您需要使 BaseController
通用:
public class BaseController<TEntity, TViewModel>
{
private readonly IService<TEntity, TViewModel> _service;
public BaseControlle(IService<TEntity, TViewModel> service)
{
_service = service;
}
}
public class StaffController : BaseController<IStaff, IStaffViewModel>
{
public StaffController(IStaffService service) : base(service)
{
}
}
我想在 asp.net mvc 中实现正确的 n 层架构,但在 [staffController
] 上有一些问题。
这是我的代码:
域
public interface IEntity {
int Id { set; get; } // all entities have a ID property
}
public interface IStaff : IEntity {
string Name { set; get; } // staff entity has NANE
}
public class Staff : IStaff {
public int Id { get; set; }
public string Name { get; set; }
}
视图模型
public interface IViewModel {
int Id { set; get; } // based on IEntity , all ViewModel have ID too
}
public interface IStaffViewModel : IViewModel {
string Name { set; get; }
string NameId { get; }
}
public class StaffViewModel : IStaffViewModel {
public int Id { get; set; }
public string Name { get; set; }
public string NameId
{
get { return "Name" + Id;}
}
}
通用存储库
public interface IRepository<TEntity, out TViewModel>
where TEntity : IEntity
where TViewModel : IViewModel {
TViewModel Load(int id);
}
public class Repository : IRepository<IEntity,IViewModel>
{
public IViewModel Load(int id)
{
// load from database -> Map entity to Viewmodel and post ViewModel
}
}
}
服务
public interface IService<in TEntity, out TViewModel>
where TEntity : IEntity
where TViewModel : IViewModel {
TViewModel LoadEntity(int id);
}
public class Service<TEntity, TViewModel>
: IService<TEntity , TViewModel>
where TEntity : IEntity
where TViewModel : IViewModel {
private readonly IRepository<TEntity, TViewModel> _repository;
public Service(IRepository<TEntity,TViewModel> repository )
{
_repository = repository;
}
public TViewModel LoadEntity(int id)
{
return _repository.Load(id);
}
}
员工服务
public interface IStaffService : IService<IStaff,IStaffViewModel> { }
public class StaffService : Service<IStaff,IStaffViewModel>, IStaffService
{
private readonly IRepository<IStaff, IStaffViewModel> _repository;
public StaffService(IRepository<IStaff, IStaffViewModel> repository) : base(repository)
{
_repository = repository;
}
}
基地控制器
public class BaseControlle
{
private readonly IService<IEntity, IViewModel> _service;
public BaseControlle(IService<IEntity,IViewModel> service )
{
_service = service;
}
}
Staff Controller 基地(服务)有问题
public class StaffController : BaseControlle
{
public StaffController(IStaffService service) : base(service)
{
// **I have Problem here with base(service)
}
}
我的代码还有其他问题吗?我可以信任这种用于开发企业解决方案的架构吗?
回答您代码中的问题:
您需要使 BaseController
通用:
public class BaseController<TEntity, TViewModel>
{
private readonly IService<TEntity, TViewModel> _service;
public BaseControlle(IService<TEntity, TViewModel> service)
{
_service = service;
}
}
public class StaffController : BaseController<IStaff, IStaffViewModel>
{
public StaffController(IStaffService service) : base(service)
{
}
}