`
ilikeido
  • 浏览: 27442 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

BeeFramework 系列三 MVC篇上

阅读更多
     这两天死了不少人,南北呼应。厦门的兄弟们别挤brt了,公交有风险,挤车需谨慎!

    继续介绍Bee的mvc,框架中已经为MVC做了一定程度的封装,我们查看Bee的源码会发现对应着BeeController、BeeModel以及各种常用的View和ViewController,另外ViewLayout添加了View界面布局的灵活性,从v0.3.0版本开始更是加入了基于XML的UI模板模块。
   
    还是先上个例子比较直观些。
   
    这个例子,通过访问国家气象局天气预报JSON数据接口,http://www.weather.com.cn/data/sk/101010100.html,返回天气数据。
    看下目录结构
   
    把MVC分离出来放到不同的目录下。
    Model层代码:
   
//
//  WeatherModel.h
//  BeeFrameWorkTest
//
//  Created by he songhang on 13-6-8.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#import <BeeFramework/Bee.h>

@interface Weather : BeeActiveRecord

@property(nonatomic,strong) NSString *city;//城市
@property(nonatomic,strong) NSString *temp;//气温
@property(nonatomic,strong) NSString *WD;//风向
@property(nonatomic,strong) NSString *WS;//风力
@property(nonatomic,strong) NSString *time;//发布时间

@end

@interface WeatherModel : BeeModel

@property(nonatomic,strong) Weather *weather;

-(void)loadOnline;//查询天气

-(void)loadCache;//读取历史记录

@end


//
//  WeatherModel.m
//  BeeFrameWorkTest
//
//  Created by he songhang on 13-6-8.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#import "WeatherModel.h"
#import "WeatherController.h"

@implementation Weather

+(void)mapRelation{
    [super mapPropertyAsKey:@"city"];
    [super mapRelation];
}

@end

@interface WeatherModel (private)

- (void)saveCache;
- (void)asyncSaveCache;

@end

@implementation WeatherModel

-(void)loadOnline{
    [self sendMessage:WeatherController.LOAD_WEATHER];
}

-(void)loadCache{
    int count = Weather.DB.WHERE(@"city",@"北京").COUNT().resultCount;
    if (count>0) {
        self.weather = [Weather.DB.WHERE(@"city",@"北京").GET_RECORDS() lastObject];
    }
}

- (void)saveCache
{
	[NSObject cancelPreviousPerformRequestsWithTarget:self];
	[self performSelector:@selector(asyncSaveCache) withObject:nil afterDelay:0.1f];
}


- (void)asyncSaveCache
{
	_weather.SAVE();
}

- (void)handleMessage:(BeeMessage *)msg
{
	[super handleMessage:msg];
}

- (void)handleWeatherController:(BeeMessage *)msg
{
	if ( [msg is:WeatherController.LOAD_WEATHER] )
	{
		if ( msg.succeed )
		{
			NSDictionary *dict = [msg.output dictAtPath:@"result"];
            if (dict) {
                self.weather = (Weather *)[dict objectForClass:[Weather class]];
                [self saveCache];
            }
            
		}
	}
    [super handleMessage:msg];
}

@end

     在.h中定义了一个Weather的实体,与天气数据对应,用到了BeeActiveRecord,这是Bee中的ORM框架,实现数据库与BeeActiveRecord的序列和反序列化。通常我们在做离线缓存时采用CoreData,bee采用的是DB的方式。感谢老郭为我们封装了原来的SQL语法,免去了封装枯燥的SQL语句。查看BeeActiveRecord的父类BeeDatabase,可以看到一些开源软件上用的很欢的以Block为参数的写法。
     在.m中通过
[self sendMessage:WeatherController.LOAD_WEATHER];
向Weathercontroller发送了一个LOAD_WEATHER的请求,在handleWeatherController中响应BeeMessage的不同状态,并完成离线数据的保存工作
[self saveCache];


      再上Controller的实现
//
//  WeatherController.h
//  BeeFrameWorkTest
//
//  Created by he songhang on 13-6-8.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#import "Bee_Controller.h"

@interface WeatherController : BeeController

AS_MESSAGE(LOAD_WEATHER)

@end


//
//  WeatherController.m
//  BeeFrameWorkTest
//
//  Created by he songhang on 13-6-8.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#import "WeatherController.h"
#import "JSONKit.h"
#import <BeeFramework/Bee.h>

@implementation WeatherController

DEF_MESSAGE(LOAD_WEATHER)

-(void)LOAD_WEATHER:(BeeMessage *)msg{
    if ( msg.sending )
	{
		NSString * callURL = @"http://www.weather.com.cn/data/sk/101010100.html" ;
		msg.HTTP_GET( callURL );
	}
	else if ( msg.progressed )
	{
        
	}
	else if ( msg.succeed )
	{
		NSDictionary * jsonData = [msg.response objectFromJSONData];
		if ( nil == jsonData )
		{
			[msg setLastError];
			return;
		}
        
        NSDictionary *weatherinfo = (NSDictionary *)[jsonData objectAtPath:@"weatherinfo"];
		        
		[msg output:@"result", weatherinfo, nil];
	}
	else if ( msg.failed )
	{
		
	}
	else if ( msg.cancelled )
	{
	}
}

@end

     WeatherController完成与天气预报服务器交互的工作,和UISignle类似,在.h中通过AS_MESSAGE(LOAD_WEATHER)定义了一个LOAD_WEATHER的方法声明,在.m中用DEF_MESSAGE(LOAD_WEATHER)对应,并完成LOAD_WEATHER的方法实现。在bee中网络层使用的是ASIHTTPRequest框架,至于为什么不用AFNetworking,这实际上是个人习惯的问题,虽然ASIHTTPRequest已经N久没更新过了,不过也够用了。BeeMessage中对ASIHTTPRequest做了很多工作,通过BeeRequestQueue,对post和get过程中状态的分离。

    再来看view层,其实在这个例子中做真正做逻辑的是ViewController,注意它的父类为BeeUIBoard,正是这个类做了UIMessage的消息转发,还实现了ViewController生命周期中的UISignal化。
//
//  ViewController.h
//  BeeFrameWorkTest
//
//  Created by he songhang on 13-6-3.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <BeeFramework/Bee.h>
#import "WeatherModel.h"

@interface ViewController : BeeUIBoard{
    __strong WeatherModel *_weatherModel;
    __weak IBOutlet UITextView *textView;
}

@end


//
//  ViewController.m
//  BeeFrameWorkTest
//
//  Created by he songhang on 13-6-3.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#import "ViewController.h"
#import "WeatherController.h"

@implementation ViewController

-(void)load{
    _weatherModel = [[WeatherModel alloc]init];
    [_weatherModel addObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)handleUISignal:(BeeUISignal *)signal
{
	[super handleUISignal:signal];
}

- (void)handleUISignal_BeeUIBoard:(BeeUISignal *)signal
{
	[super handleUISignal:signal];
    
	if ( [signal is:BeeUIBoard.CREATE_VIEWS] )
	{
		[self showNavigationBarAnimated:YES];
        [self showBarButton:UINavigationBar.BARBUTTON_RIGHT system:UIBarButtonSystemItemRefresh];
        self.title = @"天气预报";
	}
	else if ( [signal is:BeeUIBoard.DELETE_VIEWS] )
	{
	}
	else if ( [signal is:BeeUIBoard.LOAD_DATAS] )
	{
		[_weatherModel loadCache];
        [self refushTextView];
	}
	else if ( [signal is:BeeUIBoard.WILL_APPEAR] )
	{
		
	}
	else if ( [signal is:BeeUIBoard.DID_DISAPPEAR] )
	{
	}
}

- (void)handleUISignal_UINavigationBar:(BeeUISignal *)signal
{
	if ( [signal is:UINavigationBar.BACK_BUTTON_TOUCHED] )
	{
	}
	else if ( [signal is:UINavigationBar.DONE_BUTTON_TOUCHED] )
	{
		[self loadOnline];
	}
}

-(void)loadOnline{
    [_weatherModel loadOnline];
}

-(void)refushTextView{
    if (_weatherModel.weather) {
        Weather *weather = _weatherModel.weather;
        NSString *weatherInfo = [NSString stringWithFormat:@"城市:%@\n气温:%@\n风向:%@\n风力%@\n发布时间:%@,",weather.city,weather.temp,weather.WD,weather.WS,weather.time];
        textView.text = weatherInfo;
    }
}

- (void)handleMessage:(BeeMessage *)msg
{
	[super handleMessage:msg];
}

- (void)handleWeatherController:(BeeMessage *)msg
{
	[super handleMessage:msg];
    
	if ( [msg is:WeatherController.LOAD_WEATHER] )
	{
		if ( msg.sending )
		{
			BeeUIActivityIndicatorView * indicator = [BeeUIActivityIndicatorView spawn];
			[self showBarButton:UINavigationBar.BARBUTTON_RIGHT custom:indicator];
			[indicator startAnimating];
		}
		else
		{
			[self showBarButton:UINavigationBar.BARBUTTON_RIGHT system:UIBarButtonSystemItemRefresh];
		}

        
        if ( msg.succeed )
		{
			[self refushTextView];
		}
	}
}

@end




     这里应注意ViewController.m中的代码:
-(void)load{
    _weatherModel = [[WeatherModel alloc]init];
    [_weatherModel addObserver:self];//注意点!!!
}
,结合WeatherModel.m中的
- (void)handleWeatherController:(BeeMessage *)msg
{
	if ( [msg is:WeatherController.LOAD_WEATHER] )
	{
		if ( msg.succeed )
		{
			NSDictionary *dict = [msg.output dictAtPath:@"result"];
            if (dict) {
                self.weather = (Weather *)[dict objectForClass:[Weather class]];
                [self saveCache];
            }
            
		}
	}
    [super handleMessage:msg];//注意点
}

     如果把
[super handleMessage:msg];
去掉,那么ViewController中的handleMessage和handleWeatherController将无法响应。
    
     本篇通过天气预报介绍了Bee中的MVC的实现场景,当然你也可以根据自己的需要选择适用的模块,你可以在Model中直接实现网络层交互,也可以去掉model层,直接在UIBoard中调用BeeController,怎么选择还是看个人习惯。
    
     以上代码下载:https://github.com/ilikeido/BeeFrameworkTest/tree/master/lesson4
    
  • 大小: 65.5 KB
  • 大小: 571.1 KB
  • 大小: 57.7 KB
0
0
分享到:
评论

相关推荐

    BeeFramework

    BeeFramework 在实现 MVC 上有其独特之处: 1. **Model**:模型层负责处理数据和业务逻辑,BeeFramework 提供了数据模型的抽象和管理,使得数据操作更加规范和高效。 2. **View**:视图层负责展示用户界面,...

    三层 MVC 增删改查例子

    在三层MVC架构中,View层并不直接与Model交互,而是通过Controller获取Model的数据并渲染到用户界面上。这样可以确保视图只关注数据的显示,不涉及数据的处理。 **控制器(Controller)**作为Model和View之间的桥梁...

    MVC三层架构

    MVC三层架构基础介绍简单来说,Design Patten 就是一个常用的方案。 在我们的开发过程中,经常会遇到一些相同或者相近的问题,每次我们都会去寻找一个新的解决方法,为了节省时间提高效率,我们提供一些能够解决这些...

    C#MVC 三层架构 ,清晰明了 bootsrap

    【C# MVC 三层架构详解】 C# MVC(Model-View-Controller)是一种设计模式,广泛应用于.NET框架下的Web应用程序开发。它将应用程序分为三个主要部分:模型(Model)、视图(View)和控制器(Controller),以实现...

    C#本科期末大作业MVC三层架构亮灯的仓库管理系统源码.zip

    C#本科期末大作业MVC三层架构亮灯的仓库管理系统源码。MVC+MYSQL+EasyuiC#本科期末大作业MVC三层架构亮灯的仓库管理系统源码。MVC+MYSQL+EasyuiC#本科期末大作业MVC三层架构亮灯的仓库管理系统源码。MVC+MYSQL+...

    ASP.NET 3.5 MVC 架构与事件源代码第 三部分实战篇

    1. **MVC模式**:MVC模式是软件设计中的一种经典模式,它将应用程序分为三个主要组件:模型(Model)、视图(View)和控制器(Controller)。模型负责业务逻辑和数据管理,视图负责展示数据,而控制器处理用户交互并...

    MVC 三层架构示例

    三层架构是一种更高级别的架构设计,它在MVC的基础上增加了数据访问层(Data Access Layer, DAL)。三层架构包括表现层(Presentation Layer,对应MVC中的Controller+View)、业务逻辑层(Business Logic Layer,...

    MVC模式(求三角形面积、计算).pptx

    MVC 模式是软件设计中一种非常重要的模式,它将软件或组件分为三个部分:模型(Model)、视图(View)和控制器(Controller)。这种模式已经 menjadi 必备的开发模式之一,广泛应用于 Web 开发、移动应用开发等领域...

    asp.net MVC三层架构

    Model(模型)是应用程序中用于处理应用程序数据逻辑的部分。 通常模型对象负责在数据库中存取数据。 View(视图)是应用程序中处理数据显示的部分。...这个适合刚接触三层架构的来学习,比较简单。

    C#三层架构的MVC项目源码

    本程序一个基于三层架构的MVC模式应用的完整示例项目源码,基于Asp.net 3.5开发, 结构更简洁,提供更多有效的示例源码参考。 方便用户更好的理解和使用该架构进行开发,配合动软.Net代码生成器,可以使开发效率...

    基于MVC的IOS快速开发框架 BeeFramework.zip

    BeeFramework是一款iOS平台的MVC应用快速开发框架,使用Objective-C开发。 其早期原型曾经被应用在QQ空间 、QQ游戏大厅 等多款精品APP中。 BeeFramework 从根本上解决了iOS开发者长期困扰的各种问题,诸如:分层架构...

    ASP.NET MVC 三层架构与mvc实例

    对于本实例中的"ASP.NET MVC1.0 BBS 简易Demo教学版源码Ajax",可能包含数据库实体类(Models)以及数据库上下文(DbContext)来实现数据访问层。 BBS(Bulletin Board System,电子公告板系统)是在线讨论和信息...

    EF+MVC+三层(MVC +三层架构+EF对数据库对数据库进行增删改查小案例).zip

    在本项目中,"EF+MVC+三层(MVC +三层架构+EF对数据库对数据库进行增删改查小案例).zip"是一个综合性的示例,它演示了如何利用Entity Framework(EF)、Model-View-Controller(MVC)架构以及三层架构来实现对...

    ASP.NETMVC框架开发系列课程2_一个简单的ASP.NETMVC应用程序WebCast20080425Video

    本课程“ASP.NET MVC框架开发系列课程2_一个简单的ASP.NET MVC应用程序WebCast20080425Video”旨在深入讲解如何使用该框架来构建实际的Web应用程序。 首先,我们要理解MVC模式的基本概念。模型(Model)负责处理...

    ASP.NET MVC框架开发系列课程(25):ASP.NET MVC正式版发布

    在本课程中,我们将深入探讨ASP.NET MVC的正式版本,这个版本通常代表着稳定性和性能的优化,以及一系列新功能的引入。 ASP.NET MVC框架的核心理念在于分离关注点,将业务逻辑、数据处理和用户界面三者有效地解耦。...

    ASP.NET MVC框架开发系列课程(1):MVC模式与ASP.NET MVC框架概述

    MVC模式是一种软件设计模式,它将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。这种分离提高了代码的组织性和可维护性。 1. **模型(Model)**:模型负责处理业务逻辑和数据管理...

    MVC模式与三层架构区别

    综上所述,MVC模式与三层架构各有侧重,它们在软件开发的不同阶段发挥着重要作用。MVC模式更专注于表现层的设计与实现,适用于快速迭代和灵活调整用户界面的场景;而三层架构则关注整个应用的结构设计,适用于构建大...

    MVC三层架构例子

    在本示例中,我们将探讨如何在MVC(Model-View-Controller)框架下实现三层架构。 **一、MVC框架介绍** MVC是一种广泛应用于Web应用开发的设计模式,由模型(Model)、视图(View)和控制器(Controller)三部分...

    BeeFramework开源框架

    2. **MVC架构支持**:遵循Model-View-Controller的设计模式,BeeFramework提供了对MVC的强力支持,帮助开发者更好地组织代码,保持良好的代码结构。 3. **网络通信**:框架内置了网络请求处理模块,支持HTTP/HTTPS...

    UML图设计模式、三层架构、MVC.EAP

    UML图设计模式、三层架构、MVC.EAP

Global site tag (gtag.js) - Google Analytics