0

I'm creating a new web application based on ASP.NET Web API and EF6.

ProjectName
 -ProjectName.Data (All entity framework stuff)
 -ProjectName.Service (All business logic)
 -ProjectName.Web (ASP.NET Web API All front end logic)

I've read tons of information the repository pattern & UoW , and the vast array of options on its advantages or disadvantages with EF. (StackOverflow has 10,081 results for "repository pattern").

We have no plans EVER to change to a different database and with EF6 i've read you can do Unit testing without additional abstraction.

I'm tending toward NOT using repository or UoW and instead talking directly to DbContext from the service layer.

Can anyone provide examples that implement EF6 without a repository, with a service layer ?

Even better a sample project or github project using this in real life ?

Edit:

Similar Questions:

Implementing Repository pattern and doing Tests

Generic Repository With EF 4.1 what is the point

these are question on the same subject but none of them provide good code example of using EF in a architecture similar to the one i described:

How to manage DbContext lifetime without repository / UoW?

How to implement unit testing using ef6 new features ?

Community
  • 1
  • 1
RuSh
  • 1,643
  • 7
  • 25
  • 39
  • And http://stackoverflow.com/q/5625746/861716 – Gert Arnold Jan 06 '15 at 21:38
  • stackoverflow.com/q/5625746/861716 and other answers dont provide a code example - "Repositories are currently very popular and overused. Everybody use them just because there are dozens of articles about creating repository for entity framework but nobody actually describes challenges related to this decision" – RuSh Jan 06 '15 at 21:44
  • I know, but asking for code examples makes this question far too broad for StackOverflow. So either this question is closed as "too broad" or "opinion based" (because these examples are a matter of taste) or as "unclear what you're asking" (because "examples" can be *anything*) - or as duplicate. – Gert Arnold Jan 06 '15 at 21:49
  • Your latest edit makes the question only broader. There are very long blog posts on these issues, like [this one](http://msdn.microsoft.com/en-us/data/dn314429.aspx). – Gert Arnold Jan 06 '15 at 21:57
  • I know there are very long blog posts :) I've been reading them for the last 24hrs , that's exactly why i'm asking for a real world detailed example. I found 100's of examples implementing this with repository but none for "no repository" implementation – RuSh Jan 06 '15 at 22:04

1 Answers1

0

Your question is too broad. There are too many ways to achieve the same result. Here is my two cents -

Although you do not want to use Repository, you still want to inject Dependencies using IoC container such as Ninject, Autofac and StructureMap.

If so, you just need to inject YourDbContext to Service class.

For example,

public class CustomerService : ICustomerService 
{
   private readonly YourDbContext _context;
   private readonly ICacheService _cacheService;
   ...

   public StateService(
      YourDbContext context, 
      ICacheService cacheService, 
      ...)
   {
      _context = context;
      _cacheService = cacheService;
      ...
   }
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • How do i manage the context lifetime ? no need to dispose ? – RuSh Jan 06 '15 at 21:54
  • IoC container will dispose it for you *(once you set object lifetime in rootscope)*. It is one of the job of IoC container. – Win Jan 06 '15 at 22:57
  • What is StateService? If its a constructor its totally declared incorrectly. – JonH Jan 08 '19 at 03:09