2013年8月21日星期三

Castle Windsor project quickly using

 

new projects are as follows:

 

 

 

a model class, an interface, an implementation method. My purpose is very clear that in UI layer through Castle call the data access layer.

 

add a project reference

 

CastleDemo.DataAccess quote CastleDemo.Domain

 

CastleDemo.WebUI quote CastleDemo.Domain (without reference to CastleDemo.DataAccess )

 

installed components

 

CastleDemo.DataAccess and CastleDemo.Domain are required to install Castle.Core , Castle.Windsor

 

CastleDemo.DataAccess Installation EntityFramework

 

 

CastleDemo.Domain

 

IRepository:

 
  
 1 public interface IRepository<T>  where T : class 
2 {
3 /// <summary>
4 /// 查询
5 /// </summary>
6 /// <param name="condition">查询条件</param>
7 /// <param name="order">排序条件</param>
8 /// <returns></returns>
9 IEnumerable<T> Find(Expression<Func<T, bool>> condition, Expression<Func<T, object>> order = null);
10 }
 
 

Product:

 
  
1  public class Product 
2 {
3 public string productId { get; set; }
4
5 public string productName { get; set; }
6 }
 
 

IoCContainer:

 
  
 1 internal class IoCContainer 
2 {
3 private static readonly object syncRoot = new object();
4
5 private IoCContainer() { }
6
7 private static IWindsorContainer _Instance;
8
9 public static IWindsorContainer Instance
10 {
11 get
12 {
13 lock (syncRoot)
14 {
15 if (_Instance == null)
16 {
17 _Instance = new WindsorContainer().Install(Configuration.FromAppConfig());
18 }
19 return _Instance;
20 }
21 }
22 }
23 }
 
 

Factories:

 
  
1 public class Factories 
2 {
3 public static T Repository<T>() where T : class
4 {
5 return IoCContainer.Instance.Resolve<T>();
6 }
7 }
 
 

CastleDemo.DataAccess

 

BaseRepository:

 
  
 1     public class BaseRepository<T> : IRepository<T> where T : class 
2 {
3 private castledemoContext context = Factories.Repository<castledemoContext>();
4
5 public IEnumerable<T> Find(Expression<Func<T, bool>> condition, Expression<Func<T,object>> order = null)
6 {
7 if (order == null)
8 {
9 return context.Set<T>().Where(condition);
10 }
11
12 return context.Set<T>().Where(condition).OrderByDescending(order);
13 }
14 }
 
 

IocInstaller:

 
  
 1     public class IoCInstaller : IWindsorInstaller 
2 {
3 public void Install(IWindsorContainer container, IConfigurationStore store)
4 {
5 container.Register(Component.For<castledemoContext>().LifeStyle.PerWebRequest);
6
7 container.Register(Classes.FromThisAssembly()
8 .InNamespace("CastleDemo.DataAccess.RepositoryImpl", true)
9 .LifestylePerWebRequest()
10 .WithService.AllInterfaces());
11 }
12 }
 
 

CastleDemo.WebUI

 

HomeController >> Index:

 
  
1  public ActionResult Index() 
2 {
3 Expression<Func<Product, bool>> condition = m => m.productName.Contains("产品");
4 Expression<Func<Product, object>> order = m => m.productId;
5
6 var products = Factories.Repository<IRepository<Product>>().Find( order: order,condition: condition);
7
8 return View(products);
9 }
 
 

Web.config in need in the appropriate place to add some configuration:

 
  
  <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>
 
 

run the code, we short of the final step in front of the add references when WebUI layer does not add CastleDemo.DataAccess layer references, so this time we need to CastleDemo.DataAccess layer EntityFramework.dll and CastleDemo.DataAccess.dll copied to the BIN WebUI inside, but there is a problem here, that we have every right CastleDemo.DataAccess layer code changes must be re-copied, and more trouble ah, workarounds, please refer to my other one Articles VS XCOPY

 

last run facie effect:

 

 

Download: CastleDemo

 

Reference Windsor Tutorial:

 

http://docs.castleproject .org/Windsor.Windsor-tutorial-ASP-NET-MVC-3-application-To-be-Seen.ashx

 

没有评论:

发表评论