Ok, after going over my notes, replies to my threads here, and my own inclinations, I came up with the following. Please tell me if this is viable, and if you’d change anything. If so, tell me what and, most importantly, why. It still requires that I code up an xml mapping file by hand, but I think I can live with that. At least I can stash these classes in a custom dependant library. What do you think?
using System.Data.Linq;
namespace Penguin.Factories
{
public interface IDataContextFactory
{
DataContext GetDataContext();
}
}
using System.Configuration;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.Reflection;
using Penguin.Managers;
namespace Penguin.Factories
{
public class DataContextFactory : IDataContextFactory
{
#region IDataContextFactory Members
public DataContext GetDataContext()
{
string connectionStringEntryName = SessionManager.ConnectionStringEntryName;
string mappingSourceAssemblyName = SessionManager.MappingSourceAssemblyName;
string mappingSourceResourceName = SessionManager.MappingSourceResourceName;
string connectionString = ConfigurationManager.ConnectionStrings[connectionStringEntryName].ConnectionString;
AssemblyName assemblyName = new AssemblyName(mappingSourceAssemblyName);
Assembly assembly = Assembly.Load(assemblyName);
Stream stream = assembly.GetManifestResourceStream(mappingSourceResourceName);
MappingSource mappingSource = XmlMappingSource.FromStream(stream);
return new DataContext(connectionString, mappingSource);
}
#endregion
}
}
using System;
using System.Linq;
namespace Penguin.Repositories
{
public interface IRepository<TEntity> where TEntity : class
{
IQueryable<TEntity> All();
IQueryable<TEntity> Where(Func<TEntity, bool> expression);
TEntity First(Func<TEntity, bool> expression);
TEntity Single(Func<TEntity, bool> expression);
void Insert(TEntity entity);
void Delete(TEntity entity);
void Commit();
}
}
using System;
using System.Data.Linq;
using System.Linq;
using Penguin.Factories;
namespace Penguin.Repositories
{
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
private DataContext dataContext { get; set; }
public Repository(IDataContextFactory dataContextFactory)
{
dataContext = dataContextFactory.GetDataContext();
}
#region IRepository<TEntity> Members
public IQueryable<TEntity> All()
{
return dataContext.GetTable<TEntity>().AsQueryable<TEntity>();
}
public IQueryable<TEntity> Where(Func<TEntity, bool> expression)
{
return dataContext.GetTable<TEntity>().Where<TEntity>(expression).AsQueryable<TEntity>();
}
public TEntity First(Func<TEntity, bool> expression)
{
return dataContext.GetTable<TEntity>().First(expression);
}
public TEntity Single(Func<TEntity, bool> expression)
{
return dataContext.GetTable<TEntity>().Single(expression);
}
public void Insert(TEntity entity)
{
dataContext.GetTable<TEntity>().InsertOnSubmit(entity);
}
public void Delete(TEntity entity)
{
dataContext.GetTable<TEntity>().DeleteOnSubmit(entity);
}
public void Commit()
{
dataContext.SubmitChanges();
}
#endregion
}
}