-2
我想在asp.net mvc中實現正確的n層體系結構,但是在[staffController
]上有一些問題。關於在ASP.net中實現N層體系結構的問題MVC
這裏是我的代碼:
域
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;
}
}
員工控制器與基地(服務)poroblem
public class StaffController : BaseControlle
{
public StaffController(IStaffService service) : base(service)
{
// **I have Problem here with base(service)
}
}
有沒有在我的代碼的任何其他問題?我可以相信這個架構是用於開發企業解決方案嗎
您可能還需要遵守[樓梯模式](http://stackoverflow.com/questions/29259414/stairway-pattern-implementation),即接口和具體類的分離。另外,在企業級別,您可能需要遵循[SOLID原則](https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)) – Corak