Guest User

PersistenceFacility

a guest
Aug 10th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1.     public class PersistenceFacility : AbstractFacility
  2.     {
  3.         protected override void Init()
  4.         {
  5.             var config = BuildDatabaseConfiguration();
  6.            
  7.             Kernel.Register(
  8.                 Component.For<ISessionFactory>()
  9.                     .UsingFactoryMethod(_ => config.BuildSessionFactory()),
  10.                 Component.For<ISession>()
  11.                     .UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
  12.                     .LifestylePerWebRequest());
  13.         }
  14.  
  15.         private FluentConfiguration BuildDatabaseConfiguration()
  16.         {
  17.             return Fluently.Configure()
  18.                 .Database(MsSqlConfiguration.MsSql2008
  19.                               .ConnectionString(
  20.                               @"Data Source=localhost\SQLEXPRESS;Initial Catalog=TestDb;Integrated Security=true")
  21.                                   .UseOuterJoin()
  22.                               .ShowSql())
  23.                 .Mappings(m =>
  24.                     m.FluentMappings.AddFromAssemblyOf<ProductMap>())
  25.                           //m.AutoMappings
  26.                               //.Add(CreateMappingModel()))
  27.                 .ExposeConfiguration(ConfigurePersistence)
  28.                 .ExposeConfiguration(BuildSchema);
  29.         }
  30.  
  31.         protected virtual void ConfigurePersistence(Configuration config)
  32.         {
  33.             SchemaMetadataUpdater.QuoteTableAndColumns(config);
  34.         }
  35.  
  36.         protected virtual bool IsDomainEntity(Type t)
  37.         {
  38.             return typeof(EntityBase).IsAssignableFrom(t);
  39.         }
  40.  
  41.         protected virtual AutoPersistenceModel CreateMappingModel()
  42.         {
  43.             var m = AutoMap.Assembly(typeof(EntityBase).Assembly)
  44.             .Where(IsDomainEntity)
  45.             .OverrideAll(ShouldIgnoreProperty)
  46.             .IgnoreBase<EntityBase>();
  47.  
  48.             return m;
  49.         }
  50.  
  51.         private void ShouldIgnoreProperty(IPropertyIgnorer property)
  52.         {
  53.             property.IgnoreProperties(p => p.MemberInfo.HasAttribute<DoNotMapAttribute>());
  54.         }
  55.  
  56.         private void BuildSchema(Configuration configuration)
  57.         {
  58.             new SchemaExport(configuration)
  59.            .Create(false, true);
  60.         }
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment