We are creating an application for the Windows desktop (and not on the website) and are trying to find the best way to implement the repository and UnitOfWork template.
In a typical Asp.Net Mvc application, your repositories are entered by the data context, repositories are introduced to the services, and finally, services are introduced to the controllers, and everything is fine, if you do not get into any exception, you make changes.
In windows / wpf applications, it is not recommended to use a single datacontext ( Oren has a message on this in MSDN), so we decided to create a data context in the presenter. We use Linq2SQl, and we have two different databases for working with presentation.
I currently have the following implementation
public interface IRepository<T> where T : class
{
IEnumerable<T> Find(Expression<Func<T, bool>> where);
T Single(Expression<Func<T, bool>> where);
...
}
public class Repository<T> : IRepository<T> where T : class
{
private readonly Table<T> _table;
public Repository(DataContext dataContext)
{
_table = dataContext.GetTable<T>();
}
}
public Class TodoService :ITodoService
{
IRepository<Todo> _todoRepository;
public TodoService(DataContext dataContext)
{
_todoRepository = new Repository<Todo>(dataContext)
}
...
}
}
// Presenter for the user interface
public class TodoPresenter
{
public void Save()
{
Using (DataContext dataContext = DataContextFactory.GetNewContextForDatabase1())
{
ITodoService service = new TodoService(dataContext);
...
service.Save(..);
dataContext.SubmitChanges();
}
}
}
TodoService ITodoService, , , , Windows ( ), .
,