示例#1
0
        private static void LINQPersistence()
        {
            bool exit = false;

            while (!exit)
            {
                Console.Clear();
                Console.WriteLine("LINQ Persistence Demo\n");
                Console.WriteLine("<0> RETURN");
                Console.WriteLine("<1> LINQ AuditTrailLog");
                Console.Write("\nChoose an option... ");

                ConsoleKeyInfo key = Console.ReadKey();
                Console.WriteLine();

                ZOperationResult operationResult = new ZOperationResult();

                AuditTrailUnitOfWorkEF unitOfWork =
                    (AuditTrailUnitOfWorkEF)DIHelper.DIManager.GetService <IAuditTrailUnitOfWork>();
                AuditTrailGenericRepositoryEF <AuditTrailLog> repository =
                    (AuditTrailGenericRepositoryEF <AuditTrailLog>)unitOfWork.GetRepository <AuditTrailLog>();
                IEnumerable <AuditTrailLog> enumerable;
                Expression <Func <AuditTrailLog, bool> > where;
                Func <IQueryable <AuditTrailLog>, IOrderedQueryable <AuditTrailLog> > orderBy;

                switch (key.KeyChar) // <ENTER> = '\r'
                {
                case ('0'):
                    exit = true;
                    break;

                case ('1'):
                    int id = 0;

                    Console.WriteLine("\nLINQ");
                    enumerable = repository
                                 .Search(x => x.Id > id, o => o.OrderByDescending(x => x.Id));
                    if (operationResult.Ok)
                    {
                        foreach (AuditTrailLog entity in enumerable)
                        {
                            Console.WriteLine("{0} {1} {2}", entity.Id, entity.LogDate, entity.LogTime);
                        }
                    }

                    Console.WriteLine("\nLINQ with parameters");
                    where      = x => x.Id > id;
                    orderBy    = o => o.OrderByDescending(x => x.Id);
                    enumerable = repository
                                 .Search(where, orderBy);
                    if (operationResult.Ok)
                    {
                        foreach (AuditTrailLog entity in enumerable)
                        {
                            Console.WriteLine("{0} {1} {2}", entity.Id, entity.LogDate, entity.LogTime);
                        }
                    }

                    Console.WriteLine("\nDynamic LINQ");
                    enumerable = repository
                                 .Search("Id > @0", new object[] { id }, "Id descending");
                    if (operationResult.Ok)
                    {
                        foreach (AuditTrailLog entity in enumerable)
                        {
                            Console.WriteLine("{0} {1} {2}", entity.Id, entity.LogDate, entity.LogTime);
                        }
                    }

                    break;
                }

                if (!exit)
                {
                    Console.WriteLine("\n{0}\n{1}\n{2}",
                                      unitOfWork.GetType().FullName,
                                      unitOfWork.DBMS.ToString(),
                                      repository.GetType().FullName);

                    if (!operationResult.Ok)
                    {
                        Console.WriteLine(operationResult.Text);
                    }

                    Console.Write("\nPress <KEY> to continue... ");
                    Console.ReadKey();
                }
            }
        }
示例#2
0
        private static void CRUDPersistence()
        {
            bool exit = false;

            while (!exit)
            {
                Console.Clear();
                Console.WriteLine("CRUD Persistence Demo\n");
                Console.WriteLine("<0> RETURN");
                Console.WriteLine("<1> CREATE AuditTrailLog");
                Console.WriteLine("<2> UPDATE AuditTrailLog");
                Console.WriteLine("<3> DELETE AuditTrailLog");
                Console.WriteLine("<4> TRANSACTION COMMIT AuditTrailLog");
                Console.WriteLine("<5> TRANSACTION ROLLBAK AuditTrailLog");
                Console.WriteLine("<T> TRUNCATE TABLE AuditTrailLog");
                Console.Write("\nChoose an option... ");

                ConsoleKeyInfo key = Console.ReadKey();
                Console.WriteLine();

                ZOperationResult operationResult = new ZOperationResult();

                AuditTrailUnitOfWorkEF unitOfWork =
                    (AuditTrailUnitOfWorkEF)DIHelper.DIManager.GetService <IAuditTrailUnitOfWork>();
                AuditTrailGenericRepositoryEF <AuditTrailLog> repository =
                    (AuditTrailGenericRepositoryEF <AuditTrailLog>)unitOfWork.GetRepository <AuditTrailLog>();
                AuditTrailLog auditTrailLog;

                switch (key.KeyChar) // <ENTER> = '\r'
                {
                case ('0'):
                    exit = true;
                    break;

                case ('1'):
                    auditTrailLog         = new AuditTrailLog();
                    auditTrailLog.LogDate = DateTime.Today;
                    auditTrailLog.LogTime = DateTime.Now;
                    if (repository.Create(operationResult, auditTrailLog))
                    {
                        unitOfWork.Save(operationResult);
                    }

                    break;

                case ('2'):
                    auditTrailLog         = new AuditTrailLog();
                    auditTrailLog.LogDate = DateTime.Today;
                    auditTrailLog.LogTime = DateTime.Now;
                    if (repository.Create(operationResult, auditTrailLog))
                    {
                        if (unitOfWork.Save(operationResult))
                        {
                            auditTrailLog.LogDate = DateTime.Today.AddMonths(1);
                            auditTrailLog.LogTime = DateTime.Now.AddMonths(1);
                            if (repository.Update(operationResult, auditTrailLog))
                            {
                                unitOfWork.Save(operationResult);
                            }
                        }
                    }

                    break;

                case ('3'):
                    auditTrailLog         = new AuditTrailLog();
                    auditTrailLog.LogDate = DateTime.Today;
                    auditTrailLog.LogTime = DateTime.Now;
                    if (repository.Create(operationResult, auditTrailLog))
                    {
                        if (unitOfWork.Save(operationResult))
                        {
                            if (repository.Delete(operationResult, auditTrailLog))
                            {
                                unitOfWork.Save(operationResult);
                            }
                        }
                    }

                    break;

                case ('4'):
                    try
                    {
                        if (unitOfWork.BeginTransaction(operationResult))
                        {
                            auditTrailLog         = new AuditTrailLog();
                            auditTrailLog.LogDate = DateTime.Today;
                            auditTrailLog.LogTime = DateTime.Now;
                            if (repository.Create(operationResult, auditTrailLog))
                            {
                                if (unitOfWork.Save(operationResult))
                                {
                                    auditTrailLog.LogDate = DateTime.Today.AddMonths(1);
                                    auditTrailLog.LogTime = DateTime.Now.AddMonths(1);
                                    if (repository.Update(operationResult, auditTrailLog))
                                    {
                                        if (unitOfWork.Save(operationResult))
                                        {
                                            unitOfWork.CommitTransaction(operationResult);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        operationResult.ParseException(exception);
                    }

                    if (!operationResult.Ok)
                    {
                        unitOfWork.RollbackTransaction(operationResult);
                    }

                    break;

                case ('5'):
                    try
                    {
                        if (unitOfWork.BeginTransaction(operationResult))
                        {
                            auditTrailLog         = new AuditTrailLog();
                            auditTrailLog.LogDate = DateTime.Today;
                            auditTrailLog.LogTime = DateTime.Now;
                            if (repository.Create(operationResult, auditTrailLog))
                            {
                                if (unitOfWork.Save(operationResult))
                                {
                                    auditTrailLog.LogDate = DateTime.Today.AddMonths(1);
                                    auditTrailLog.LogTime = DateTime.Now.AddMonths(1);
                                    if (repository.Update(operationResult, auditTrailLog))
                                    {
                                        if (unitOfWork.Save(operationResult))
                                        {
                                            unitOfWork.RollbackTransaction(operationResult);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        operationResult.ParseException(exception);
                    }

                    if (!operationResult.Ok)
                    {
                        unitOfWork.RollbackTransaction(operationResult);
                    }

                    break;

                case ('t'):
                case ('T'):
                    unitOfWork.SQLCommand("TRUNCATE TABLE EasyLOBAuditTrailLog");
                    break;
                }

                if (!exit)
                {
                    if (operationResult.Ok)
                    {
                        List <AuditTrailLog> list = (List <AuditTrailLog>)repository.SearchAll();
                        foreach (AuditTrailLog entity in list)
                        {
                            Console.WriteLine("{0} {1} {2}", entity.Id, entity.LogDate, entity.LogTime);
                        }
                    }

                    if (!operationResult.Ok)
                    {
                        Console.WriteLine(operationResult.Text);
                    }

                    Console.Write("\nPress <KEY> to continue... ");
                    Console.ReadKey();
                }
            }
        }