Пример #1
0
        private CreateClassData CreateInteractor(MakeUseCaseParameter param)
        {
            var className      = $"Mock{param.CompleteName}Interactor";
            var classNamespace = $"MockUseCase.{param.ControllerName}";

            var meta = new ClassMeta(classNamespace, className);

            meta.SetupUsing()
            .AddUsing("NrsLib.SequentiallyJsonDataLoader")
            .AddUsing("UseCase." + param.ControllerName + "." + param.ActionName);

            meta.SetupImplements()
            .AddImplements("I" + param.CompleteName + "UseCase");

            meta.SetupFields()
            .AddField("jsonsLoader",
                      field => field.SetReadOnly(true).SetType("JsonsLoader"));

            meta.SetupConstructor()
            .AddConstructor(constructor => constructor
                            .SetAccessLevel(AccessLevel.Public)
                            .AddArgument("jsonsLoader", "JsonsLoader")
                            .AddBody("this.jsonsLoader = jsonsLoader;"));

            var responseClassName = param.CompleteName + "Response";

            meta.SetupMethods()
            .AddMethod("Handle", method => method
                       .SetReturnType(responseClassName)
                       .AddArgument("request", param.CompleteName + "Request")
                       .SetAccessLevel(AccessLevel.Public)
                       .AddBody($"return jsonsLoader.Generate<{responseClassName}>();")
                       );

            var content = classFileGenerateDriver.Create(meta, Language.CSharp);

            return(new CreateClassData(
                       param.ControllerName,
                       className + ".cs",
                       content
                       ));
        }
Пример #2
0
        static void Main(string[] args)
        {
            var classMeta = new ClassMeta("TestNameSpace.Test", "TestClass");

            // setup partial class
            classMeta.SetupClass()
            .SetPartial(true);

            // setup using
            classMeta.SetupUsing()
            .AddUsing("System")
            .AddUsing("System.Linq");

            // setup implements
            classMeta.SetupImplements()
            .AddImplements("ISuperClass")
            .AddImplements("ISuperClassWithGeneric", implement => implement.AddGeneric("string"));

            // setup constructor
            classMeta.SetupConstructor()
            .AddConstructor(constructor => constructor
                            .SetAccessLevel(AccessLevel.Public)
                            .AddArgument("args", "string[]")
                            .AddArgument("num", "int")
                            .AddBody("Console.WriteLine(\"Instantiated\");"))
            .AddConstructor(constructor => constructor
                            .SetAccessLevel(AccessLevel.Public)
                            .AddArgument("args", "string[]")
                            .SetCallThis("args", "1"));

            // setup field
            classMeta.SetupFields()
            .AddField("testField", field => field.SetType("int").SetValue("1"));

            // setup simple public method
            classMeta.SetupMethods()
            .AddMethod("TestPublicMethod", method => method.AddArgument("test", "string").SetAccessLevel(AccessLevel.Public));

            // setup no body method
            classMeta.SetupMethods()
            .AddMethod("NoBodyMethod", method => method.NoBodyOnNoLine(true));

            // setup complex method
            classMeta.SetupMethods()
            .AddMethod("ComplexMethod", method =>
                       method.SetReturnType("int")
                       .AddBody("int i = 0;", "i++;", "return i;")
                       .AddGenerics("T", generics => generics.AddWhere("class").AddWhere("new()"))
                       .AddGenerics("TT", generics => generics.AddWhere("class"))
                       .AddArgument("t", "T")
                       );

            classMeta.SetupProperty()
            .AddProperty("test");

            // if you don't want to use lambda
            var tmpMethod = new MethodDefinition("TempMethod");

            tmpMethod
            .AddBody("Console.WriteLine(\"Hello world\");")
            .SetAccessLevel(AccessLevel.Public)
            .AddArgument("args", "string[]");
            classMeta.SetupMethods()
            .AddMethod(tmpMethod);

            var interfaceMeta = new InterfaceMeta("TestNameSpace.Test", "TestInterface");

            interfaceMeta.SetupUsing()
            .AddUsing("System.Linq");

            interfaceMeta.SetupImplements()
            .AddImplements("SuperClass", x => x.AddGeneric("string"));

            interfaceMeta.SetupMethod()
            .AddMethod("TestMethod", x => x.AddArgument("test", "string"))
            .AddMethod("Test2Method")
            .AddMethod("TestGenericsMethod", method =>
                       method.AddGenerics("TKey", generics => generics.AddWhere("class").AddWhere("new()"))
                       .AddGenerics("TVal", generics => generics.AddWhere("struct"))
                       );

            // create
            var driver        = new MainDriver();
            var classText     = driver.Create(classMeta, Language.CSharp);
            var interfaceText = driver.Create(interfaceMeta, Language.CSharp);

            Console.WriteLine("---- class -----");
            Console.WriteLine(classText);
            Console.WriteLine("---- interface -----");
            Console.WriteLine(interfaceText);
            Console.WriteLine("push any key to end");
            Console.ReadKey();
        }