예제 #1
0
        public static bool Insert <T>(this IPersistable <T> obj, bool recursive)
        {
            var inserts = new List <Func <bool> >();

            if (recursive)
            {
                var mis = obj.GetType().GetMembers().Where(m => m.CanPreserve() && !m.GetDeclarationType().IsSimpleType()).Select(m => obj.Get(m.Name));
                mis.ForEach(m => {
                    var insert = GetInsert(m);
                    if (insert != null)
                    {
                        inserts.Add(insert);
                    }
                });

                if (inserts.Count > 0)
                {
                    using (var tran = new TransactionScope()) {
                        inserts.ForEach(insert => insert());
                        DataContext.GetEntry <T>().Insert((T)obj);
                        tran.Complete();
                        return(true);
                    }
                }
            }
            return(DataContext.GetEntry <T>().Insert((T)obj) > 0);
        }