title: Mvc4: N tier architecture
body: <p>Tools: Mvc4, Sql server, Nhibernate</p>
  
  <p>I am learning Ntier architecture and plan to learn this with a small example.  This will be a student registration application which will have a form for 
  a. first name
  b. last name
  c. address
  d. Student Id
  The application will be able to 
  a. Get student by Id
  b. Get all students
  c. Register new students/Save student
  d. Edit a student
  e. Delete a student</p>
  
  <p>I plan to have the following tiers</p>
  
  <p>Presentation layer (seperate project mvc 4 application)</p>
  
  <p>--- html for student form goes here. I can use jquery etc here
  --- my controller will call the service</p>
  
  <p>Service layer (seperate project : class library project. In this case only the web will be my client. I will learn to use webAPI or wcf for this later in another project)</p>
  
  <p>--- StudentService here</p>
  
  <p>--- IstudentService here</p>
  
  <p>Business layer : (seperate project : class library project)
  ??</p>
  
  <p>Data layer : (seperate project : class library project)
  ??</p>
  
  <p>Database : (sql server database)</p>
  
  <p>Now I got confused and my questions are:</p>
  
  <ol>
  <li><p>where will I create my student model (which layer ?)</p></li>
  <li><p>What will I be writing in my business layer for this student example I have.</p></li>
  <li><p>What will go in my data layer? Which methods will I be writing? Are they methods that will communicate
  with the database directly?</p>
  
  <p>Some examples will be great. I will look for a good IOC container.</p></li>
  </ol>
  
  <p>Here is sample code below:</p>
  
  <pre><code>public interface IStudentService
  
      {
          IEnumerable&lt;Student&gt; GetStudents();
  
          Student GetStudentById(int id);
  
          void CreateStudent(Student student);
  
          void UpdateStudent(Student student);
  
          void DeleteStudent(int id);
  
          void SaveStudent();
      }
  
  public class StudentService : IStudentService
      {
          private DataContext _datacontext;
          public StudentService()
          {
              _datacontext = new DataContext();
          }
  
          public IEnumerable&lt;Student&gt; GetStudents()
          {
              var students = _datacontext.Students;
              return students;
          }
  
          public Student GetStudentById(int id)
          {
              return _datacontext.Students.Find(id);
          }
  
          public void CreateStudent(Student student)
          {
              _datacontext.Students.Add(student);
              _datacontext.SaveChanges();
          }
  
          public void UpdateStudent(Student student)
          {
              _datacontext.Entry(student).State = EntityState.Modified;
              //_datacontext.Entry(student).State = EntityState.Modified;
              _datacontext.SaveChanges();
          }
  
          public void DeleteStudent(int id)
          {
              var student = _datacontext.Students.Find(id);
              _datacontext.Entry(student).State = EntityState.Deleted;
              _datacontext.SaveChanges();
          }
  
          public void SaveStudent()
          {
              _datacontext.SaveChanges();
          }
      }
  </code></pre>