Reputation: 19232
So I am using Entity Framework, I will not be using the repository pattern, please don't suggest it.
I am trying to create a child context that inherits from System.Data.Entity.DbContext and also using dependency injection so I need an interface and a concrete implementation.
The interface:
public interface IHomeUpContext
{
void Test();
}
My Entity Framework generated context:
public partial class HomeUpEntities : DbContext
...
My context base class:
public class HomeUpContext : HomeUpEntities, IHomeUpContext
{
public override int SaveChanges()
{
return base.SaveChanges();
}
public void Test()
{
}
}
And I have the following MVC Base Controller:
public class BaseController: Controller
{
public IHomeUpContext Context { get; set; }
}
And then my child controller:
public class HomeController : BaseController
{
public HomeController(IHomeUpContext context)
{
Context = context;
}
public ActionResult Index()
{
//complex queries encapsulated
var query = new ListPostsQuery(Context);
var posts = query.Execute(0, 10);
var postsViewModel = Mapper.Map<List<Post>, List<PostViewModel>>(posts);
return View(postsViewModel);
}
[HttpPost]
public ActionResult Edit(PostViewModel model)
{
var post = Context. //Only Test() method available here because my interface only defines that Method.
}
Obviously, I am doing dependency injection. In my HomeController.Edit method I want to be able to:
var post = Context.MyTable.Where(item => item.PostId == model.PostId);
I would have to define a bunch of stuff in my IHomeContext to do this. So now I will have to cast my injected concrete implementation of IHomeUpContext to HomeUpContext in my controller. No bueno.
Again I will not be using the Repository Pattern and a Unit of Work pattern, I firmly believe it is a leaky abstraction 9 times out of 10 and an "ever-spiraling Repository API": http://www.wekeroad.com/2014/03/04/repositories-and-unitofwork-are-not-a-good-idea/
I want to use dependency injection, encapsulate complex queries but do simple Context save update and get by id directly in my controller. But having to cast my IHomeUpContext to a concrete instance defeats the purpose of the dependency injection.
Upvotes: 1
Views: 663
Reputation: 24916
In this case if you don't want to use repository pattern (which I understand you don't) then you have to expose all DataSets in the interface as well:
public interface IHomeUpContext
{
DbSet<channel> channel { get; set; }
int SaveChanges();
}
Upvotes: 0