Esempio n. 1
0
        static void Main(string[] args)
        {
            ArgsParser parser = new ArgsParser(
                new Parameter("assembly", "a", "the assembly from which the mapping should be generated"),
                new Parameter("model", "m", "the model from which the mapping should be generated"),
                new Parameter("namespace", "n", "maps only the namespace(s) you specify"),
                new Parameter("driver", "d", true, "the SQL driver to use, eg. SqlServer"),
                new Parameter("output", "o", true, "the file where the mapping should be generated")
            );
            parser.Description = "This tool helps you to generate a default mapping. Here are the available commands :";
            if (parser.Parse(args))
            {
                Model model = new Model();
                var visitor = new ModelMetaDataVisitor(model);
                string assembly = parser["assembly"];
                if (assembly != null)
                {
                    string @namespace = parser["namespace"];
                    string[] namespaces = null;
                    if (@namespace != null)
                        namespaces = @namespace.Split(' ');
                    foreach (IMetaData metadata in MetaDataFactory.FromAssembly(Assembly.LoadFile(assembly), namespaces))
                        metadata.Accept(visitor);
                }

                Mapping mapping = new Mapping();
                Type driver = Type.GetType(parser["driver"]);
                if (driver == null)
                    driver = Type.GetType("Evaluant.Uss.SqlMapper.Drivers." + parser["driver"] + ", Evaluant.Uss.SqlMapper");
                if (driver == null)
                {
                    Console.WriteLine("The driver named " + parser["driver"] + " could not be found");
                    return;
                }
                IMapper mapper = new DefaultMapper((IDriver)Activator.CreateInstance(driver), mapping);
                mapper.Map(model);
                XmlSerializer serializer = new XmlSerializer(typeof(Mapping));
                using (Stream s = File.OpenWrite(parser["output"]))
                {
                    serializer.Serialize(s, mapping);
                    s.Close();
                }
                Console.WriteLine("The mapping was generated successfully...");
                Console.ReadLine();
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            if (args.Length > 1)
            {
                Environment.CurrentDirectory = args[1];
            }
            else
                Environment.CurrentDirectory = args[0].Substring(0, args[0].Length - Path.GetFileName(args[0]).Length);

            if (!File.Exists(args[0]))
                throw new FileNotFoundException(args[0]);
            if (!Path.IsPathRooted(args[0]))
                args[0] = Path.Combine(Environment.CurrentDirectory, args[0]);

            Assembly assembly = Assembly.LoadFile(args[0]);
            Model model = new Model();
            ModelMetaDataVisitor visitor = new ModelMetaDataVisitor(model);
            foreach (IMetaData metadata in MetaDataFactory.FromAssembly(assembly, args.Length > 2 ? args[2] : null))
                metadata.Accept(visitor);

            AssemblyName assemblyName = new AssemblyName();
            assemblyName.Name = assembly.GetName().Name + ".Proxies";

            // Create a new assembly with one module
            AssemblyBuilder newAssembly = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave, Environment.CurrentDirectory);
            newAssembly.SetCustomAttribute(new CustomAttributeBuilder(typeof(ProxyAssemblyAttribute).GetConstructor(Type.EmptyTypes), new object[0]));

            ModuleBuilder moduleBuilder = newAssembly.DefineDynamicModule(assemblyName.Name, assemblyName.Name + ".dll", true);
            EntityResolver resolver = new EntityResolver();
            foreach (Entity e in model.Entities.Values)
            {
                Type t = assembly.GetType(e.Type);
                if (t.IsInterface || t.IsAbstract)
                    continue;
                resolver.GenerateType(t, moduleBuilder, model).CreateType();
            }

            newAssembly.Save(assemblyName.Name + ".dll");
        }
 public virtual void RegisterMetaData(params Evaluant.Uss.MetaData.IMetaData[] metadata)
 {
     ModelMetaDataVisitor visitor = new ModelMetaDataVisitor(Model);
     foreach(IMetaData md in metadata)
         md.Accept(visitor);
 }