0

I would like to wrap my context in an interface to allow for easier mocking with RhinoMocks.

//Instead of creating a concrete type I want to inject the interface
public Repository(ISessionService sessionService, IMyContext context)
{
    //This gets the correct connection string dynamically based on a dropdown selection
    var connectionString = CustomerConnection.GetCustomerConnection(sessionService.GetCustomerId());
    //see link below
    _context = new MyEntity(connectionString.ToEntityConnectionString(typeof(MyEntity)));
}

This converts an ADO.Net connection string to an EF connections string

This is custom code to extend the generated entity I get from the DB first model. It just allows me to pass a connection string instead of use the one that was created by the designer. The reason for using this is we use a model table that all our tables are based on.

public partial class MyEntity : IMyContext
{
    public MyEntity(string connectionString) : base(connectionString){}
}

With Ninject I think it will look something like this:

kernel.Bind<IMyContext>().To<MyEntity>().InRequestScope().WithConstructorArgument("connectionString", connectionString);

So the issues:

problems: I get the customerId from session. The IOC is only established once.
question: How do I change the context when the Dropdownlist changes

problem: The sessionService is its own entity.
question: Is that value accessable in the IOC?

Am I coming at this problem from the wrong angle?

Community
  • 1
  • 1
Robert
  • 4,306
  • 11
  • 45
  • 95
  • Not sure if this will help all your issues, but for the first problem you can use an abstract factory (example here: http://stackoverflow.com/a/21714374/1362136) – Stephen Byrne Aug 06 '14 at 16:16

1 Answers1

0

So I took a different approach to the situation. Instead of injecting IMyContext and ISessionService I decided to create an IEntityService that I could mock and then stub the method that returns my context on:

public Repository(IEntityService entityService)
{
    _context = entityService.GetCustomerContext();
}

public partial class MyEntity : IMyContext
{
    public MyEntity(string connectionString) : base(connectionString){}
}

public class EntityService : IEntityService
{
    private readonly ISessionService _sessionService;

    public EntityService(ISessionService sessionService)
    {
        _sessionService = sessionService;
    }

    //This is only public because I need the connectionString to get a List of all the stored procs on the table.
    public string GetConnectionString()
    {
        var connectionString = "";
        CustomerConnection.GetCustomerConnection(_sessionService.GetCustomerId(), out connectionString);
        return connectionString;
    }

    public ICustomerContext GetCustomerContext()
    {
        return new MyEntity(connectionString.ToEntityConnectionString(typeof(MyEntity)));
    }
}

Now in my tests I just need to stub the return of the GetCustomerContext() method (I am using RhinoMocks and NUnit):

private Repository _sut;
private IEntityService _serviceFake;
private ICustomerContext _contextFake;

[SetUp]
public void SetUp()
{
    _serviceFake = MockRepository.GenerateMock<IEntityService>();
    _sut = new Repository(_serviceFake);
    _contextFake = MockRepository.GenerateMock<ICustomerContext>();
    _serviceFake.Stub(x => x.GetCustomerContext()).Return(_contextFake);
}

Now I can test the individual methods to verify expectations.

Robert
  • 4,306
  • 11
  • 45
  • 95