public IList <Booking> GetAll() { List <string> names = new List <string>(); IList <Booking> bookings; IList <Passenger> passengers; // create our NHibernate session factory string connectionString = " server=BW-DESKTOP\\SQLEXPRESS2014; database=FlightsDB;Trusted_Connection=True;"; var sessionFactory = SessionFactoryBuilder.BuildSessionFactory(connectionString, true, true); using (var session = sessionFactory.OpenSession()) { using (session.BeginTransaction()) { bool isopen = session.IsOpen; bool isconnected = session.IsConnected; passengers = session.Query <Passenger>().ToList(); bookings = session.Query <Booking>().ToList(); // Querying to get all the books foreach (var b in bookings) { names.Add(b.FlightNo); } } } return(bookings); }
public Task RouteAsync(RouteContext context) { var SessionFactory = SessionFactoryBuilder.BuildSessionFactory(true, true); using (var Session = SessionFactory.OpenSession()) { using (var transaction = Session.BeginTransaction()) { string url = context.HttpContext.Request.Path.Value.TrimStart('/').TrimEnd('/'); var FullUrl = Session.CreateCriteria <Url>().Add(Restrictions.Like("ShortUrl", url, MatchMode.Anywhere)); IList <Url> Urls = FullUrl.List <Url>(); foreach (var record in Urls) { if (url != "" && record.ShortUrl.Contains(url)) { record.PassCount++; Session.SaveOrUpdate(record); context.Handler = async ctx => { ctx.Response.Redirect(record.FullUrl, true); }; } } transaction.Commit(); } } return(Task.CompletedTask); }
static void Main(string[] args) { // create our NHibernate session factory string connectionStringName = "Server=DESKTOP-JFV34LE;database=practica;Integrated Security=SS"; var sessionFactory = SessionFactoryBuilder.BuildSessionFactory(connectionStringName, true, true); using (var session = sessionFactory.OpenSession()) { // populate the database using (var transaction = session.BeginTransaction()) { // create a couple of Persons var person1 = new Person { Name = "Rayen Trabelsi" }; var person2 = new Person { Name = "Mohamed Trabelsi" }; var person3 = new Person { Name = "Hamida Rebai" }; //create tasks var task1 = new Task { Title = "Task 1", State = TaskState.Open, AssignedTo = person1 }; var task2 = new Task { Title = "Task 2", State = TaskState.Closed, AssignedTo = person2 }; var task3 = new Task { Title = "Task 3", State = TaskState.Closed, AssignedTo = person3 }; // save both stores, this saves everything else via cascading session.SaveOrUpdate(task1); session.SaveOrUpdate(task2); session.SaveOrUpdate(task3); transaction.Commit(); } using (var session2 = sessionFactory.OpenSession()) { //retreive all tasks with person assigned to using (session2.BeginTransaction()) { var tasks = session.CreateCriteria(typeof(Task)) .List <Task>(); foreach (var task in tasks) { TaskService.GetTaskInfo(task); } } } Console.ReadKey(); } }
public int AddOrUpdate(int id, string endereco, object emails, string name, object phones, string empresa) { try { using (NHibernate.ISessionFactory sessionFactory = SessionFactoryBuilder.BuildSessionFactory()) { using (NHibernate.ISession session = sessionFactory.OpenSession()) { using (NHibernate.ITransaction transaction = session.BeginTransaction()) { ContatoEntitis contatoToBeSave = new ContatoEntitis { Id = id, Endereco = endereco, Email = JsonConvert.SerializeObject(emails), Nome = name, Telefone = JsonConvert.SerializeObject(phones), Empresa = empresa }; if (contatoToBeSave.Id > 0) { session.Update(contatoToBeSave); } else { session.Save(contatoToBeSave); } transaction.Commit(); return(contatoToBeSave.Id); } } } } catch (Exception ex) { throw; } }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { JsonConvert.DefaultSettings = () => { var settings = new JsonSerializerSettings(); settings.Converters.Add(new StringEnumConverter()); settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); return(settings); }; services .AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); var container = new Container(); services.AddSingleton <NHibernate.ISessionFactory>(SessionFactoryBuilder.BuildSessionFactory(true, false)); services.AddScoped <NHibernate.ISession>(factory => factory .GetServices <NHibernate.ISessionFactory>() .First() .OpenSession() ); container.Configure(config => { config.AddRegistry(new DiRegistry()); config.Populate(services); }); //container.Populate(services); return(container.GetInstance <IServiceProvider>()); }
public void ConfigureServices(IServiceCollection services) { services.AddCors(); services.AddControllers(); services.AddMvc() .AddControllersAsServices() .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) .AddNewtonsoftJson() .AddFluentValidation(o => o.RegisterValidatorsFromAssemblyContaining <Startup>()); services.Configure <AppSettings>(Configuration.GetSection("AppSettings")); services.AddScoped <IUserService, UserService>(); services.AddScoped <ICompanyService, CompanyService>(); services.AddScoped <ICompanyRepository, CompanyRepository>(); var sessionFactory = SessionFactoryBuilder.BuildSessionFactory(connectionString, false, true); services.AddSingleton(sessionFactory); }
public List <ContatoEntitis> List() { try { List <ContatoEntitis> retorno = new List <ContatoEntitis>(); using (NHibernate.ISessionFactory sessionFactory = SessionFactoryBuilder.BuildSessionFactory()) { using (NHibernate.ISession session = sessionFactory.OpenSession()) { retorno = session.Query <ContatoEntitis>().ToList(); session.Flush(); } } return(retorno); } catch (Exception ex) { throw; } }
public ContatoEntitis Get(int id) { try { ContatoEntitis retorno = new ContatoEntitis(); using (NHibernate.ISessionFactory sessionFactory = SessionFactoryBuilder.BuildSessionFactory()) { using (NHibernate.ISession session = sessionFactory.OpenSession()) { retorno = session.Get <ContatoEntitis>(id); session.Flush(); } } return(retorno); } catch (Exception exs) { throw; } }
public bool Delete(ContatoEntitis contatoToBeDelete) { try { using (NHibernate.ISessionFactory sessionFactory = SessionFactoryBuilder.BuildSessionFactory()) { using (NHibernate.ISession session = sessionFactory.OpenSession()) { using (NHibernate.ITransaction transaction = session.BeginTransaction()) { session.Delete(contatoToBeDelete); transaction.Commit(); } } } return(true); } catch (Exception ex) { throw; } }
public AgendaService() { _sessionFactory = SessionFactoryBuilder.BuildSessionFactory(); _contatos = new List <Contato>() { new Contato { Identificador = 1, Nome = "Erlan Torres de Aguiar", Empresa = "Erlan Ltda", Cep = "06020194", Logradouro = "Av Manoel Pedro Pimental", Numero = "200", Complemento = "Bloco 12 Apto 54", Bairro = "Continental", Cidade = "Osasco", Uf = "SP", DadosAdicionais = new List <DadoAdicional> { new DadoAdicional { TipoDado = new TipoDadoAdicional { SiglaTipo = "TELEFONE", Nome = "Telefone" }, ClassificacaoDado = new ClassificacaoDadoAdicional { SiglaClassificacao = "CASA", Nome = "Casa" }, Valor = "11954203111" }, new DadoAdicional { TipoDado = new TipoDadoAdicional { SiglaTipo = "TELEFONE", Nome = "Telefone" }, ClassificacaoDado = new ClassificacaoDadoAdicional { SiglaClassificacao = "TRABALHO", Nome = "Trabalho" }, Valor = "11954203111" }, new DadoAdicional { TipoDado = new TipoDadoAdicional { SiglaTipo = "EMAIL", Nome = "E-mail" }, ClassificacaoDado = new ClassificacaoDadoAdicional { SiglaClassificacao = "CASA", Nome = "Casa" }, Valor = "*****@*****.**" }, new DadoAdicional { TipoDado = new TipoDadoAdicional { SiglaTipo = "TELEFONE", Nome = "Telefone" }, ClassificacaoDado = new ClassificacaoDadoAdicional { SiglaClassificacao = "TRABALHO", Nome = "Trabalho" }, Valor = "1197889888" } } }, new Contato { Identificador = 20, Nome = "Júlia Peres Pereira", Empresa = "Magical Clock Ltda", Cep = "06020194", Logradouro = "Av Manoel Pedro Pimental", Numero = "200", Complemento = "Bloco 12 Apto 54", Bairro = "Continental", Cidade = "Osasco", Uf = "SP", DadosAdicionais = new List <DadoAdicional> { new DadoAdicional { TipoDado = new TipoDadoAdicional { SiglaTipo = "TELEFONE", Nome = "Telefone" }, ClassificacaoDado = new ClassificacaoDadoAdicional { SiglaClassificacao = "TRABALHO", Nome = "Trabalho" }, Valor = "11954203111" }, new DadoAdicional { TipoDado = new TipoDadoAdicional { SiglaTipo = "EMAIL", Nome = "E-mail" }, ClassificacaoDado = new ClassificacaoDadoAdicional { SiglaClassificacao = "CASA", Nome = "Casa" }, Valor = "*****@*****.**" } } } }; }
static void Main(string[] args) { // create our NHibernate session factory string connectionStringName = "LocalDevelopment"; var sessionFactory = SessionFactoryBuilder.BuildSessionFactory(connectionStringName, true, true); //var sessionFactory = Build(); using (var session = sessionFactory.OpenSession()) { using (var transaction = session.BeginTransaction()) { // create a couple of Stores each with some Products and Employees var barginBasin = new Store { Name = "Bargin Basin" }; var superMart = new Store { Name = "SuperMart" }; var potatoes = new Product { Name = "Potatoes", Price = 3.60 }; var fish = new Product { Name = "Fish", Price = 4.49 }; var milk = new Product { Name = "Milk", Price = 0.79 }; var bread = new Product { Name = "Bread", Price = 1.29 }; var cheese = new Product { Name = "Cheese", Price = 2.10 }; var waffles = new Product { Name = "Waffles", Price = 2.41 }; var daisy = new Employee { FirstName = "Daisy", LastName = "Harrison" }; var jack = new Employee { FirstName = "Jack", LastName = "Torrance" }; var sue = new Employee { FirstName = "Sue", LastName = "Walkters" }; var bill = new Employee { FirstName = "Bill", LastName = "Taft" }; var joan = new Employee { FirstName = "Joan", LastName = "Pope" }; // add products to the stores, there's some crossover in the products in each // store, because the store-product relationship is many-to-many AddProductsToStore(barginBasin, potatoes, fish, milk, bread, cheese); AddProductsToStore(superMart, bread, cheese, waffles); // add employees to the stores, this relationship is a one-to-many, so one // employee can only work at one store at a time AddEmployeesToStore(barginBasin, daisy, jack, sue); AddEmployeesToStore(superMart, bill, joan); // save both stores, this saves everything else via cascading session.SaveOrUpdate(barginBasin); session.SaveOrUpdate(superMart); transaction.Commit(); } // retreive all stores and display them using (session.BeginTransaction()) { var stores = session.CreateCriteria(typeof(Store)) .List <Store>(); foreach (var store in stores) { WriteStorePretty(store); } } Console.ReadKey(); } }
public UrlsController() { _SessionFactoryBuilder = SessionFactoryBuilder.BuildSessionFactory(true, true); }