Mercurial > altnet-hispano
changeset 67:c8099df941bd
Implementando persistencia con NHibernate en el proyecto web
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Factories/AgendaFactory.cs Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Factories/AgendaFactory.cs Thu May 19 01:24:56 2011 -0300 @@ -1,15 +1,19 @@ -using AltNetHispano.Agendas.Domain; -using AltNetHispano.Agendas.Repositories.Memory; +using Agendas.NHibernate; +using AltNetHispano.Agendas.Domain; +using AltNetHispano.Agendas.Repositories.NHibernate; +using NHibernate; + namespace AltNetHispano.Agendas.Factories { - public static class AgendaFactory - { - private static Agenda _agenda; + public static class AgendaFactory + { + private static Agenda _agenda; - public static Agenda GetAgenda() - { - return _agenda ?? (_agenda = new Agenda(null, null, new EventoRepository(), new PonenteRepository())); - } - } + public static Agenda GetAgenda() + { + ISessionFactory sessionFactory = NhHelper.GetSessionFactory(); + return _agenda ?? (_agenda = new Agenda(null, null, new EventoRepository(sessionFactory), new PonenteRepository(sessionFactory))); + } + } } \ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Factories/Agendas.Factories.csproj Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Factories/Agendas.Factories.csproj Thu May 19 01:24:56 2011 -0300 @@ -31,8 +31,12 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> + <Reference Include="NHibernate"> + <HintPath>..\packages\NHibernate.3.1.0.4000\lib\Net35\NHibernate.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> + <Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="Microsoft.CSharp" /> @@ -48,9 +52,13 @@ <Project>{A14907DF-02E4-4FA7-BE27-4292AF50AA22}</Project> <Name>Agendas.Domain</Name> </ProjectReference> - <ProjectReference Include="..\Agendas.Repositories.Memory\Agendas.Repositories.Memory.csproj"> - <Project>{28C5EBFB-EE69-4765-A880-D4DE0BC89F48}</Project> - <Name>Agendas.Repositories.Memory</Name> + <ProjectReference Include="..\Agendas.NHibernate\Agendas.NHibernate.csproj"> + <Project>{9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}</Project> + <Name>Agendas.NHibernate</Name> + </ProjectReference> + <ProjectReference Include="..\Agendas.Repositories.NHibernate\Agendas.Repositories.NHibernate.csproj"> + <Project>{0973DF44-3B90-4D2A-B579-C64C93B6C853}</Project> + <Name>Agendas.Repositories.NHibernate</Name> </ProjectReference> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Factories/AttributeFactory.cs Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,13 @@ +using System.Web.Mvc; +using Agendas.NHibernate; + +namespace AltNetHispano.Agendas.Factories +{ + public class AttributeFactory + { + public static ActionFilterAttribute GetNHibernateSessionPerAction() + { + return new NHibernateSessionPerActionAttribute(NhHelper.GetSessionFactory()); + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.NHibernate/Agendas.NHibernate.csproj Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,77 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProductVersion>8.0.30703</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>Agendas.NHibernate</RootNamespace> + <AssemblyName>Agendas.NHibernate</AssemblyName> + <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="ConfOrm"> + <HintPath>..\packages\ConfOrm.1.0.1.5\lib\Net35\ConfOrm.dll</HintPath> + </Reference> + <Reference Include="ConfOrm.Shop"> + <HintPath>..\packages\ConfOrm.1.0.1.5\lib\Net35\ConfOrm.Shop.dll</HintPath> + </Reference> + <Reference Include="Iesi.Collections"> + <HintPath>..\packages\Iesi.Collections.3.1.0.4000\lib\Net35\Iesi.Collections.dll</HintPath> + </Reference> + <Reference Include="NHibernate"> + <HintPath>..\packages\NHibernate.3.1.0.4000\lib\Net35\NHibernate.dll</HintPath> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> + <Reference Include="System.Xml.Linq" /> + <Reference Include="System.Data.DataSetExtensions" /> + <Reference Include="Microsoft.CSharp" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="NhHelper.cs" /> + <Compile Include="NHibernateSessionPerActionAttribute.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Agendas.Domain\Agendas.Domain.csproj"> + <Project>{A14907DF-02E4-4FA7-BE27-4292AF50AA22}</Project> + <Name>Agendas.Domain</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.NHibernate/NHibernateSessionPerActionAttribute.cs Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,35 @@ +using System.Web.Mvc; +using NHibernate; +using NHibernate.Context; + +namespace Agendas.NHibernate +{ + public class NHibernateSessionPerActionAttribute : ActionFilterAttribute + { + private readonly ISessionFactory _sessionFactory; + + public NHibernateSessionPerActionAttribute(ISessionFactory sessionFactory) + { + _sessionFactory = sessionFactory; + } + + public override void OnActionExecuting(ActionExecutingContext filterContext) + { + var session = _sessionFactory.OpenSession(); + CurrentSessionContext.Bind(session); + + base.OnActionExecuting(filterContext); + } + + public override void OnResultExecuted(ResultExecutedContext filterContext) + { + base.OnResultExecuted(filterContext); + + var session = _sessionFactory.GetCurrentSession(); + + session.Flush(); + session.Close(); + + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,63 @@ +using System; +using AltNetHispano.Agendas.Domain; +using ConfOrm; +using ConfOrm.NH; +using NHibernate; +using NHibernate.Cfg; +using NHibernate.Tool.hbm2ddl; + +namespace Agendas.NHibernate +{ + public static class NhHelper + { + private static ISessionFactory _sessionfactory; + private static Configuration _cfg; + + public static Configuration GetConfiguration() + { + if (_cfg == null) + { + var orm = new ObjectRelationalMapper(); + orm.TablePerClass<Evento>(); + orm.TablePerClass<Ponente>(); + + orm.Cascade<Evento, Ponente>(Cascade.None); + orm.Cascade<Ponente, Evento>(Cascade.None); + + var mapper = new Mapper(orm); + + var mapping = mapper.CompileMappingFor(typeof(Evento).Assembly.GetTypes()); + + _cfg = new Configuration(); + _cfg.Configure(); + _cfg.AddDeserializedMapping(mapping, null); + } + return _cfg; + } + + public static ISessionFactory GetSessionFactory() + { + if (_sessionfactory == null) + { + var cfg = GetConfiguration(); + +#if DEBUG + var schemaValidator = new SchemaValidator(cfg); + try + { + schemaValidator.Validate(); + } + catch(Exception ex) + { + var schemaExport = new SchemaExport(cfg); + schemaExport.Create(false, true); + } + +#endif + + _sessionfactory = cfg.BuildSessionFactory(); + } + return _sessionfactory; + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.NHibernate/Properties/AssemblyInfo.cs Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Agendas.NHibernate")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("Agendas.NHibernate")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("b9429941-9ddb-4741-99da-cc05ed518f18")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.NHibernate/packages.config Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="Iesi.Collections" version="3.1.0.4000" /> + <package id="NHibernate" version="3.1.0.4000" /> + <package id="ConfOrm" version="1.0.1.5" /> +</packages> \ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Repositories.NHibernate/Agendas.Repositories.NHibernate.csproj Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Repositories.NHibernate/Agendas.Repositories.NHibernate.csproj Thu May 19 01:24:56 2011 -0300 @@ -53,7 +53,6 @@ </ItemGroup> <ItemGroup> <Compile Include="EventoRepository.cs" /> - <Compile Include="NhHelper.cs" /> <Compile Include="PonenteRepository.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="RepositoryBase.cs" />
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Repositories.NHibernate/NHibernateSessionPerRequestAttribute.cs Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,36 @@ +using System.Web.Mvc; +using NHibernate; +using NHibernate.Context; + +namespace AltNetHispano.Agendas.Repositories.NHibernate +{ + public class NHibernateSessionPerRequestAttribute: ActionFilterAttribute + { + private readonly ISessionFactory _sessionFactory; + + public NHibernateSessionPerRequestAttribute(ISessionFactory sessionFactory) + { + _sessionFactory = sessionFactory; + } + + public override void OnActionExecuting(ActionExecutingContext filterContext) + { + //iniciar session + var session = _sessionFactory.OpenSession(); + CurrentSessionContext.Bind(session); + + base.OnActionExecuting(filterContext); + } + + public override void OnResultExecuted(ResultExecutedContext filterContext) + { + base.OnResultExecuted(filterContext); + + //cerrar session + var session = _sessionFactory.GetCurrentSession(); + session.Flush(); + session.Close(); + + } + } +} \ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Repositories.NHibernate/NhHelper.cs Tue May 17 16:59:39 2011 -0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -using AltNetHispano.Agendas.Domain; -using ConfOrm; -using ConfOrm.NH; -using NHibernate; -using NHibernate.Cfg; - -namespace AltNetHispano.Agendas.Repositories.NHibernate -{ - public static class NhHelper - { - private static ISessionFactory _sessionfactory; - private static Configuration _cfg; - - public static Configuration GetConfiguration() - { - if (_cfg == null) - { - var orm = new ObjectRelationalMapper(); - orm.TablePerClass<Evento>(); - orm.TablePerClass<Ponente>(); - - orm.Cascade<Evento, Ponente>(Cascade.None); - orm.Cascade<Ponente, Evento>(Cascade.None); - - var mapper = new Mapper(orm); - - var mapping = mapper.CompileMappingFor(typeof(Evento).Assembly.GetTypes()); - - _cfg = new Configuration(); - _cfg.Configure(); - _cfg.AddDeserializedMapping(mapping, null); - } - return _cfg; - } - - public static ISessionFactory GetSessionFactory() - { - if (_sessionfactory == null) - { - var cfg = GetConfiguration(); - _sessionfactory=cfg.BuildSessionFactory(); - } - return _sessionfactory; - } - } -} \ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Tests/Agendas.Tests.csproj Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Tests/Agendas.Tests.csproj Thu May 19 01:24:56 2011 -0300 @@ -89,6 +89,10 @@ <Project>{45804F1F-EB72-4F82-A227-088CAD9B785D}</Project> <Name>Agendas.Google</Name> </ProjectReference> + <ProjectReference Include="..\Agendas.NHibernate\Agendas.NHibernate.csproj"> + <Project>{9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}</Project> + <Name>Agendas.NHibernate</Name> + </ProjectReference> <ProjectReference Include="..\Agendas.Repositories.Memory\Agendas.Repositories.Memory.csproj"> <Project>{28C5EBFB-EE69-4765-A880-D4DE0BC89F48}</Project> <Name>Agendas.Repositories.Memory</Name>
--- a/Agendas/trunk/src/Agendas.Tests/Cruds/EventoCrudNhTests.cs Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Tests/Cruds/EventoCrudNhTests.cs Thu May 19 01:24:56 2011 -0300 @@ -1,4 +1,5 @@ using System; +using Agendas.NHibernate; using AltNetHispano.Agendas.Domain; using AltNetHispano.Agendas.Repositories.NHibernate; using Moq;
--- a/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Thu May 19 01:24:56 2011 -0300 @@ -74,6 +74,9 @@ <ItemGroup> <Content Include="Content\altnetlogo.png" /> <Content Include="Global.asax" /> + <Content Include="hibernate.cfg.xml"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </Content> <Content Include="Web.config"> <SubType>Designer</SubType> </Content> @@ -184,4 +187,8 @@ </FlavorProperties> </VisualStudio> </ProjectExtensions> + <PropertyGroup> + <PostBuildEvent>copy "$(SolutionDir)packages\NHibernate.Castle.3.1.0.4000\lib\Net35\NHibernate.ByteCode.Castle.dll" "$(TargetDir)" & +copy "$(SolutionDir)packages\Castle.Core.2.5.2\lib\NET35\Castle.Core.dll" "$(TargetDir)"</PostBuildEvent> + </PropertyGroup> </Project> \ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Web/Global.asax.cs Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Global.asax.cs Thu May 19 01:24:56 2011 -0300 @@ -13,6 +13,7 @@ public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); + filters.Add(AttributeFactory.GetNHibernateSessionPerAction()); } public static void RegisterRoutes(RouteCollection routes)
--- a/Agendas/trunk/src/Agendas.Web/Web.config Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Web.config Thu May 19 01:24:56 2011 -0300 @@ -9,6 +9,8 @@ <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> + <add name="AltNetHispano" connectionString="Data Source=.\sqlexpress;Initial Catalog=AltNetHispano;Integrated Security=SSPI"/> + </connectionStrings> <appSettings>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/hibernate.cfg.xml Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > + <session-factory> + <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> + <property name="connection.connection_string_name">AltNetHispano</property> + <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> + + <property name="current_session_context_class">web</property> + <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> + </session-factory> +</hibernate-configuration> \ No newline at end of file
--- a/Agendas/trunk/src/Agendas.sln Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.sln Thu May 19 01:24:56 2011 -0300 @@ -24,6 +24,8 @@ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agendas.Repositories.NHibernate", "Agendas.Repositories.NHibernate\Agendas.Repositories.NHibernate.csproj", "{0973DF44-3B90-4D2A-B579-C64C93B6C853}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agendas.NHibernate", "Agendas.NHibernate\Agendas.NHibernate.csproj", "{9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -62,6 +64,10 @@ {0973DF44-3B90-4D2A-B579-C64C93B6C853}.Debug|Any CPU.Build.0 = Debug|Any CPU {0973DF44-3B90-4D2A-B579-C64C93B6C853}.Release|Any CPU.ActiveCfg = Release|Any CPU {0973DF44-3B90-4D2A-B579-C64C93B6C853}.Release|Any CPU.Build.0 = Release|Any CPU + {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE
--- a/Agendas/trunk/src/install packages.bat Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/install packages.bat Thu May 19 01:24:56 2011 -0300 @@ -1,4 +1,5 @@ ..\tools\nuget i Agendas.Tests\packages.config -o packages ..\tools\nuget i Agendas.Repositories.NHibernate\packages.config -o packages +..\tools\nuget i Agendas.NHibernate\packages.config -o packages PAUSE \ No newline at end of file