技术开发 频道

NBearV3 Step by Step教程——IoC篇

    Step 7 配置Service,使用ServiceFactory,调用Service

    7.1 编译ServiceImpls。我们就可以准备在website中使用Service了。为website添加到Entities、ServiceInterfacs、dist\NBear.Common.dll、dist\NBear.IoC.dll的引用。

    注意,这里无需为website添加到ServiceImpls的引用。想想为什么?如果website依赖ServiceImpls的实现代码,那么就不叫IoC了。IoC的核心是基于容器的依赖注入。换句话说,我们只需要在配置文件中指定一个Service的接口和对应的实现类的位置,从而使得Service的调用者与Service的实现者松散耦合。但是,为了能让我们的website能根据配置文件找到Service的实现类,我们还是需要复制ServiceImpls编译输出的ServiceImpls.dll到website的Bin目录中。

    7.2 接着,我们需要在Web.config中配置IoC容器。NBearV3的IoC组件使用castle作为IoC容器,因此,可以使用标准的castle配置与法进行配置。不过一般,只需要使用下面这样最简单的语法就行了:

<?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> <!--You can use standard castle component decleration schema to define service interface impls here--> <component id="category service" service="ServiceInterfaces.ICategoryService, ServiceInterfaces" type="ServiceImpls.CategoryService, ServiceImpls"/> <component id="product service" service="ServiceInterfaces.IProductService, ServiceInterfaces" type="ServiceImpls.ProductService, ServiceImpls"/> </components> </castle> <appSettings/> <connectionStrings> <add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/> </connectionStrings> <system.web> <compilation debug="true"> <assemblies> <add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation> <authentication mode="Windows"/> </system.web> </configuration>

    7.3 接着,在Default.aspx.cs文件中的PageLoad中,添加下面的代码,访问Service。

    注意,所有的Service只需简单地从ServiceFactory.GetService<ServiceType>()得到。然后就能使用了。

protected void Page_Load(object sender, EventArgs e) { ServiceFactory factory = ServiceFactory.Create(); IProductService ps = factory.GetService<IProductService>(); Product[] products = ps.GetAllProducts(); WriteLine(string.Format("Got all products, {0} in total.", products.Length)); ICategoryService cs = factory.GetService<ICategoryService>(); Category[] categories = cs.GetAllCategories(); WriteLine(string.Format("Got all categories, {0} in total.", categories.Length)); WriteLine("In each category:"); foreach (Category item in categories) { WriteLine(string.Format("ID={0}, Name={1}, Products in category: {2}.", item.CategoryID, item.CategoryName, item.Products.Length)); } } private void WriteLine(string str) { Response.Write(Server.HtmlEncode(str) + "<br /><br />"); }



0
相关文章