diff --git a/Generator/EntityFactories.hs b/Generator/EntityFactories.hs
new file mode 100644
--- /dev/null
+++ b/Generator/EntityFactories.hs
@@ -0,0 +1,29 @@
+{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE OverloadedStrings #-}
+module Generator.EntityFactories where
+import AST
+import Data.Maybe
+import qualified Data.Text as T
+import Data.List
+import Text.Shakespeare.Text hiding (toText)
+import Generator.Common
+
+entityFactory :: Entity -> String
+entityFactory e = T.unpack $(codegenFile "codegen/entity-factory.cg")
+    where requiredFields = [ f | f <- entityFields e, 
+                                 fieldOptional f == False, 
+                                 isNothing $ fieldDefault f ]
+          defaultFields = (entityFields e) \\ requiredFields
+          fieldParamName f = fieldName f ++ "_"
+          fieldSetter f = entityFieldName e f ++ " = " ++ value f
+          value f 
+            | f `elem` requiredFields = fieldParamName f
+            | fieldOptional f = "Nothing"
+            | otherwise = case fieldDefault f of
+                    Just fv -> fieldValueToHs fv
+                    Nothing -> error "missing default value in entityFactory"
+
+entityFactories :: Module -> String
+entityFactories m = concatMap entityFactory (modEntities m)
+
+
diff --git a/yesod-dsl.cabal b/yesod-dsl.cabal
--- a/yesod-dsl.cabal
+++ b/yesod-dsl.cabal
@@ -1,5 +1,5 @@
 name:           yesod-dsl
-version:        0.1.1.14
+version:        0.1.1.15
 license:        BSD3
 license-file:   LICENSE
 author:         Tero Laitinen 
@@ -44,6 +44,7 @@
                    Generator.Cabal
                    Generator.EsqueletoInstances
                    Generator.Fay
+                   Generator.EntityFactories
                    Obfuscate
                    SyncFile
                    ExpandMacros
