Database Reference
In-Depth Information
6.
Create the IReservationContext interface in Listing 8-14. We'll use this interface to help us
test against a fake DbContext so that changes are not saved to the real database.
Listing 8-14. Use this IReservationContext to Define the Methods You'll Need from the DbContext
public interface IReservationContext : IDisposable
{
IDbSet<Train> Trains { get; }
IDbSet<Schedule> Schedules { get; }
IDbSet<Reservation> Reservations { get; }
int SaveChanges();
}
7.
The POCO template generates both the POCO classes and the class that implements the
object context. We'll need this object context class to implement the IReservationContext
interface. To do this, edit the Recipe8.Context.tt template file and add
IReservationContext at the end of the line that generates the name of the object context
class. The complete line should look like the following:
<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> :
DbContext,IReservationContext
8.
Create the repository class in Listing 8-15. This class takes an IReservationContext in
the constructor.
Listing 8-15. The ReservationRepository Class That Takes an IReservationContext in the
Constructor
public class ReservationRepository: IDisposable
{
private IReservationContext _context;
public ReservationRepository(IReservationContext context)
{
if (context == null)
throw new ArgumentNullException("context is null");
_context = context;
}
public void AddTrain(Train train)
{
_context.Trains.Add(train);
}
public void AddSchedule(Schedule schedule)
{
_context.Schedules.Add(schedule);
}
public void AddReservation(Reservation reservation)
{
_context.Reservations.Add(reservation);
}
 
Search WWH ::




Custom Search