您的位置:首页 > 编程语言 > ASP

ASP.NET MVC 开源项目Kigg解读(2)——Kigg.Core第一部分

2010-09-13 17:59 961 查看
Kigg是一个很好的ASP.NETMVC范例项目,本着研究的目的,对Kigg进行解读。

上一篇中,我们介绍了Kigg的启动、后台任务和事件聚合器。这一篇,我们来看看Kigg的核心类库Kigg.Core

一、类库一览





Kigg.Core是Kigg的灵魂所在,主要包含:



ConfigurationSettings:站点设置

DomainObjects:领域模型

Extension:对.NET对象的扩展

Helper:一些帮助方法

Infrastructure:基础架构

Repository:数据访问

Service:数据服务

二、杂项

在想到底用什么来描述ConfigurationSettings,Extension,Helper,第一印象是杂项,那么就叫杂项吧!ORZ~

1、配置信息

ConfigurationSettings其实是Kigg的站点配置信息:




Kigg是通过在Unity的配置文件中来设置这些配置信息的

<typetype="IConfigurationSettings"mapTo="ConfigurationSettings">

[code]<lifetimetype="Singleton"/>
<typeConfigextensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,Microsoft.Practices.Unity.Configuration">

<propertyname="RootUrl"propertyType="System.String">

<valuetype="System.String"value="http://localhost:4252"/>

</property>

<propertyname="WebmasterEmail"propertyType="System.String">

<valuetype="System.String"value=""/>

</property>

//省略……

</typeConfig>

</type>

[/code]



2、Extension

再来看Extension:





Extension包含了对集合、枚举对象、Guid、DateTime以及String的扩展,有兴趣的同学可以去看看。

第一篇中的ForEach就来自于EnumerableExtension

publicstaticclassEnumerableExtension

[code]{
[DebuggerStepThrough]

publicstaticvoidForEach<T>(thisIEnumerable<T>enumerable,Action<T>action)

{

foreach(Titeminenumerable)
{

action(item);

}

}

}

[/code]

3、Helper

最后看看Helper





这里包含了一些帮助类库:

CheckArgument是参数检验,比如:


[code][DebuggerStepThrough]
publicstaticvoidIsNotEmpty(Guidargument,stringargumentName)
{
if(argument==Guid.Empty)
{
thrownewArgumentException("\"{0}\"cannotbeemptyguid.".FormatWith(argumentName),argumentName);

}

}

[/code]

CheckArgument广泛的运用在Kigg.Core的方法中,比如第一部分中的

publicStartBackgroundTasks(IBackgroundTask[]tasks)

[code]{
Check.Argument.IsNotEmpty(tasks,"tasks");
_tasks=tasks;10:

}

[/code]

PagedResult是个有意思的玩意,如果你使用过Subsonic,你会发现这个和PagedList有点像。PagedResult在Kigg中作为获取列表方法的返回类型,包含了结果集和总记录数,因此分页就比较方便了。

publicclassPagedResult<T>

[code]{
privatereadonlyReadOnlyCollection<T>_result;
privatereadonlyint_total;
publicPagedResult(IEnumerable<T>result,inttotal)

{
Check.Argument.IsNotNull(result,"result");
Check.Argument.IsNotNegative(total,"total");
_result=newReadOnlyCollection<T>(newList<T>(result));
_total=total;

}
publicPagedResult():this(newList<T>(),0)

{

}
publicICollection<T>Result

{
[DebuggerStepThrough]
get
{
return_result;

}

}
publicintTotal

{
[DebuggerStepThrough]
get
{
return_total;

}

}
publicboolIsEmpty

{
[DebuggerStepThrough]
get
{
return_result.Count==0;

}

}

}

[/code]

三、基础架构





话说,这一块是Kigg比较核心的部分。

这里包含了前面提到的Boot、BackgroundTask和EventAggrator,在这里就不再多说了。

1、IOC

IOC是Kigg的基石,大部分的类的实例化和状态维持都由它来实现。Kigg使用Unity来作为IOC工具,想要简单的话,直接使用Unity就可以了,但是为了解除与Unity的依赖,Kigg特地抽象出一个接口IDependencyResolver:

publicinterfaceIDependencyResolver:IDisposable

[code]{
voidRegister<T>(Tinstance);
voidInject<T>(Texisting);
TResolve<T>(Typetype);
TResolve<T>(Typetype,stringname);
TResolve<T>();
TResolve<T>(stringname);
IEnumerable<T>ResolveAll<T>();

}

[/code]

这样,不管使用什么IOC工具,只需要实现这个接口就可以了。

在没用IOC的时候,要创建一个借口的实例怎么做?用工厂模式呗!这样在AppSetting中配置下dependencyResolverTypeName就可以了

publicinterfaceIDependencyResolverFactory

[code]{
IDependencyResolverCreateInstance();

}

publicclassDependencyResolverFactory:IDependencyResolverFactory
{
privatereadonlyType_resolverType;
publicDependencyResolverFactory(stringresolverTypeName)

{
Check.Argument.IsNotEmpty(resolverTypeName,"resolverTypeName");
_resolverType=Type.GetType(resolverTypeName,true,true);

}
publicDependencyResolverFactory():this(newConfigurationManagerWrapper().AppSettings["dependencyResolverTypeName"])

{

}
publicIDependencyResolverCreateInstance()

{
returnActivator.CreateInstance(_resolverType)asIDependencyResolver;

}

}

[/code]

为了方便使用IOC,Kigg特定创建了一个静态类库IOC:


publicstaticclassIoC

[code]{
privatestaticIDependencyResolver_resolver;

[DebuggerStepThrough]
publicstaticvoidInitializeWith(IDependencyResolverFactoryfactory)

{
Check.Argument.IsNotNull(factory,"factory");
_resolver=factory.CreateInstance();

}

[DebuggerStepThrough]
publicstaticvoidRegister<T>(Tinstance)

{
Check.Argument.IsNotNull(instance,"instance");
_resolver.Register(instance);

}

[DebuggerStepThrough]
publicstaticvoidInject<T>(Texisting)

{
Check.Argument.IsNotNull(existing,"existing");
_resolver.Inject(existing);

}

[DebuggerStepThrough]
publicstaticTResolve<T>(Typetype)

{
Check.Argument.IsNotNull(type,"type");
return_resolver.Resolve<T>(type);

}

[DebuggerStepThrough]
publicstaticTResolve<T>(Typetype,stringname)

{
Check.Argument.IsNotNull(type,"type");
Check.Argument.IsNotEmpty(name,"name");
return_resolver.Resolve<T>(type,name);

}

[DebuggerStepThrough]
publicstaticTResolve<T>()

{
return_resolver.Resolve<T>();

}

[DebuggerStepThrough]
publicstaticTResolve<T>(stringname)

{
Check.Argument.IsNotEmpty(name,"name");
return_resolver.Resolve<T>(name);

}

[DebuggerStepThrough]
publicstaticIEnumerable<T>ResolveAll<T>()

{
return_resolver.ResolveAll<T>();

}

[DebuggerStepThrough]
publicstaticvoidReset()

{
if(_resolver!=null)
{
_resolver.Dispose();

}

}

}

[/code]

在使用的使用,需要先调用IoC.InitializeWith(),初始化IoC内部的静态字段_resolver,然后就可以直接方便的使用IOC了,回顾第一篇的代码:
1:publicstaticclassBootstrapper
2:{
3:staticBootstrapper()
4:{
5:try
6:{
7:IoC.InitializeWith(newDependencyResolverFactory());
8:}
9:catch(ArgumentException)
10:{
11://ConfigfileisMissing
12:}
13:}
14:
15:publicstaticvoidRun()
16:{
17:IoC.ResolveAll<IBootstrapperTask>().ForEach(t=>t.Execute());
18:}
19:}


2、缓存

缓存组件很多,Kigg选用的是EnterpriseLibrary。同样的为了消除与缓存组件的依赖,Kigg定义了自己的缓存接口:


publicinterfaceICache

[code]{
intCount

{
get;

}
voidClear();
boolContains(stringkey);
TGet<T>(stringkey);
boolTryGet<T>(stringkey,outTvalue);
voidSet<T>(stringkey,Tvalue);
voidSet<T>(stringkey,Tvalue,DateTimeabsoluteExpiration);
voidSet<T>(stringkey,Tvalue,TimeSpanslidingExpiration);
voidRemove(stringkey);

}

[/code]

同样的,为了方便调用,Kigg创建了Cache静态类,这次,采用的是一个静态属性,注意下InternalCache是通过IOC初始化的。这样,只需要在IOC的配置文件中指定下哪个具体的类实现了ICache接口就可以了。


publicstaticclassCache

[code]{
publicstaticintCount

{
[DebuggerStepThrough]
get
{
returnInternalCache.Count;

}

}
privatestaticICacheInternalCache

{
[DebuggerStepThrough]
get
{
returnIoC.Resolve<ICache>();

}

}

[DebuggerStepThrough]
publicstaticvoidClear()

{
InternalCache.Clear();

}

[DebuggerStepThrough]
publicstaticboolContains(stringkey)

{
Check.Argument.IsNotEmpty(key,"key");
returnInternalCache.Contains(key);

}

[DebuggerStepThrough]
publicstaticTGet<T>(stringkey)

{
Check.Argument.IsNotEmpty(key,"key");
returnInternalCache.Get<T>(key);

}

[DebuggerStepThrough]
publicstaticboolTryGet<T>(stringkey,outTvalue)

{
Check.Argument.IsNotEmpty(key,"key");
returnInternalCache.TryGet(key,outvalue);

}

[DebuggerStepThrough]
publicstaticvoidSet<T>(stringkey,Tvalue)

{
Check.Argument.IsNotEmpty(key,"key");
InternalCache.Set(key,value);

}

//以下省略


}

[/code]

3、日志

日志和缓存同样一个道理,有一个Log接口


publicinterfaceILog

[code]{
voidInfo(stringmessage);
voidWarning(stringmessage);
voidError(stringmessage);
voidException(Exceptionexception);

}

[/code]

然后一个静态类Log,这里没有用一个静态的字段或属性来缓存ILog接口,而是一个GetLog()方法,效率上不如前面的Cache,算是一个小瑕疵吧。


publicstaticclassLog

[code]{
[MethodImpl(MethodImplOptions.NoInlining)]

[DebuggerStepThrough]
publicstaticvoidInfo(stringmessage)

{
Check.Argument.IsNotEmpty(message,"message");
GetLog().Info(message);

}
//省略部分代码
[MethodImpl(MethodImplOptions.NoInlining)]
privatestaticILogGetLog()

{
returnIoC.Resolve<ILog>();

}
[MethodImpl(MethodImplOptions.NoInlining)]
privatestaticstringFormat(stringformat,paramsobject[]args)

{
Check.Argument.IsNotEmpty(format,"format");
returnformat.FormatWith(args);

}

}

[/code]

未完、待续……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐
章节导航