Пример #1
0
        private static string GenerateSharpCode(Type serverInterface, ISomeClient client) {
            var instantName = "RC" + client.GetType().Name + serverInterface.Name;
            var nameSpaceName = client.GetType().Name + "Caller";

            var codeRoot = CreateDomObject(serverInterface, client, nameSpaceName, instantName);

            return CreateSrc(codeRoot);
        }
Пример #2
0
        static CodeCompileUnit CreateDomObject(Type serverInterface, ISomeClient client, string nameSpaceName, string instantName)
        {
            var codeRoot = new CodeCompileUnit();

            var codeNamespace = new CodeNamespace(nameSpaceName);

            codeNamespace.Imports.Add(new CodeNamespaceImport("System"));
            codeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            codeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections"));
            //codeNamespace.Imports.Add(new CodeNamespaceImport("System.Linq"));


            foreach (var nmsp in GetTypeNamespaces(client.GetType()))
            {
                codeNamespace.Imports.Add(new CodeNamespaceImport(nmsp));
            }

            foreach (var nmsp in GetTypeNamespaces(serverInterface))
            {
                codeNamespace.Imports.Add(new CodeNamespaceImport(nmsp));
            }

            codeRoot.Namespaces.Add(codeNamespace);

            var targetClass = new CodeTypeDeclaration(instantName);

            targetClass.IsClass        = true;
            targetClass.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed;
            targetClass.BaseTypes.Add(serverInterface);

            targetClass.Members.Add(AddField(NameOfClientField, typeof(ISomeClient)));

            targetClass.Members.Add(CreateCtor());


            const BindingFlags methodFlags =
                BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance;

            //var envAtt = (EnviromentAttribute) serverInterface.GetCustomAttribute(typeof(EnviromentAttribute));
            var envAtt = (EnviromentAttribute)serverInterface.GetCustomAttributes(typeof(EnviromentAttribute), true).FirstOrDefault();

            foreach (var interfaceMethodInfo in serverInterface.GetMethods(methodFlags))
            {
                var funcArgs = interfaceMethodInfo.GetParameters().ToArray();
                var retType  = interfaceMethodInfo.ReturnType;

                Func <string, bool> isEmpty = string.IsNullOrWhiteSpace;
                var defineName = isEmpty(envAtt.EnviromentName)
                    ? interfaceMethodInfo.Name
                    : $"{envAtt.EnviromentName}:{interfaceMethodInfo.Name}";

                targetClass.Members.Add(CreateMethod(interfaceMethodInfo.Name, defineName, funcArgs, retType));
            }

            codeNamespace.Types.Add(targetClass);

            return(codeRoot);
        }
Пример #3
0
        public static object CreateTcpCaller(Type serverInterface, ISomeClient client)
        {
            //var code = GenerateSharpCode(serverInterface, client);

            var instantName   = "RC" + client.GetType().Name + serverInterface.Name;
            var nameSpaceName = client.GetType().Name + "Caller";

            var fullName = $"{nameSpaceName}.{instantName}";

            Type callerType = null;

            lock (_callerTypeStore) { // TODO improve lock
                if (_callerTypeStore.TryGetValue(fullName, out callerType))
                {
                    return(Activator.CreateInstance(callerType, client));
                }

                var codeRoot = CreateDomObject(serverInterface, client, nameSpaceName, instantName);

                var compiler = CodeDomProvider.CreateProvider("CSharp");

                var DOMref =
                    AppDomain.CurrentDomain.GetAssemblies()
                    .Where(obj => !obj.IsDynamic)
                    .Select(obj => obj.Location)
                    .ToList();

                var currentAssembly = Assembly.GetExecutingAssembly();
                DOMref.Add(currentAssembly.Location);

                var compilerParams = new CompilerParameters(DOMref.ToArray());
                compilerParams.GenerateInMemory        = true;
                compilerParams.GenerateExecutable      = false;
                compilerParams.IncludeDebugInformation = false;

                var cr = compiler.CompileAssemblyFromDom(compilerParams, codeRoot);

                foreach (var ce in cr.Errors)
                {
                    throw new Exception(ce.ToString());
                }

                callerType = cr.CompiledAssembly.GetType($"{nameSpaceName}.{instantName}");

                _callerTypeStore[fullName] = callerType;
            }

            return(Activator.CreateInstance(callerType, client));
        }
Пример #4
0
        public void DoStuff_GivenStuff_ExpectedStuff()
        {
            //Arrange
            ISomeRepository      repository = A.Fake <ISomeRepository>();
            ISomeClient          client     = A.Fake <ISomeClient>();
            ISomeLogger          logger     = A.Fake <ISomeLogger>();
            IImprovedSomeService service    = new DependencyInjectionMakesUnitTestingSimple.ImprovedSomeService(repository,
                                                                                                                client,
                                                                                                                logger);
            //Act
            var result = service.DoStuff();

            //Assert
            A.CallTo(() => repository.WriteStuffToDb()).MustHaveHappened();
            A.CallTo(() => client.SomeCallToWebService()).MustHaveHappened();
            A.CallTo(() => logger.LogStuff()).MustHaveHappened();
            Assert.True(result);
        }
Пример #5
0
        public ImprovedSomeService(ISomeRepository someRepository,
                                   ISomeClient someClient,
                                   ISomeLogger someLogger)
        {
            if (someRepository == null)
            {
                throw new ArgumentNullException(nameof(someRepository));
            }
            if (someClient == null)
            {
                throw new ArgumentNullException(nameof(someClient));
            }
            if (someLogger == null)
            {
                throw new ArgumentNullException(nameof(someLogger));
            }

            this.SomeRepository = someRepository;
            this.SomeClient     = someClient;
            this.SomeLogger     = someLogger;
        }
        public ImprovedSomeService(ISomeRepository someRepository,
            ISomeClient someClient,
            ISomeLogger someLogger)
        {
            if (someRepository == null)
            {
                throw new ArgumentNullException(nameof(someRepository));
            }
            if (someClient == null)
            {
                throw new ArgumentNullException(nameof(someClient));
            }
            if (someLogger == null)
            {
                throw new ArgumentNullException(nameof(someLogger));
            }

            this.SomeRepository = someRepository;
            this.SomeClient = someClient;
            this.SomeLogger = someLogger;
        }
Пример #7
0
 public SomeService(ISomeClient client)
 {
     this.client = client;
 }
Пример #8
0
 public HttpClientController(HttpClient httpClient
                             , ISomeClient someClient)
 {
     _httpClient = httpClient;
     _someClient = someClient;
 }