AspNetCore运行原理
#region
{
EndpointMiddlewareFacde endpointMiddlewareFacde
= new EndpointMiddlewareFacde();
endpointMiddlewareFacde
.Invoke(new HttpContext());
}
#endregion
Endpoint相关类
class Endpoint
{
public MethodInfo methodInfo
{ set; get; }
public IList
<ParameterInfo
> parameterInfos
{ set; get; }
public object returnValue
{ set; get; }
public object Controller
{ set; get; }
}
class EndpointDataSources
{
public IDictionary
<string, Endpoint
> endpoints
{ set; get; } = new Dictionary<string, Endpoint>();
}
class EndpointHandler
{
public object HandlerEndpoint(Endpoint endpoint
,object[] providedArgs
)
{
MethodInfo methodInfo
= endpoint
.methodInfo
;
return methodInfo
.Invoke(endpoint
.Controller
, providedArgs
);
}
}
class EndpointRoute
{
public EndpointRouteBuilder endpointRouteBuilder
{ set; get; }
public Endpoint Match(string requestPath
) {
return endpointRouteBuilder
.Build().endpoints
[requestPath
];
}
}
class EndpointRouteBuilder
{
private EndpointDataSources endpointDataSources
= new EndpointDataSources();
public void MapControllers()
{
Endpoint endpoint
= new Endpoint();
endpoint
.methodInfo
= typeof(EndpointRouteBuilder
).GetMethods()[0];
endpointDataSources
.endpoints
.Add("/index.html", endpoint
);
}
public void MapRazorPages()
{
}
public EndpointDataSources Build()
{
return endpointDataSources
;
}
}
class HttpContext
{
}
适配器日志相关类
log4
class Log4jLog : ILog
{
private log4net
.ILog log
;
public Log4jLog(log4net
.ILog log
)
{
this.log
= log
;
}
public void Debug(string msg
)
{
log
.Debug(msg
);
}
public void Error(string msg
)
{
log
.Error(msg
);
}
public void Info(string msg
)
{
log
.Info(msg
);
}
public void Warn(string msg
)
{
log
.Warn(msg
);
}
}
class Log4NetLogAdpater : ILogAdpater
{
public ILog GetLog(Type type
)
{
NLog.Logger logger
= NLog
.LogManager
.GetLogger("product", type
);
return new NLogLog(logger
);
}
}
NLog
class NLogILogAdpater : ILogAdpater
{
public ILog GetLog(Type type
)
{
NLog.Logger logger
= NLog
.LogManager
.GetLogger("product", type
);
return new NLogLog(logger
);
}
}
class NLogLog : ILog
{
private NLog.Logger logger
;
public NLogLog(Logger logger
)
{
this.logger
= logger
;
}
public void Debug(string msg
)
{
logger
.Debug(msg
);
}
public void Error(string msg
)
{
logger
.Error(msg
);
}
public void Info(string msg
)
{
logger
.Info(msg
);
}
public void Warn(string msg
)
{
logger
.Warn(msg
);
}
}
日志工厂
interface ILog
{
public void Debug(string msg
);
public void Info(string msg
);
public void Warn(string msg
);
public void Error(string msg
);
}
interface ILogAdpater
{
public ILog GetLog(Type type
);
}
class LogFactory
{
public ILog GetLog(string loggerFlag
,Type type
)
{
if ("log4net".Equals(loggerFlag
))
{
return new Log4NetLogAdpater().GetLog(type
);
} else if ("nlog".Equals(loggerFlag
))
{
return new NLogILogAdpater().GetLog(type
);
}
return null;
}
}
节点中间件
class EndpointMiddlewareFacde
{
private EndpointRouteBuilder endpointRouteBuilder
;
private EndpointRoute endpointRoute
;
private EndpointHandler endpointHandler
;
public EndpointMiddlewareFacde()
{
this.endpointRouteBuilder
= new EndpointRouteBuilder();
this.endpointRoute
= new EndpointRoute();
this.endpointHandler
= new EndpointHandler();
}
public void Invoke(HttpContext httpContext
)
{
endpointRouteBuilder
.MapControllers();
Endpoint endpoint
= endpointRoute
.Match("/index");
object result
= endpointHandler
.HandlerEndpoint(endpoint
, new object[] { "原理解析" });
ViewResult viewResult
= (ViewResult
)result
;
if (viewResult
.ViewType
.Equals("Razor"))
{
RazorView razorView
= new RazorView();
razorView
.Render(viewResult
.ViewData
, httpContext
);
} else if (viewResult
.ViewType
.Equals("XML"))
{
XMLView xMLView
= new XMLView();
xMLView
.Render(viewResult
.ViewData
, httpContext
);
}
}
}
view相关
abstract class AbstractView
{
public void Render(IDictionary
<string, object> viewData
, HttpContext httpContext
)
{
TransformData(viewData
);
BuildPage(viewData
);
WritePage(httpContext
);
}
protected abstract void TransformData(IDictionary
<string, object> viewData
);
protected abstract void BuildPage(IDictionary
<string, object> viewData
);
protected abstract void WritePage(HttpContext httpContext
);
}
interface IView
{
public void Render(IDictionary
<string,object> viewData
,HttpContext httpContext
);
}
class RazorView : AbstractView
{
protected override void TransformData(IDictionary
<string, object> viewData
)
{
Console
.WriteLine($
"将数据转换成Razor数据");
}
protected override void BuildPage(IDictionary
<string, object> viewData
)
{
Console
.WriteLine($
"构造Razor页面");
}
protected override void WritePage(HttpContext httpContext
)
{
Console
.WriteLine($
"输出Razor页面");
}
}
class ViewResult
{
public IDictionary
<string, object> ViewData
{ set; get; }
public string ViewName
{set;get;}
public string ViewType
{ set; get; }
}
class XMLView:AbstractView
{
protected override void TransformData(IDictionary
<string, object> viewData
)
{
Console
.WriteLine($
"将数据转换成Razor数据");
}
protected override void BuildPage(IDictionary
<string, object> viewData
)
{
Console
.WriteLine($
"构造Razor页面");
}
protected override void WritePage(HttpContext httpContext
)
{
Console
.WriteLine($
"输出Razor页面");
}
}
转载请注明原文地址: https://lol.8miu.com/read-20028.html