Castle Windsor 项目中快速使用
新建项目如下:
一个模型类,一个接口,一个实现方法。我的目的很明确就是在UI层通过Castle 调用数据访问层的方法。
添加项目引用
CastleDemo.DataAccess 引用 CastleDemo.Domain
CastleDemo.WebUI 引用 CastleDemo.Domain(不需要引用CastleDemo.DataAccess)
安装组件
CastleDemo.DataAccess和 CastleDemo.Domain 都需安装 Castle.Core , Castle.Windsor
CastleDemo.DataAccess 安装 EntityFramework
CastleDemo.Domain
IRepository:
public interface IRepository<T> where T : class
{
/// <summary>
/// 查询
/// </summary>
/// <param name="condition">查询条件</param>
/// <param name="order">排序条件</param>
/// <returns></returns>
IEnumerable<T> Find(Expression<Func<T, bool>> condition, Expression<Func<T, object>> order = null);
}
Product:
public class Product
{
public string productId { get; set; } public string productName { get; set; }
}
IoCContainer:
internal class IoCContainer
{
private static readonly object syncRoot = new object(); private IoCContainer() { } private static IWindsorContainer _Instance; public static IWindsorContainer Instance
{
get
{
lock (syncRoot)
{
if (_Instance == null)
{
_Instance = new WindsorContainer().Install(Configuration.FromAppConfig());
}
return _Instance;
}
}
}
}
Factories:
public class Factories
{
public static T Repository<T>() where T : class
{
return IoCContainer.Instance.Resolve<T>();
}
}
CastleDemo.DataAccess
BaseRepository:
public class BaseRepository<T> : IRepository<T> where T : class
{
private castledemoContext context = Factories.Repository<castledemoContext>(); public IEnumerable<T> Find(Expression<Func<T, bool>> condition, Expression<Func<T,object>> order = null)
{
if (order == null)
{
return context.Set<T>().Where(condition);
} return context.Set<T>().Where(condition).OrderByDescending(order);
}
}
IocInstaller:
public class IoCInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<castledemoContext>().LifeStyle.PerWebRequest); container.Register(Classes.FromThisAssembly()
.InNamespace("CastleDemo.DataAccess.RepositoryImpl", true)
.LifestylePerWebRequest()
.WithService.AllInterfaces());
}
}
CastleDemo.WebUI
HomeController >> Index:
public ActionResult Index()
{
Expression<Func<Product, bool>> condition = m => m.productName.Contains("产品");
Expression<Func<Product, object>> order = m => m.productId; var products = Factories.Repository<IRepository<Product>>().Find( order: order,condition: condition); return View(products);
}
Web.config里还需在相应的地方添加一些配置:
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,Castle.Windsor" />
</configSections> --分割--
<httpModules>
<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
</httpModules> --分割-- <castle>
<installers>
<install type="CastleDemo.DataAccess.IoCInstaller,CastleDemo.DataAccess" />
</installers>
</castle>
在运行代码前,我们还差最后一步,在前面的添加引用的时候WebUI层没有添加对CastleDemo.DataAccess层的引用,那么这个时 候我们需要把CastleDemo.DataAccess层的EntityFramework.dll 和 CastleDemo.DataAccess.dll拷贝到WebUI的BIN里面,但又有个问题来了,那我们每次对 CastleDemo.DataAccess层代码的改动都要重新拷贝,多麻烦啊,解决方法请参考我的另一篇文章VS XCOPY
最后运行看下效果:
下载:CastleDemo
参考Windsor Tutorial:
http://docs.castleproject.org/Windsor.Windsor-tutorial-ASP-NET-MVC-3-application-To-be-Seen.ashx