接口:
namespace ServiceInterfaces
{
public interface IPerson:IServiceInterface
{
Person[] GetAllPerson();
Person GetPersonByID(string PersonID);
}
}
接口实现:
namespace ServiceImpls
{
public class PersonService:ServiceInterfaces .IPerson
{
public Person[] GetAllPerson()
{
return Gateway.Default.FindArray<Person>();
}
public Person GetPersonByID(string PersonID)
{
return Gateway.Default.Find<Person>(PersonID);
}
}
}
服务工厂:
public abstract class SeviceFactory
{
private static NBear.IoC.Service.ServiceFactory factory = NBear.IoC.Service.ServiceFactory.Create();
public static IServiceInterFaceType GetService<IServiceInterFaceType>() where IServiceInterFaceType:IServiceInterface
{
return factory.GetService<IServiceInterFaceType>();
}
}
页面调用:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ServiceInterfaces.IPerson ps = SeviceFactory.GetService<ServiceInterfaces.IPerson>();
this.Chjw.DataSource = ps.GetAllPerson();
this.Chjw.DataBind();
}
}
Web.config:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="entityConfig" type="NBear.Common.EntityConfigurationSection, NBear.Common"/>
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
</configSections>
<entityConfig>
<includes>
<add key="Sample Entity Config" value="~/EntityConfig.xml"/>
</includes>
</entityConfig>
<castle>
<components>
<component id="category service" service="ServiceInterfaces.IPerson, ServiceInterfaces" type="ServiceImpls.PersonService, ServiceImpls"/>
</components>
</castle>
<appSettings/>
<connectionStrings>
<add name="DbConn2005" connectionString="Data Source=.;Initial Catalog=Student;Integrated Security=True" providerName="NBear.Data.SqlServer9.SqlDbProvider9"/>
</connectionStrings>
<system.web>
<compilation debug="true">
<assemblies>
<add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
<authentication mode="Windows"/>
</system.web>
</configuration>
问题是:
“SeviceFactory”的类型初始值设定项引发异常。