ASP.net MVC中实现N层体系结构的问题

时间:2015-11-09 11:01:58

标签: c# asp.net-mvc architecture n-tier-architecture

我想在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; }
}

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;}
    }
}

通用存储库

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;
    }
}

员工控制人员与基地(服务)有相容性

public class StaffController : BaseControlle
{
    public StaffController(IStaffService service) : base(service)
    {
       // **I have Problem here with base(service)
    }
}

我的代码中是否还有其他问题?我可以信任这种架构来开发企业解决方案吗?

1 个答案:

答案 0 :(得分:1)

回答代码中的问题:
您需要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)
    {
    }
}