android studio 添加compline,Castle Windsor 项目中快速使用

新建项目如下:

655048b660308c8c8249b5b31e6d2d29.png

一个模型类,一个接口,一个实现方法。我的目的很明确就是在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:

67d0865f17d9be527254bf8a8ff6c5b4.gif

public interface IRepository where T : class

{

///

/// 查询

///

/// 查询条件

/// 排序条件

///

IEnumerable Find(Expression> condition, Expression> order = null);

}

67d0865f17d9be527254bf8a8ff6c5b4.gif

Product:

67d0865f17d9be527254bf8a8ff6c5b4.gif

public class Product

{

public string productId { get; set; }

public string productName { get; set; }

}

67d0865f17d9be527254bf8a8ff6c5b4.gif

IoCContainer:

67d0865f17d9be527254bf8a8ff6c5b4.gif

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;

}

}

}

}

67d0865f17d9be527254bf8a8ff6c5b4.gif

Factories:

67d0865f17d9be527254bf8a8ff6c5b4.gif

public class Factories

{

public static T Repository() where T : class

{

return IoCContainer.Instance.Resolve();

}

}

67d0865f17d9be527254bf8a8ff6c5b4.gif

CastleDemo.DataAccess

BaseRepository:

67d0865f17d9be527254bf8a8ff6c5b4.gif

public class BaseRepository : IRepository where T : class

{

private castledemoContext context = Factories.Repository();

public IEnumerable Find(Expression> condition, Expression> order = null)

{

if (order == null)

{

return context.Set().Where(condition);

}

return context.Set().Where(condition).OrderByDescending(order);

}

}

67d0865f17d9be527254bf8a8ff6c5b4.gif

IocInstaller:

67d0865f17d9be527254bf8a8ff6c5b4.gif

public class IoCInstaller : IWindsorInstaller

{

public void Install(IWindsorContainer container, IConfigurationStore store)

{

container.Register(Component.For().LifeStyle.PerWebRequest);

container.Register(Classes.FromThisAssembly()

.InNamespace("CastleDemo.DataAccess.RepositoryImpl", true)

.LifestylePerWebRequest()

.WithService.AllInterfaces());

}

}

67d0865f17d9be527254bf8a8ff6c5b4.gif

CastleDemo.WebUI

HomeController >> Index:

67d0865f17d9be527254bf8a8ff6c5b4.gif

public ActionResult Index()

{

Expression> condition = m => m.productName.Contains("产品");

Expression> order = m => m.productId;

var products = Factories.Repository>().Find( order: order,condition: condition);

return View(products);

}

67d0865f17d9be527254bf8a8ff6c5b4.gif

Web.config里还需在相应的地方添加一些配置:

67d0865f17d9be527254bf8a8ff6c5b4.gif

--分割--

--分割--

67d0865f17d9be527254bf8a8ff6c5b4.gif

在运行代码前,我们还差最后一步,在前面的添加引用的时候WebUI层没有添加对CastleDemo.DataAccess层的引用,那么这个时 候我们需要把CastleDemo.DataAccess层的EntityFramework.dll 和 CastleDemo.DataAccess.dll拷贝到WebUI的BIN里面,但又有个问题来了,那我们每次对 CastleDemo.DataAccess层代码的改动都要重新拷贝,多麻烦啊,解决方法请参考我的另一篇文章VS XCOPY

最后运行看下效果:

30c09065edcf7ea9e8f13b8441ddbe92.png

参考Windsor Tutorial:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值