• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MzAw

Related Code
Generic Entity Framework 4.0 Base Repository
Generic Entity Framework 4.0 Base Repository with Paging
UnitOfWork Action filter for ASP.NET MVC and nHibernate
ForEach Extension method for IEnumerable<T>
Invoke Extension Method
Loading Strategy for Entity Framework 4.0
Use of Extension methods to hide "infrastructure code"
Unity Service Locator for ASP.NET MVC 3.0 Beta 1
Null Dot "Operator" Extension Method
Keep Entity Framework ObjectContext in a WCF Service OperationContext
WCF RIA Services Unity 2.0 DomainService Factory
A Default Entity Framework 4.0 ObjectContext Factory
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
Custom C# exception class
Email validation with Regular Expression
Reflection dynamically get private fields or properties
Url Validation with Regular Expression
Method to hash passwords
Number of sealed / unsealed types in the framework
Extension to the HttpClient

Generic linq-to-sql repository

A generic linq-to-sql class for easy, strongly-typed data access

1
1.04k 0 0 1 1 3

Repository interface:

public interface IRepository<T>
{
    IEnumerable<T> GetAll();
    IEnumerable<T> Get(Func<T, bool> exp);
    void Insert(T entity);
    void CommitChanges();
}

Implementation:

    public class LinqToSqlRepository<T> : IRepository<T> where T : class
    {
        private DataContext _db;

        public LinqToSqlRepository(DataContext db)
        {
        	_db = db;
        }

        protected Table<T> GetTable()
        {
        	return _db.GetTable<T>();
        }

        public IEnumerable<T> GetAll()
        {
        	return GetTable().AsQueryable();
        }

        public IEnumerable<T> Get(Func<T, bool> exp)
        {
        	return GetTable().Where(exp);
        }

        public void Insert(T entity)
        {
        	GetTable().InsertOnSubmit(entity);
        }

        public void CommitChanges()
        {
        	_db.SubmitChanges();
        }
    }

A fake repository for used with tests:

public class InMemoryRepository<T> : IRepository<T> where T : class
{
    private readonly IList<T> _storage;

    public InMemoryRepository(IList<T> storage)
    {
    	_storage = storage;
    }

    public IEnumerable<T> GetAll()
    {
    	return _storage.AsEnumerable();
    }

    public IEnumerable<T> Get(Func<T, bool> exp)
    {
    	return _storage.Where(exp);
    }

    public void Insert(T entity)
    {
    	_storage.Add(entity);
    }

    public void CommitChanges()
    {

    }
}

The DataContext passed to LinqToSqlRepository should be a UoW implementation. The repository could handle all the disposal by itself but i found it more useful to do explicitly do that.


Share: twitter | facebook   Action: used | fork | flag

.net

Mark '.net' tag as 'like'

Mark '.net' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

linq-to-sql

Mark 'linq-to-sql' tag as 'like'

Mark 'linq-to-sql' tag as 'ignore'

repository

Mark 'repository' tag as 'like'

Mark 'repository' tag as 'ignore'


 alexandern
110
September 29, 2010 10:59 PM

Fork

 Generic linq-to-sql repository - alexandern Wednesday 29, 2010 10:59 PM


2 Feedback

What happens if you don't need Insert in some of your repositories? -  @johannormen Thursday 30, 2010 1:35 PM
What about disposing the external resources as the db connection? -  @lennybacon Thursday 21, 2010 11:39 AM

You must log in before you can give any feedback


3 Discussion(s)

Newest Oldest
0

@johannormen Well, in this case you must implement the Insert method signature, but you can skip implementing the method body.

link | flag  | Reply

 alexandern
110
Thursday 30, 2010 5:46 PM

0
public interface IRepository<T> : IDisposable
{
    IEnumerable<T> GetAll();
    IEnumerable<T> Get(Func<T, bool> exp);
    void Insert(T entity);
    void CommitChanges();
}

public class LinqToSqlRepository<T> : IRepository<T> where T : class
{
    private DataContext _db;

    public LinqToSqlRepository()
    {
        var connStr =
            ConfigurationManager.
                ConnectionStrings["RepositoryConnection"].
                    ConnectionString;
        _db = new DataContext(connStr);
    }

    public LinqToSqlRepository(DataContext db)
    {
        _db = db;
    }

    protected Table<T> GetTable()
    {
        return _db.GetTable<T>();
    }

    public IEnumerable<T> GetAll()
    {
        return GetTable().AsQueryable();
    }

    public IEnumerable<T> Get(Func<T, bool> exp)
    {
        return GetTable().Where(exp);
    }

    public void Insert(T entity)
    {
        GetTable().InsertOnSubmit(entity);
    }

    public void CommitChanges()
    {
        _db.SubmitChanges();
    }

    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    /// <filterpriority>2</filterpriority>
    public void Dispose()
    {
        _db.Dispose();
    }
}
link | flag  | Reply

 @lennybacon
222
Thursday 21, 2010 11:40 AM

0

Reply to: lennybacon

A Perfect Fork, try that feature ;)

link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Thursday 21, 2010 12:22 PM


You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010