   }
 }
 
+
+// MARK:- Private
+
+private extension StoreType  {
+  var coreDataType: String {
+    switch self {
+    case .SQLite: return NSSQLiteStoreType
+    case .Binary: return NSBinaryStoreType
+    case .InMemory: return NSInMemoryStoreType
+    }
+  }
+}
+
 //MARK: - Factory
-extension PersistenceLayer {
+private extension PersistenceLayer {
+
+  typealias StoreParams = (configuration: String?, URL: NSURL?, options: [NSObject : AnyObject]?)
+
+  class func setupCoordinator(coordinator: NSPersistentStoreCoordinator, name:String, type: StoreType, location: StoreLocationType, errorHandler: ErrorHandler) -> NSPersistentStoreCoordinator {
+
+    func params() -> StoreParams? {
+      switch (type, location) {
+        case (.SQLite, .PrivateFolder):
+          let url = NSURL.fileURLWithPath(FileHelper.filePath("\(name).sqlite"))
+          return (nil, url, nil)
+      case (_, .SharedGroup):
+        return nil
+      case (_, _):
+        return nil
+      }
+    }
+    
+    return storeConfigurator(coordinator)(type: .InMemory, params: params(), errorHandler: errorHandler)
+    
+  }
+  
+  final class func storeConfigurator(coordinator: NSPersistentStoreCoordinator)(type: StoreType, params: StoreParams?, errorHandler: ErrorHandler) -> NSPersistentStoreCoordinator {
+    var error: NSError?
+    if coordinator.addPersistentStoreWithType(type.coreDataType, configuration: params?.configuration, URL: params?.URL, options: params?.options, error: &error) == nil {
+      errorHandler.handle(error!)
+    }
+    return coordinator
+  }
+  
 
   class Factory {
     

import CoreData
import Sweet

public class PersistenceLayer {
  
  private let coreDataName: String
  public var errorHandler: ErrorHandler

  lazy var managedObjectModel: NSManagedObjectModel = Factory.defaultMOM(self.coreDataName)
  lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = Factory.storeCoordinator(self.coreDataName, mom: self.managedObjectModel, errorHandler: self.errorHandler)

  public lazy var mainMOC: NSManagedObjectContext = Factory.mainMOC(self.persistentStoreCoordinator)
  
  public init(name: String, errorHandler: ErrorHandler) {
    coreDataName = name
    self.errorHandler = errorHandler
  }

  public convenience init(name : String) {
    self.init(name : name, errorHandler: ErrorHandler())
  }
  public convenience init() {
    self.init(name : AppInfo.productName)
  }
}


      assertionFailure("model with name: \(name).momd not foun")
    }
    
// MARK:- NSManagedObjectContext
    
    final class func mainMOC (storeCoordinator:  NSPersistentStoreCoordinator) -> NSManagedObjectContext {
      let managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
      managedObjectContext.persistentStoreCoordinator = storeCoordinator
      return managedObjectContext
    }
    
  }
}

