score:3
My suggestion is to use dependeny injection (or inversion of control - depends how would you like to call it). I use myself castle windor. Really simple to integrate with mvc.net. read more
When IoC is up and running create ContextManager. Somethig like this:
public class ContextManager : IContextManager
{
private XContext context;
public XContext GetContext()
{
return context ?? (context = XContext.Create());
}
}
Set IContextManager lifestyle as perwebrequest and you got yourself context that you can access from repositories and services. and it's same per one request.
EDIT
You also have to create your own controllerFactory
then you can use your services and repositories like this:
public class MyController : Controller
{
public ISomeService SomeService { get; set; }
public IContextManager ContextManager { get; set; }
...
}
You dont have to create new instances for services and repositories and you can manage those objects lifestyle from configuration. Most reasonable would be singleton
Source: stackoverflow.com
Related Articles
- Design of Service Layer and Repositories in Microsoft MVC
- Should the UI layer be able to pass lambda expressions into the service layer instead of calling a specific method?
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- Use a linq query as microsoft local report Data Source (WinForms)
- creating Linq to sqlite dbml from DbLinq source code
- Advice With Repository/Service Layer Design Pattern
- source code for LINQ 101 samples
- Encapsulate LINQ query for different Repositories to avoid repeated code
- LINQ complains about different EF contexts used in Service implementing multiple repositories
- WPF/LINQ data layer design question
- List or Array of String Contain specific word in Html Source Code
- Linq to SQL Source in Microsoft ReferenceSource
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- Enabling Service Broker in SQL Server 2008
- thread messaging system database schema design
- What does this C# code with an "arrow" mean and how is it called?
- How to resolve Value cannot be null. Parameter name: source in linq?
- The source contains no DataRows
- Return Entity Framework result as JSON
- C# Datatable - group by multiple columns with linq
- Distinct or Group By Based on Two Interchangeable Columns (LINQ)
- Disable all lazy loading or force eager loading for a LINQ context
- How to bind dropdownlist in mvc 3.0
- Reusable linq select query in Entity Framework
- AutoMapper ProjectTo fails with FirstOrDefault
- How do I filter an entity based on another entity? Linq many-to-many in C#
- join two Nav web services data in one repeater
- LINQ Performance Issue on only a few hundred records
- LINQ Multiple table left join, distinct count not giving proper result
- Linq ObservableCollection return no result
- Creating and querying a large database from CSVs
- Array comparison with Linq in Asp.NET MVC
- Getting all record by using a reference number in LINQ
- LINQ, can I write cleaner joins with method syntax?
- Using custom methods or delegates in linq-sql statement
- Using LinqToSql without load base in memory
- Convert SQL query result to C# Dictionary in LINQPad
- Unable to create a constant value of type... + cross context join?