Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository pattern in F#

Tags:

.net

f#

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?

like image 506
yanta Avatar asked Nov 29 '10 16:11

yanta


2 Answers

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.

like image 191
Tim Robinson Avatar answered Sep 20 '22 14:09

Tim Robinson


I don't think that it makes sense to have an immutable interface to an innately mutable type (such as a database). However, you might want to split the functionality into a mutable database type (IRepository<'a> in your case) and an immutable set of changes (such as ChangeSet<'a>, for example). The result might look something like:

type ChangeSet<'a> = ...                         //'
module ChangeSet = begin                         //'
  let empty = ...                                //'
  let add a c = ...                              //'
  ...
end

type IRepository<'a> =                           //'
  abstract GetAll : unit -> seq<'a>              //'
  abstract ApplyChanges : ChangeSet<'a> -> unit  //'

type Repository<'a> = ...                        //'

let repo = new Repository<Question>(...)
let changes =
  ChangeSet.empty
  |> ChangeSet.add (Question "Repository pattern in F#")
  |> ChangeSet.add (Question "...")
repo.ApplyChanges changes
let results = repo.GetAll()
like image 34
kvb Avatar answered Sep 18 '22 14:09

kvb