Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alex 22 posts 105 karma points
    Apr 02, 2012 @ 05:47
    Alex
    2

    Code review: Umbraco 5.x - Create instance of HiveManager from another application

    Hello,

    I finally managed to create an instance of HiveManager from another application (in my case it's console application). I used Umbraco unit tests as a source of information how it works and how it should be setup.

    It works and it reads data from database and display data to me, but I understand only 20% of the code I created.

    If somebody have a deep knowledge in Umbraco 5.x API, could you please review my code and give some feedback - am I doing it right way, what I have to change, what I can improve?

    I posted whole application source code and explanation what I understand there:

    How to create instance of HiveManager

    Thanks,
    Alexander
    http://yaplex.com/

  • Alex 22 posts 105 karma points
    Apr 02, 2012 @ 05:49
    Alex
    1

    And also the code if you don't want to download it:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using NHibernate;
    using Umbraco.Framework;
    using Umbraco.Framework.Context;
    using Umbraco.Framework.Localization;
    using Umbraco.Framework.Localization.Configuration;
    using Umbraco.Framework.Persistence.Model.Attribution.MetaData;
    using Umbraco.Framework.Persistence.NHibernate;
    using Umbraco.Framework.Persistence.NHibernate.Dependencies;
    using Umbraco.Framework.Persistence.NHibernate.OrmConfig;
    using Umbraco.Framework.Persistence.ProviderSupport._Revised;
    using Umbraco.Framework.Persistence.RdbmsModel.Mapping;
    using Umbraco.Framework.Tasks;
    using Umbraco.Framework.TypeMapping;
    using Umbraco.Hive;
    using Umbraco.Hive.Configuration;
    using Umbraco.Hive.ProviderSupport;
    using Configuration = NHibernate.Cfg.Configuration;
    
    namespace WorkingWithHive
    {
        public class HiveManagerWrapper
        {
            public IHiveManager GetHiveManager()
            {
                IFrameworkContext frameworkContext = GetFrameworkContext();
    
                var providerMetadata = new ProviderMetadata("p__nhibernate"new Uri("content://"), truefalse);
    
                var builder =
                    new NHibernateConfigBuilder(
                        ConfigurationManager.ConnectionStrings["DatabaseConnString"].ConnectionString,
                        "unit-tester"SupportedNHDrivers.MsSql2008"call"falsefalse);
    
                NhConfigurationCacheKey cacheKey;
                Configuration config = builder.BuildConfiguration(falseout cacheKey);
    
                var nhHelper = new NhFactoryHelper(config, nullfalsefalse, frameworkContext);
                ISession session;
                nhHelper.GenerateSessionAndTransaction(falseout session);
    
                ProviderDependencyHelper dependencyHelper = new DependencyHelper(nhHelper, providerMetadata);
                var revisionRepositoryFactory = new RevisionRepositoryFactory(providerMetadata, frameworkContext,
                                                                              dependencyHelper);
    
                var revisionSchemaSessionFactory = new NullProviderRevisionRepositoryFactory<EntitySchema>(providerMetadata,
                                                                                                           frameworkContext);
                var schemaRepositoryFactory = new SchemaRepositoryFactory(providerMetadata, revisionSchemaSessionFactory,
                                                                          frameworkContext, dependencyHelper);
    
                ProviderSetup singleWriter = GetWriterProvider(providerMetadata, revisionRepositoryFactory,
                                                               schemaRepositoryFactory, frameworkContext, dependencyHelper);
    
                ReadonlyProviderSetup singleReader = GetReaderProvider(providerMetadata, revisionRepositoryFactory,
                                                                       schemaRepositoryFactory, frameworkContext,
                                                                       dependencyHelper, config);
                IHiveManager hive = new HiveManager(new[]
                                                        {
                                                            new ProviderMappingGroup(
                                                                "test",
                                                                new WildcardUriMatch("content://"),
                                                                singleReader,
                                                                singleWriter,
                                                                frameworkContext)
                                                        }, frameworkContext);
                return hive;
            }
    
            private ReadonlyProviderSetup GetReaderProvider(ProviderMetadata providerMetadata,
                                                            RevisionRepositoryFactory revisionRepositoryFactory,
                                                            SchemaRepositoryFactory schemaRepositoryFactory,
                                                            IFrameworkContext frameworkContext,
                                                            ProviderDependencyHelper dependencyHelper, Configuration config)
            {
                AbstractReadonlyEntityRepositoryFactory readonlyEntityRepositoryFactory =
                    new EntityRepositoryFactory(providerMetadata, revisionRepositoryFactory, schemaRepositoryFactory,
                                                frameworkContext, dependencyHelper);
    
                var readonlyUnitFactory = new ReadonlyProviderUnitFactory(readonlyEntityRepositoryFactory);
                AbstractProviderBootstrapper bootstrapper = new ProviderBootstrapper(config, null);
                var singleReader = new ReadonlyProviderSetup(readonlyUnitFactory, providerMetadata, frameworkContext,
                                                             bootstrapper, 0);
                return singleReader;
            }
    
            private ProviderSetup GetWriterProvider(ProviderMetadata providerMetadata,
                                                    RevisionRepositoryFactory revisionRepositoryFactory,
                                                    SchemaRepositoryFactory schemaRepositoryFactory,
                                                    IFrameworkContext frameworkContext,
                                                    ProviderDependencyHelper dependencyHelper)
            {
                var entityRepositoryFactory = new EntityRepositoryFactory(providerMetadata, revisionRepositoryFactory,
                                                                          schemaRepositoryFactory, frameworkContext,
                                                                          dependencyHelper);
                var unitFactory = new ProviderUnitFactory(entityRepositoryFactory);
                var singleWriter = new ProviderSetup(unitFactory, providerMetadata, frameworkContext, null, 0);
                return singleWriter;
            }
    
            private IFrameworkContext GetFrameworkContext()
            {
                TextManager textManager = LocalizationConfig.SetupDefault();
                MappingEngineCollection typeMappers = GetTypeMappers();
    
                AbstractScopedCache scopedCache = new DictionaryScopedCache();
                AbstractApplicationCache applicationCache = new HttpRuntimeApplicationCache();
                AbstractFinalizer finalizer = new NestedLifetimeFinalizer();
                var taskMgr = new ApplicationTaskManager(Enumerable.Empty<Lazy<AbstractTaskTaskMetadata>>());
                IFrameworkContext frameworkContext = new DefaultFrameworkContext(textManager, typeMappers, scopedCache,
                                                                                 applicationCache, finalizer, taskMgr);
    
                return frameworkContext;
            }
    
            private MappingEngineCollection GetTypeMappers()
            {
                var webmModelMapper = new RdbmsModelMapper(nullnull);
                var binders = new List<Lazy<AbstractMappingEngineTypeMapperMetadata>>();
                var metadata = new TypeMapperMetadata(true);
                var bind = new Lazy<AbstractMappingEngineTypeMapperMetadata>(() => webmModelMapper, metadata);
                binders.Add(bind);
                binders.Add(new Lazy<AbstractMappingEngineTypeMapperMetadata>(() => new ManualMapperv2(nullnull),
                                                                                metadata));
                var typeMappers = new MappingEngineCollection(binders);
                typeMappers.Configure();
                return typeMappers;
            }
        }
    }
  • Jonas Eriksson 930 posts 1825 karma points
    Apr 18, 2012 @ 10:54
    Jonas Eriksson
    0

    Very nice Alex, thanks alot for posting this. I used your project as is and added a SqlCe database from a site, worked nice.

    I added a Umbraco.sdf from a test-site I had + reference to the sqlce 4 dll. For some reason I'm not really sure I had to reference to the explicit version in web config, so here's what it finally turned out looking like:

      <?xml version="1.0"?>
    <configuration>
      <connectionStrings>
        <add name="DatabaseConnString" connectionString="Data Source=Umbraco.sdf" providerName="System.Data.SqlServerCe.4.0" />
      </connectionStrings>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <qualifyAssembly partialName="System.Data.SqlServerCe" fullName="System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
        </assemblyBinding>
      </runtime>
    </configuration>
    

    The Umbraco.sdf needs to be in the root and have "copy always" with this confs. Can obviously be set to a fixed path if one wants it to be kept as is.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies