I wanna implement correct n-tier architecture in asp.net mvc but have some problem on that [staffController
].
here is my code :
Domain
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; }
}
ViewModel
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;}
}
}
Generic Repository
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
}
}
}
Service
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);
}
}
Staff Service
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;
}
}
Base Controller
public class BaseControlle
{
private readonly IService<IEntity, IViewModel> _service;
public BaseControlle(IService<IEntity,IViewModel> service )
{
_service = service;
}
}
Staff Controller have poroblem with base(service)
public class StaffController : BaseControlle
{
public StaffController(IStaffService service) : base(service)
{
// **I have Problem here with base(service)
}
}
Is there any other problems in my code ? can I trust this architecture for developing enterprise solutions?