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

  • SEARCH
  • Home
  • 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 8309575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:09:50+00:00 2026-06-08T19:09:50+00:00

So you have a DAL in C# that using the Repository pattern and you

  • 0

So you have a DAL in C# that using the Repository pattern and you have an interface for each Repository. It is backed by ADO.NET, MS SQL Server, and Stored Procedure calls.

This is great for stubbing/mocking out the repository where it is being used else where when doing unit tests, and I Love it!

However, I would love to add some unit testing for the DAL itself. Preferably using Rhino Mocks with NUnit. However I am open to switching to MoQ if a solid case could be made that it can do something that Rhino can’t in relation to this question.

I would rather not have it talk to any DB during the Unit Tests to keep they more “pure” while still effectively testing the DAL itself. If however you can not really effectively unit test a DAL itself without it talking to a DB, what would be the alternatives that could be in memory replacements or a portable file based replacements that are compatible with of SQL Server and same SqlConnection and SqlCommand calls.

Open to refactoring the DAL if needed to make it easier to inject as long as it doesn’t over complicate the design at the same time. Already using Microsoft Unity, for DI with the Repository Pattern.

  • 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-06-08T19:09:51+00:00Added an answer on June 8, 2026 at 7:09 pm

    What exactly would those unit tests test? Considering typical DAL code is rarely anything more than delegating actual work to 3rd party data access code (ADO.NET, EntityFramework, NHibernate).

    I haven’t used ADO.NET much, but I suppose this NHibernate example is sufficient enough:

    public User GetUser(int id)
    {
        using (var session = sessionFactory.OpenSession())
        {
            return session.Get<User>(id);
        }
    }
    

    Of course, assuming the code is wrote correctly, all the dependencies (sessionFactory namely) would be passed via interfaces and therefore easy to stub, so writing unit test is possible (you’ll just have to mock a lot of stuff to make it happen).

    Purist approach would be to write such unit test, as it’s yet another data point proving your code indeed does what you assume (calling Get). However, note that test like this is rather expensive, as you essentially have to stub entire database access made by your provider (mocking sessionFactory to return mocked session which is then checked for calls made).

    This is difficult decision to be made (whether follow purist approach in this case), as you will have to write integration tests with real database anyways. And those tests will cover exactly the same areas as unit tests you’d wrote, but working in real environment (instead of stubbed one).

    In my experience, unit testing DAL is usually not worth the benefits it offers, especially when contrasted with its rather high costs (time spent writing code to stub the database environment) for unit test) and existence of proper integration tests suite.

    And last, I would advise against using in-memory database in any kind of testing. For the reasons below:

    • it is slow
    • it has several possible failure points that may be difficult to control (ORM configuration, in-memory database setup, possibly invalid test data)
    • gives you false sense of doing integration testing (while you’re not; in-memory database might behave entirely different than your real, production one)

    Unless you can load up exactly the same database to the memory, stick to proper integration tests (that would be DAL testing priority) with isolated unit tests backup (only if you can afford to write them).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a DAL using Rob Conery's spin on the repository pattern (from
I have a data access layer (DAL) that is written in ASP.NET 3.5 and
I have a .NET 3.5 assembly, a DAL, that connects to a database through
I have a ASP.net (C#) project that is using a three layer architecture. I
I have three SQL Server databases that a single application retrieves data from. I
I have an ASP.NET MVC application which is using Linq to SQL classes placed
We have a DAL that we're using with NHibernate.Search , so classes that need
I'm currently working with a system that has inherited a DAL using .Net's strongly
I have a Winform app that has 16 SQL Connections currently stored in the
I have a asp.net web application that is using Linq to NHibernate in NHibernate

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.