-2

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?

Mironline
  • 2,755
  • 7
  • 35
  • 61
  • You might also want to follow the [stairway pattern](http://stackoverflow.com/questions/29259414/stairway-pattern-implementation), i.e. separation of interfaces and concrete classes. Also, at enterprise level, you probably want to follow the [SOLID principles](https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)) – Corak Nov 09 '15 at 11:26

1 Answers1

1

To answer the question in your code:
You need to make BaseController generic:

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)
    {
    }
}
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443