逻辑层框架:IOC/AOP/Message Bus/CQRS

  • IOC模式
        //注册IList<string>接口
        IOCContainer.RegisterType<IList<string>, List<string>>();
        //获得IList<string>接口的实例
        var list = IOCContainer.Resolve<IList<string>>();
  • AOP模式
public class PojoRepository : AspectMapper<PojoEntity>
{
    public PojoRepository()
        : base("ID")
    {
        //在实体保存之前触发
        this.Before(MapperEvent.EntityCreate, Validator.Validate1);
        //在实体保存完毕之后触发
        this.AfterFinally(MapperEvent.EntityCreate, Validator.Validate2);
    }
}

public class Validator
{
        public static void Validate1(BeforeSubmitEvent command)
        {
            Debug.WriteLine(string.Format("PojoEntity {0} Creating Canceled.", command.PrimaryFields[0].Value));
            //取消当前命令
            command.Cancel = true;
        }
        public static void Validate2(AfterFinallyEvent command)
        {
            Debug.WriteLine(string.Format("PojoEntity {0} Created.", command.PrimaryFields[0].Value));
        }
}
  • Message Bus模式
    //将自定义命令发送到Message Bus
    MessageBus.CommandService.Send(new CustomCommand("PojoEntity_Delete", 2));

    //指定任意方法为"PojoEntity_Delete"命令的处理程序
    [OnMessage("PojoEntity_Delete", MessageBus.CommandServiceUrl)]
    public void PojoEntity_Delete(CustomCommand cmd)
    {
        Debug.WriteLine(string.Format("PojoEntity {0} Deleted.", cmd.ID));
    }

    //定义一个同步命令
    public class CustomCommand : SyncMessage
    {
        public CustomCommand(string commandName, int? ID)
            : base(commandName)
        {
            this.ID = ID;
        }
        public int? ID { get; set; }
    }
  • CQRS模式
    private void TestEventBus()
    {
        //业务模块在默认消息端点上侦听Order_Delete消息,一条消息可以添加多个侦听器
        MessageBus.CommandService.On("Order_Delete", new MessageHandler<DebugCommand>(
            //一个侦听器可以添加多个回调委托
            new MessageHandlerItem<DebugCommand>(d => Debug.WriteLine("订单 " + d.OrderNo + " 是有效订单"),
                //一个回调委托可以依赖于另一个回调委托,只有依赖项全部执行完毕后才会执行回调主体
                new MessageHandlerItem<DebugCommand>(d => Debug.WriteLine("检查订单信息"), EMessageLevel.Low),
                //还可以为回调委托设置优先级和执行超时
                new MessageHandlerItem<DebugCommand>(d => Debug.WriteLine("检查用户信息"), EMessageLevel.High)
            ),
            //一个侦听器可以添加多个回调委托
            new MessageHandlerItem<DebugCommand>(d => Debug.WriteLine("正在删除订单 " + d.OrderNo))
        ));
        //日志模块在默认消息端点上侦听Order_Delete消息,消息接收者可以是delegate或method
        MessageBus.RegisterHandler();
        //向默认消息端点上发送Order_Delete消息
        MessageBus.CommandService.Send(new DebugCommand("Order_Delete", "201301010009"));
    }
    [OnMessage("Order_Delete", MessageBus.CommandServiceUrl, EMessageType.Async)]
    private void WriteLog(DebugCommand command)
    {
        Debug.WriteLine("订单已删除 " + command.OrderNo);
    }

    public class DebugCommand : SyncMessage
    {
        public DebugCommand(string commandName, string orderNo)
            : base(commandName)
        {
            this.OrderNo = orderNo;
        }
        
        /// <summary>
        /// 订单编号
        /// </summary>
        public string OrderNo
        {
            get;
            set;
        }
    }

下一页:建立实体关联

Last edited Feb 26, 2013 at 9:21 AM by gameboyLV, version 8

Comments

No comments yet.