コード例 #1
0
        private static TInterface CreateExtension <TInterface>(IElementContainer target, string name, Guid productId, string toolkitId,
                                                               Func <Action <IProduct>, bool, IProduct> elementFactory,
                                                               Action <TInterface> initializer = null, bool raiseInstantiateEvents = true)
            where TInterface : class
        {
            Guard.NotNull(() => target, target);
            Guard.NotNullOrEmpty(() => name, name);
            Guard.NotNullOrEmpty(() => toolkitId, toolkitId);

            if (initializer == null)
            {
                initializer = e => { }
            }
            ;

            GetToolkitInterfaceAttributeOrThrow(typeof(TInterface));
            using (var transaction = target.BeginTransaction())
            {
                var element = elementFactory(e =>
                {
                    e.DefinitionId = productId;
                    e.ExtensionId  = toolkitId;
                    e.InstanceName = name;
                }, raiseInstantiateEvents);

                var interfaceLayer = GetInterfaceLayer <TInterface, IProduct>(element);
                initializer(interfaceLayer);

                transaction.Commit();

                return(interfaceLayer);
            }
        }