对于依赖注入解析器技术主题,我是一种新知识。我可以知道如何通过配置文件部分来解决依赖性。下面是我的类和接口以及配置文件。
我确定缺少某些代码/设置。您能帮我吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnityConfiguration_Testing
{
public interface IUserAuthentication
{
string Authenticate(string username, string password);
string GetUserRole(string username);
}
}
public class CloudAppAuthetication: IUserAuthentication
{
public string Authenticate(string username, string password)
{
return "This Authenticate method executed from cloud class";//Jwt token based authentication logic should be there
}
public string GetUserRole(string username)
{
return "This GetUserRole method executed from cloud class";//New logic to user management api call
}
}
public class StandaloneAppAuthetication : IUserAuthentication
{
public string Authenticate(string username, string password)
{
return "This Authenticate method executed from standalone class";//current logic should be here
}
public string GetUserRole(string username)
{
return "This GetUserRole method executed from standalone class";//current logic should be here
}
}
控制台方法的控制台应用程序调用:
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnityConfiguration_Testing
{
class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.LoadConfiguration("TestContainer");
IUserAuthentication _userAuthentication = null;
string validatedUser = _userAuthentication.Authenticate("testuser@user.com", "testpassword");
string validatedUserRole = _userAuthentication.GetUserRole("testuser@user.com");
}
}
}
我的控制台应用程序的App.config文件是:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container name="TestContainer">
<register type="UnityConfiguration_Testing.IUserAuthentication,UnityConfiguration_Testing" mapTo="UnityConfiguration_Testing.StandaloneAppAuthetication,UnityConfiguration_Testing" />
</container>
</unity>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
答案 0 :(得分:0)
如果我正确理解了这个问题,那大概就是您要寻找的东西。
using System;
using System.Reflection;
public class Program
{
public static void Main()
{
Type t = Type.GetType("Second"); //get class name from config here
MethodInfo method
= t.GetMethod("Hello", BindingFlags.Static | BindingFlags.Public); // get method by name here
Console.WriteLine(method.Invoke(null,null));
}
}
public static class First
{
public static string Hello(){
return "hello";
}
}
public static class Second
{
public static string Hello(){
return "Hello World";
}
}
这将允许您获取由可从配置文件中获取的名称定义的类和方法。
答案 1 :(得分:-1)
控制反转竞争者是您想要的。 有许多。例如Autofac。他们可以根据您在配置文件中的配置注册接口的实现,也可以在运行时进行配置。