/// <summary>
        /// The context constructor should only be called once. After the context has been created call GetContext for specific copies
        /// </summary>
        /// <param name="loader">The loader used to load classes.</param>
        /// <param name="datas">Custom data handlers.</param>
        public Context(IConfigurationLoader loader, IEnumerable<AbstractSitecoreDataHandler> datas)
        {
            //the setup must only run if the context has not been setup
            //second attempts to setup a context should throw an error
            if (!_contextLoaded)
            {
                lock (_lock)
                {
                    if (!_contextLoaded)
                    {

                        //load all classes
                        var classes = loader.Load().ToDictionary();

                        datas = LoadDefaultDataHandlers(datas);

                        InstanceContext instance = new InstanceContext(classes, datas);
                        StaticContext = instance;

                        //now assign a data handler to each property
                        foreach (var cls in classes)
                        {
                            IList<AbstractSitecoreDataHandler> handlers = new List<AbstractSitecoreDataHandler>();

                            foreach (var prop in cls.Value.Properties)
                            {
                                
                                var handler =  instance.GetDataHandler(prop);
                              
                                //set the ID property of the class
                                //the ID property is needed later for writing and page editing, 
                                //saves time having to look it
                                if (prop.Attribute is SitecoreIdAttribute)
                                    cls.Value.IdProperty = prop;
                                else if (prop.Attribute is SitecoreInfoAttribute
                                    && prop.Attribute.CastTo<SitecoreInfoAttribute>().Type == SitecoreInfoType.Language)
                                    cls.Value.LanguageProperty = prop;
                                else if (prop.Attribute is SitecoreInfoAttribute
                                   && prop.Attribute.CastTo<SitecoreInfoAttribute>().Type == SitecoreInfoType.Version)
                                    cls.Value.VersionProperty = prop;

                                
                                handlers.Add(handler);

                            }
                            cls.Value.DataHandlers = handlers;
                        }
                        
                       

                    }
                }
            }
            else
                throw new MapperException ("Context already loaded");
        }
        /// <summary>
        /// The context constructor should only be called once. After the context has been created call GetContext for specific copies
        /// </summary>
        /// <param name="loader">The loader used to load classes.</param>
        public Context(params AbstractConfigurationLoader [] loaders)
        {
            //the setup must only run if the context has not been setup
            //second attempts to setup a context should throw an error
            if (!_contextLoaded)
            {
                lock (_lock)
                {
                    if (!_contextLoaded)
                    {

                        //load all classes
                        List<SitecoreClassConfig> configs = new List<SitecoreClassConfig>();
                        List<AbstractSitecoreDataHandler> dataHandlers = new List<AbstractSitecoreDataHandler>();
                       
                        foreach(var loader in loaders){
                            configs.AddRange(loader.Load());
                            if(loader.DataHandlers != null)
                                dataHandlers.AddRange(loader.DataHandlers);
                        }
                                                
                        var classes = configs.ToDictionary();

                        InstanceContext instance = new InstanceContext(classes, dataHandlers);
                        StaticContext = instance;

                        //now assign a data handler to each property
                        foreach (var cls in classes)
                        {
                            IList<AbstractSitecoreDataHandler> handlers = new List<AbstractSitecoreDataHandler>();

                            //create constructors

                            CreateConstructorDelegates(cls.Value);

                            foreach (var prop in cls.Value.Properties)
                            {

                                var handler = instance.GetDataHandler(prop);

                                //set the ID property of the class
                                //the ID property is needed later for writing and page editing, 
                                //saves time having to look it
                                if (prop.Attribute is SitecoreIdAttribute)
                                    cls.Value.IdProperty = prop;
                                else if (prop.Attribute is SitecoreInfoAttribute
                                    && prop.Attribute.CastTo<SitecoreInfoAttribute>().Type == SitecoreInfoType.Language)
                                    cls.Value.LanguageProperty = prop;
                                else if (prop.Attribute is SitecoreInfoAttribute
                                   && prop.Attribute.CastTo<SitecoreInfoAttribute>().Type == SitecoreInfoType.Version)
                                    cls.Value.VersionProperty = prop;


                                handlers.Add(handler);

                            }
                            cls.Value.DataHandlers = handlers;
                        }



                    }
                }
            }
            else
                throw new MapperException("Context already loaded");

        }
Exemple #3
0
        /// <summary>
        /// The context constructor should only be called once. After the context has been created call GetContext for specific copies
        /// </summary>
        /// <param name="loader">The loader used to load classes.</param>
        public Context(params AbstractConfigurationLoader [] loaders)
        {
            //the setup must only run if the context has not been setup
            //second attempts to setup a context should throw an error
            if (!_contextLoaded)
            {
                lock (_lock)
                {
                    if (!_contextLoaded)
                    {
                        //load all classes
                        List <SitecoreClassConfig>         configs      = new List <SitecoreClassConfig>();
                        List <AbstractSitecoreDataHandler> dataHandlers = new List <AbstractSitecoreDataHandler>();

                        foreach (var loader in loaders)
                        {
                            configs.AddRange(loader.Load());
                            if (loader.DataHandlers != null)
                            {
                                dataHandlers.AddRange(loader.DataHandlers);
                            }
                        }

                        var classes = configs.ToDictionary();

                        InstanceContext instance = new InstanceContext(classes, dataHandlers, loaders);
                        StaticContext = instance;

                        //now assign a data handler to each property
                        foreach (var cls in classes)
                        {
                            IList <AbstractSitecoreDataHandler> handlers = new List <AbstractSitecoreDataHandler>();

                            //create constructors

                            CreateConstructorDelegates(cls.Value);

                            foreach (var prop in cls.Value.Properties)
                            {
                                var handler = instance.GetDataHandler(prop);

                                //set the ID property of the class
                                //the ID property is needed later for writing and page editing,
                                //saves time having to look it
                                if (prop.Attribute is SitecoreIdAttribute)
                                {
                                    cls.Value.IdProperty = prop;
                                }
                                else if (prop.Attribute is SitecoreInfoAttribute &&
                                         prop.Attribute.CastTo <SitecoreInfoAttribute>().Type == SitecoreInfoType.Language)
                                {
                                    cls.Value.LanguageProperty = prop;
                                }
                                else if (prop.Attribute is SitecoreInfoAttribute &&
                                         prop.Attribute.CastTo <SitecoreInfoAttribute>().Type == SitecoreInfoType.Version)
                                {
                                    cls.Value.VersionProperty = prop;
                                }


                                handlers.Add(handler);
                            }
                            cls.Value.DataHandlers = handlers;
                        }
                    }
                }
            }
            else
            {
                throw new MapperException("Context already loaded");
            }
        }