Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4028536
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:13:56+00:00 2026-05-20T11:13:56+00:00

We are builing a windows desktop application (not web based) and trying to come

  • 0

We are builing a windows desktop application (not web based) and trying to come up with the best way to implement Repository and UnitOfWork Pattern.

In a typical Asp.Net Mvc application your repositories are injected with data context, services are injected with repositories and finally controllers are injected with services and all is well if you don’t hit any exception, you will commit the changes.

In windows forms/wpf applications it is not advisable to use single datacontext ( Oren has a post on this on MSDN) so we have decided to create data context at the presenter. We are using Linq2SQl and we have two different databases to work with based on the view.

Currently I 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 UI

public class TodoPresenter
{
    public void Save()
    {
       Using (DataContext dataContext = DataContextFactory.GetNewContextForDatabase1())
       {
           ITodoService service = new TodoService(dataContext);
            ...
           service.Save(..);
           dataContext.SubmitChanges();           
       }
    }

}

I would like decouple the presenter from service and would like to inject TodoService when ITodoService is requested, but I cannot inject data context for two reasons, as I have to decide based on the database or can’t even maintain a data context at the application level even if we have only one database being a windows application (many views are open at a time as tabs in the app) and with no data context I cannot create Repository classes and can’t inject services.

Any idea on how to achieve decoupling in this situation

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:13:57+00:00Added an answer on May 20, 2026 at 11:13 am
     > I cannot inject data context
    

    but maybe you can inject a factory method that creates the context and the service

    public class TodoPresenter
    {
        private Func<DataContext> dataContextFactory;
        private Func<DataContext, ITodoService> serviceFactory;
    
        // created with new TodoPresenter(DataContextFactory.GetNewContextForDatabase1(), 
        //                   dc => new TodoService(dc, 
        //                              new ToDoRepository(dc => new ToDoRepository(dc))));
        public TodoPresenter(Func<DataContext> dataContextFactory, 
                             Func<DataContext, ITodoService> serviceFactory)
        {
            this.dataContextFactory = dataContextFactory;
            this.serviceFactory = serviceFactory;
        }
    
        public void Save()
        {
            using (DataContext dataContext = this.dataContextFactory())
            {
                ITodoService service = serviceFactory(dataContext);
                // ...
                //service.Save(..);
                //dataContext.SubmitChanges();           
            }
    
        }
    }
    

    Update

    The Service needs a factory to get the repository as well

    public TodoService(DataContext dataContext, 
             Func<DataContext, IRepository<Todo> todoRepository){...}
    

    An integrationtest with Service and presenter looks like this

      var toDoRepository = new Mock<..>(..);
      var datacontext= new Mock<..>(..);
      var presenter = new TodoPresenter(dc => datacontext, 
                      dc => new TodoService(dc, dc2 => toDoRepository ));
    

    An unitest for presenter starts like this

      var TodoService= new Mock<..>(..);
      var datacontext= new Mock<..>(..);
      var presenter = new TodoPresenter(dc => datacontext, 
                      dc => TodoService);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an application that is targeting Windows, Mac and Linux soon. I was
I'm building a .net application with windows forms. I'm pondering on the following problem:
I'm building a C++ application and need to use PDCurses on Windows. I'm compiling
I am building Windows apps for a few clients. I read Joel on Software
I'm building a Windows Forms form in C# with various elements in a panel
I'm building a Windows service and following this MSDN article , but I'm stuck
I get the following error when building my Windows Forms solution: LC.exe exited with
Platform Builder is a tool for building a Windows CE Operating system on your
I have very little experience building software for Windows, and zero experience using the
I am building a physics simulation engine and editor in Windows. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.