Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You actually just created a repository only you call it a 'data access layer' which is, in my opinion, not a good name since Entity Framework is the data access layer. A repository is an abstraction on top of a data access layer, Entity Framework in this case. </p> <hr /> <blockquote> <p>Is it wrong to create a new DbContext on each call to DAL ? Any Performance hit?</p> </blockquote> <p>Nope, it's just fine, but it might cause trouble when you fetch an entity in one instance of <code>DbContext</code>, and try to update it in another instance.</p> <hr /> <blockquote> <p>Would you use Linq2SQL in a new project ?</p> </blockquote> <p>Nope, Microsoft proposed Entity Framework as the successor of L2SQL and active development of it has stopped.</p> <hr /> <blockquote> <p>Any other recommended structure for using EF ? Examples? :)</p> </blockquote> <p>The approach you use, specific repositories, will result in a lot of redundant code. You could create a generic repository implementing an interface:</p> <pre><code>public interface IRepository&lt;TEntity&gt; where TEntity : class, new() { IEnumerable&lt;TEntity&gt; GetAll(); TEntity GetById(int id); IQueryable&lt;TEntity&gt; Table { get; } } </code></pre> <p>And an implementation of this:</p> <pre><code>public EfRepository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : class, new() { private readonly DatingSiteContext _context; public EfRepository() { _context = new DatingSiteContext(); } private IDbSet&lt;TEntity&gt; Entities { get { return _context.Set&lt;TEntity&gt;(); } } public IEnumerable&lt;TEntity&gt; GetAll() { return Entities.ToList(); } public TEntity GetById(int id) { return Entities.Find(id); } public IQueryable&lt;TEntity&gt; Table { get { return Entities; } } } </code></pre> <p>You can use this repository in your controller like this:</p> <pre><code>public class HomeController : Controller { private readonly IRepository&lt;User&gt; _userRepository; public HomeController() { _userRepository = new EfRepository&lt;User&gt;(); } public ActionResult Index() { var users = _userRepository.GetAll(); var inactiveUsers = _userRepository.Table.Where(u =&gt; !u.Active).ToList(); } } </code></pre> <p>This generic repository allows you to create mocked repositories:</p> <pre><code>public class FakeUserRepository : IRepository&lt;User&gt; { // ... } </code></pre> <p><hr /> This approach might seem like a lot of code, but as your entity type amount grows, it will save you a lot of work since all you have to do is create an <code>IRepository&lt;&gt;</code> field in a controller. Yet you have a lot of flexibility with the <code>IQueryable&lt;&gt;</code> property which allows deferred execution.</p> <p>I'm not saying this is the best method, just one I use regularly in projects. I have to say that I usually write a business (service) layer between the controller and the repositories. I keep my business logic and complex Linq queries (and their execution) there. I also use an IoC container which handles the lifetime of my objects (instances of <code>DbContext</code> and services for example). See <a href="https://stackoverflow.com/questions/3058/what-is-inversion-of-control">this question</a> for more information about that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload