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 3440324
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:25:07+00:00 2026-05-18T08:25:07+00:00

I’m working on a prototype to use a document database (currently MongoDB, may change)

  • 0

I’m working on a prototype to use a document database (currently MongoDB, may change) and found the .NET drivers a bit of a pain, so I thought I would abstract the data access with the Repository pattern. This should make it easy to swap out whatever driver I’m using now (NoRM, mongodb-csharp, simple-mongob) with your killer f# mongodb driver that doesn’t suck when it’s ready.

My question is around the Add operation. This is going to have some side affect to the database and thus subsequent calls to All will be different. Should I care? In C# traditionally I wouldn’t, but I feel that in F# I should.

Here is the generic repository interface:

type IRepository<'a> =
    interface
        abstract member All : unit -> seq<'a>

        // Add has a side-effect of modifying the database
        abstract member Add : 'a -> unit
    end

And here is how a MongoDB implementation looks:

type Repository<'b when 'b : not struct>(server:MongoDB.IMongo,database) =
    interface IRepository<'b> with

        member x.All() =
            // connect and return all

        member x.Add(document:'b) =
            // add and return unit

Throughout the app I will use IRepository, making it easy to change drivers and potentially databases.

Calling All is fine, but with Add what I was hoping was instead of returning unit, return a new repository instance. Something like:

        // Add has a side-effect of modifying the database
        // but who cares as we now return a new repository
        abstract member Add : 'a -> IRepository<'a>

The problem is that if I call Get, then Add, the original repository still returns all the documents. Example:

let repo1 = new Repository<Question>(server,"killerapp") :> IRepository<Question>
let a1 = repo1.All() 
let repo2 = repo1.Add(new Question("Repository pattern in F#"))
let a2 = repo2.All()

Ideally I want length of a1 and a2 to be different, but they are the same as they both hit the database. The application works, users can ask their question, but the programmer is left wondering why it returns a new IRepository.

So should I be trying to handle the side-effect from Add on the database in the design of the types? How would others go about this, do you use a Repository or some interface class like this or have some better functional approach?

  • 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-18T08:25:08+00:00Added an answer on May 18, 2026 at 8:25 am

    It looks like you’re applying immutability to functions that affect state in the outside world. Regardless of the F# implementation, how would you see this working at the MongoDB level? How would you prevent repo1 from seeing any changes that repo2 makes? What happens if some other process affects the database — do both repo1 and repo2 change in this case?

    To put it another way, imagine an implementation of System.Console that worked like this. If Console.Out.WriteLine always returned a new immutable object, how would it interact with calls to Console.In.ReadLine?

    Edit tl;dr: Don’t do this. Sometimes side effects are fine.

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

Sidebar

Related Questions

No related questions found

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.