示例#1
0
        private static Assembly BuildAssembly(TypeDescriptorProvider typeDescriptorProvider, DataAccessModelConfiguration configuration)
        {
            DataAccessObjectTypeBuilder dataAccessObjectTypeBuilder;

            var hash               = configuration.GetSha1();
            var assemblyName       = new AssemblyName(typeDescriptorProvider.DataAccessModelType.Assembly.GetName().Name + "." + typeDescriptorProvider.DataAccessModelType.Name);
            var sharedAssemblyName = new AssemblyName("Shaolinq.GeneratedDataAccessModel");
            var assemblyBuilder    = AppDomain.CurrentDomain.DefineDynamicAssembly(sharedAssemblyName, AssemblyBuilderAccess.RunAndSave);
            var moduleBuilder      = assemblyBuilder.DefineDynamicModule(assemblyName.Name, assemblyName.Name + "." + hash + ".dll");

            var propertiesBuilder = moduleBuilder.DefineType("$$$DataAccessModelProperties", TypeAttributes.Class, typeof(object));

            var assemblyBuildContext = new AssemblyBuildContext(assemblyBuilder, propertiesBuilder);

            var dataAccessModelTypeBuilder = new DataAccessModelTypeBuilder(assemblyBuildContext, moduleBuilder);

            dataAccessModelTypeBuilder.BuildTypePhase1(typeDescriptorProvider.DataAccessModelType);

            var typeDescriptors = typeDescriptorProvider.GetTypeDescriptors();

            foreach (var typeDescriptor in typeDescriptors)
            {
                dataAccessObjectTypeBuilder = new DataAccessObjectTypeBuilder(typeDescriptorProvider, assemblyBuildContext, moduleBuilder, typeDescriptor.Type);
                dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(1));
            }

            foreach (var typeDescriptor in typeDescriptors)
            {
                dataAccessObjectTypeBuilder = assemblyBuildContext.TypeBuilders[typeDescriptor.Type];
                dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(2));
            }

            assemblyBuildContext.DataAccessModelPropertiesTypeBuilder.CreateType();
            dataAccessModelTypeBuilder.BuildTypePhase2();

            bool saveConcreteAssembly;

            bool.TryParse(ConfigurationManager.AppSettings["Shaolinq.SaveConcreteAssembly"], out saveConcreteAssembly);

#if DEBUG
            const bool isInDebugMode = true;
#else
            const bool isInDebugMode = false;
#endif

            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            if (saveConcreteAssembly || isInDebugMode)
            {
                ActionUtils.IgnoreExceptions(() => assemblyBuilder.Save(assemblyName + "." + hash + ".dll"));
            }

            return(assemblyBuilder);
        }
		private static Assembly BuildAssembly(TypeDescriptorProvider typeDescriptorProvider, DataAccessModelConfiguration configuration)
		{
			DataAccessObjectTypeBuilder dataAccessObjectTypeBuilder;

			var hash = configuration.GetSha1();
			var assemblyName = new AssemblyName(typeDescriptorProvider.DataAccessModelType.Assembly.GetName().Name + "." + typeDescriptorProvider.DataAccessModelType.Name);
			var sharedAssemblyName = new AssemblyName("Shaolinq.GeneratedDataAccessModel");
			var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(sharedAssemblyName, AssemblyBuilderAccess.RunAndSave);
			var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, assemblyName.Name + "." + hash + ".dll");

			var propertiesBuilder = moduleBuilder.DefineType("$$$DataAccessModelProperties", TypeAttributes.Class, typeof(object));
			
			var assemblyBuildContext = new AssemblyBuildContext(assemblyBuilder, propertiesBuilder);

			var dataAccessModelTypeBuilder = new DataAccessModelTypeBuilder(assemblyBuildContext, moduleBuilder);
			dataAccessModelTypeBuilder.BuildTypePhase1(typeDescriptorProvider.DataAccessModelType);

			var typeDescriptors = typeDescriptorProvider.GetTypeDescriptors();
			
			foreach (var typeDescriptor in typeDescriptors)
			{
				dataAccessObjectTypeBuilder = new DataAccessObjectTypeBuilder(typeDescriptorProvider, assemblyBuildContext, moduleBuilder, typeDescriptor.Type);
				dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(1));
			}

			foreach (var typeDescriptor in typeDescriptors)
			{
				dataAccessObjectTypeBuilder = assemblyBuildContext.TypeBuilders[typeDescriptor.Type];
				dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(2));
			}

			assemblyBuildContext.DataAccessModelPropertiesTypeBuilder.CreateType();
			dataAccessModelTypeBuilder.BuildTypePhase2();

			bool saveConcreteAssembly;
			bool.TryParse(ConfigurationManager.AppSettings["Shaolinq.SaveConcreteAssembly"], out saveConcreteAssembly);

#if DEBUG
			const bool isInDebugMode = true;
#else
			const bool isInDebugMode = false;
#endif

			// ReSharper disable once ConditionIsAlwaysTrueOrFalse
			if (saveConcreteAssembly || isInDebugMode)
			{
				ActionUtils.IgnoreExceptions(() => assemblyBuilder.Save(assemblyName + "." + hash + ".dll"));
			}

			return assemblyBuilder;
		}
        private static Assembly BuildAssembly(TypeDescriptorProvider typeDescriptorProvider, DataAccessModelConfiguration configuration)
        {
            DataAccessObjectTypeBuilder dataAccessObjectTypeBuilder;

            var configMd5       = configuration.GetMd5();
            var assemblyName    = new AssemblyName(typeDescriptorProvider.DataAccessModelType.Assembly.GetName().Name + "." + typeDescriptorProvider.DataAccessModelType.Name);
            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
            var moduleBuilder   = assemblyBuilder.DefineDynamicModule(assemblyName.Name, assemblyName.Name + "." + configMd5 + ".dll");

            var assemblyBuildContext = new AssemblyBuildContext(assemblyBuilder);

            var dataAccessModelTypeBuilder = new DataAccessModelTypeBuilder(assemblyBuildContext, moduleBuilder);

            dataAccessModelTypeBuilder.BuildType(typeDescriptorProvider.DataAccessModelType);

            var typeDescriptors = typeDescriptorProvider.GetTypeDescriptors();

            foreach (var typeDescriptor in typeDescriptors)
            {
                dataAccessObjectTypeBuilder = new DataAccessObjectTypeBuilder(typeDescriptorProvider, assemblyBuildContext, moduleBuilder, typeDescriptor.Type);
                dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(1));
            }

            foreach (var typeDescriptor in typeDescriptors)
            {
                dataAccessObjectTypeBuilder = assemblyBuildContext.TypeBuilders[typeDescriptor.Type];
                dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(2));
            }

            bool saveConcreteAssembly;

            bool.TryParse(ConfigurationManager.AppSettings["Shaolinq.SaveConcreteAssembly"], out saveConcreteAssembly);

#if DEBUG
            const bool isInDebugMode = true;
#else
            const bool isInDebugMode = false;
#endif

            if (saveConcreteAssembly || isInDebugMode)
            {
                ActionUtils.IgnoreExceptions(() => assemblyBuilder.Save(assemblyName + "." + configMd5 + ".dll"));
            }

            return(assemblyBuilder);
        }
        private static Assembly BuildAssembly(TypeDescriptorProvider typeDescriptorProvider, DataAccessModelConfiguration configuration)
        {
            DataAccessObjectTypeBuilder dataAccessObjectTypeBuilder;

            var configMd5 = configuration.GetMd5();
            var assemblyName = new AssemblyName(typeDescriptorProvider.DataAccessModelType.Assembly.GetName().Name + "." + typeDescriptorProvider.DataAccessModelType.Name);
            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
            var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, assemblyName.Name + "." + configMd5 + ".dll");

            var assemblyBuildContext = new AssemblyBuildContext(assemblyBuilder);

            var dataAccessModelTypeBuilder = new DataAccessModelTypeBuilder(assemblyBuildContext, moduleBuilder);
            dataAccessModelTypeBuilder.BuildType(typeDescriptorProvider.DataAccessModelType);

            var typeDescriptors = typeDescriptorProvider.GetTypeDescriptors();

            foreach (var typeDescriptor in typeDescriptors)
            {
                dataAccessObjectTypeBuilder = new DataAccessObjectTypeBuilder(typeDescriptorProvider, assemblyBuildContext, moduleBuilder, typeDescriptor.Type);
                dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(1));
            }

            foreach (var typeDescriptor in typeDescriptors)
            {
                dataAccessObjectTypeBuilder = assemblyBuildContext.TypeBuilders[typeDescriptor.Type];
                dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(2));
            }

            bool saveConcreteAssembly;
            bool.TryParse(ConfigurationManager.AppSettings["Shaolinq.SaveConcreteAssembly"], out saveConcreteAssembly);

            #if DEBUG
            const bool isInDebugMode = true;
            #else
            const bool isInDebugMode = false;
            #endif

            if (saveConcreteAssembly || isInDebugMode)
            {
                ActionUtils.IgnoreExceptions(() => assemblyBuilder.Save(assemblyName + "." + configMd5 + ".dll"));
            }

            return assemblyBuilder;
        }
 public DataAccessModelTypeBuilder(AssemblyBuildContext assemblyBuildContext, ModuleBuilder moduleBuilder)
     : base(assemblyBuildContext, moduleBuilder)
 {
 }
		public DataAccessModelTypeBuilder(AssemblyBuildContext assemblyBuildContext, ModuleBuilder moduleBuilder)
			: base(assemblyBuildContext, moduleBuilder)
		{
		}
示例#7
0
		protected BaseTypeBuilder(AssemblyBuildContext assemblyBuildContext, ModuleBuilder moduleBuilder)
		{
			this.ModuleBuilder = moduleBuilder;
			this.AssemblyBuildContext = assemblyBuildContext;
		}
示例#8
0
 protected BaseTypeBuilder(AssemblyBuildContext assemblyBuildContext, ModuleBuilder moduleBuilder)
 {
     this.ModuleBuilder        = moduleBuilder;
     this.AssemblyBuildContext = assemblyBuildContext;
 }
示例#9
0
        private static Assembly BuildAssembly(TypeDescriptorProvider typeDescriptorProvider, DataAccessModelConfiguration configuration)
        {
            DataAccessObjectTypeBuilder dataAccessObjectTypeBuilder;
            var serializedConfiguration = XmlSerializer <DataAccessModelConfigurationUniqueKey> .New().SerializeToString(new DataAccessModelConfigurationUniqueKey(configuration));

            var filename = GetFileName(typeDescriptorProvider, configuration.GeneratedAssembliesSaveDirectory, serializedConfiguration, out var fullhash);

            if (configuration.SaveAndReuseGeneratedAssemblies ?? false)
            {
                if (filename != null && File.Exists(filename))
                {
                    var candidate = Assembly.LoadFile(filename);

                    if (ReadResource(candidate, "configuration.xml") == serializedConfiguration &&
                        ReadResource(candidate, "sha1.txt") == fullhash)
                    {
                        return(candidate);
                    }
                }
            }

            var filenameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
            var typeDescriptors          = typeDescriptorProvider.GetTypeDescriptors();
            var assemblyName             = new AssemblyName(filenameWithoutExtension);
            var assemblyBuilder          = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetDirectoryName(filename));
            var moduleBuilder            = assemblyBuilder.DefineDynamicModule(filenameWithoutExtension, filenameWithoutExtension + ".dll");

            assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(TypeUtils.GetConstructor(() => new GeneratedAssemblyAttribute()), Type.EmptyTypes));

            var propertiesBuilder    = moduleBuilder.DefineType("$$$DataAccessModelProperties", TypeAttributes.Class, typeof(object));
            var assemblyBuildContext = new AssemblyBuildContext(assemblyBuilder, moduleBuilder, propertiesBuilder);

            var dataAccessModelTypeBuilder = new DataAccessModelTypeBuilder(assemblyBuildContext, moduleBuilder);

            dataAccessModelTypeBuilder.BuildTypePhase1(typeDescriptorProvider.DataAccessModelType);

            foreach (var typeDescriptor in typeDescriptors)
            {
                dataAccessObjectTypeBuilder = new DataAccessObjectTypeBuilder(typeDescriptorProvider, assemblyBuildContext, moduleBuilder, typeDescriptor.Type);
                dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(1));
            }

            foreach (var typeDescriptor in typeDescriptors)
            {
                dataAccessObjectTypeBuilder = assemblyBuildContext.TypeBuilders[typeDescriptor.Type];
                dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(2));
            }

            assemblyBuildContext.DataAccessModelPropertiesTypeBuilder.CreateType();
            assemblyBuildContext.FinishConstantsContainer();
            assemblyBuildContext.ConstantsContainer.CreateType();

            dataAccessModelTypeBuilder.BuildTypePhase2();

#if DEBUG
            const bool isInDebugMode = true;
#else
            const bool isInDebugMode = false;
#endif

            var saveAssembly = configuration.SaveAndReuseGeneratedAssemblies ?? !isInDebugMode;

            if (saveAssembly)
            {
                try
                {
                    moduleBuilder.DefineManifestResource("configuration.xml", new MemoryStream(Encoding.UTF8.GetBytes(serializedConfiguration)), ResourceAttributes.Public);
                    moduleBuilder.DefineManifestResource("sha1.txt", new MemoryStream(Encoding.UTF8.GetBytes(fullhash)), ResourceAttributes.Public);

                    assemblyBuilder.Save(Path.GetFileName(filename));
                }
                catch
                {
                }
            }

            return(assemblyBuilder);
        }