Monday, August 29, 2011

(ZT)MVC vs. MVP vs. MVVM Jul 18

 

From link:

http://nirajrules.wordpress.com/2009/07/18/mvc-vs-mvp-vs-mvvm/

An important FAQ. The answer actually depends on where the person is coming from. MVC is a fundamental pattern which has been tweaked quite a bit to fit into various platforms. For instance if you had asked anybody how to implement an MVC in ASP.NET (prior to release of ASP.NET MVC framework) you would get very different answers. So let’s start with basic. The common motivation behind all 3 is separation of concerns, cutting flab from UI (good for UI designers), swapping UIs (for instance windows to web), make UI easy for Unit Testing, etc. Have a look at the below diagram, I have taken it from CAB documentation.

MVCMVP

MVC: Three components – View (your UI), Model (your business entities / data – that view is displaying) & Controller (contains the logic that alters the model depending on the action triggered by UI, typically implementing a Use Case). It’s widely known that MVC is a compound pattern (View and Controller have Strategy implementation, View itself can be a Composite implementation & View and Model are synched through Observer). In this case Controller doesn’t know anything about View, and the idea is that a View can switch Controllers (for instance depending upon who has logged to the system) & a single controller can be used by multiple Views. View subscribes to the changes done to the model & hence both are sync from the data perspective. One of the disadvantages of MVC is that it’s difficult to unit test. Controller manipulates the data but how about asserting those changes from a view perspective. For instance on click of a button you raise an event to controller, and controller modifies the value in model. This value modification changes the font size / color in View. Unit testing this scenario is slightly difficult in MVC.

MVP: Again three components. But dependencies change (look at arrows in the diagram). Over here we replace Controller with Presenter (one which presents the changes done in model back to view). The main difference between both is that Presenter refers back to the view while Controller doesn’t. Normal pattern found here is to create an abstraction of the View (in terms of properties / events) & Presenter refers to it. This makes the mocking of View much easier & hence the Unit Testing aspect. Presenter here hence takes the responsibility of not only manipulating model but also updating the view. Of course the implementations of MVP differ in real world in terms of how much thin the view is, some prefer keeping basic logic still inside view & taking complex logic in presenter, while others prefer keeping the entire logic in Presenter. Martin fowler describes 2 variations on MVP on these lines namely – Supervising Controller & Passive View described below

(A Passive View handles this by reducing the behavior of the UI components to the absolute minimum by using a controller that not just handles responses to user events, but also does all the updating of the view. This allows testing to be focused on the controller with little risk of problems in the view.

Supervising Controller uses a controller both to handle input response but also to manipulate the view to handle more complex view logic. It leaves simple view behavior to the declarative system, intervening only when effects are needed that are beyond what can be achieved declaratively.)

MVVM: Model–View-ViewModel talks of creating a new model (in addition to your domain model). This model normally adds additonal properties from the prespective of View (as we understand that View has controls in addition to data which it’s displaying). For instance if View had a property IsChecked and Presenter was setting in classic MVP, in MVVM Presenter will have that IsChecked Property which View will sync up with (doesn’t it look like Strategy pattern has been replaced with Observer?). So now a Presenter becomes more like a combo of – View Properties & Model properties which would be synchronized with View. So why not rename Presenter to ViewModel? Do that and you get MVVM. MVVM is attractive for platforms which support bi-directional binding with less effort. Also a minor tradeoff is ViewModel unlike Presenter can stand on its own (Presenter normally requires a View’s interface). Martin fowler describes similar pattern called Presentation Model & Josh Smith captures MVVM implementation for WPF / Silverlight in this article.

MVVM

ASP.NET MVC: So what has MVC got to do with ASP.NET MVC? First, Web works on a different model. Here, user interacts with HTML in browser and send a request back to the server for processing (for client side Ajax you might go just for data). As the interaction is normally stateless, when the request comes back to the server we need to recreate our View, load the model back & manipulate both of them as required. There are 2 variations on how handle this recreation – Page Controller & Front Controller. Make Page the decision maker – in this widely implemented pattern HTTP request is specific to physical page on server (.aspx for instance) & page in turn creates itself (builds the view from postback data) decides what model it needs and triggers the manipulation (events in codebehind file) it requires. As you see here the distinction between View & Controller becomes blur & is little difficult to separate. This where ASP.NET MVC comes in which behaves like a Front Controller – where Controller is the decision maker. Here all HTTP requests are mapped to methods on the Controller Class. Controller class recreates the model & view as required and does the manipulations. This makes unit testing easier as we can directly instantiate the front controller class & invoke methods on it to perform the assertions.

I can add code snippets to above explanations if you feel they would help you understand things better. I will look forward to your comments :) .

Friday, August 26, 2011

SSIS Deployment With SQL Server Agent

(1) . Change SQL Server Agent Log on as Network Service (default is Localsystem) from target sql server configuration manager ( eg. target sql server is colvts02, need to do this work under colvts02)
(2). Using windows Authentication in all SSIS packages.

Friday, August 12, 2011

SSAS 名词 (ZT)

在SSAS(SQL Server Analysis Services)中构建Cube和编写MDX的时候,我们很容易被一些名词弄糊涂,比如:Dimension(维度),Measures Dimension(度量维度),Measure(度量),Hierarchy(层次结构),Attribute hierarchy(属性层次结构),Level(级别),Cell(单元),Member(成员),Member Property(成员属性),Set(集),Turple(元组)等等。要想弄清楚这些名词,就必须理解Cube的结构。
CubeDimensionMeasure
Cube就象一个坐标系,每一个Dimension代表一个坐标轴,要想得到一个点,就必须在每一个坐标轴上取的一个值,而这个点就是Cube中的Cell。见下图
clip_image001
上 图很好的说明了Cube、Dimension、Measure之间的关系。这里需要注意的是:其实Measure也属于一个维度,即Measures Dimension。所有的Measure构成了Measures Dimension,这个维度的只有一个Hierarchy,而且这个Hierarchy只有一个层次(Level)。
HierarchyLevelMemeber
在 上节的图中,每个Dimension只有一个Hierarchy,而在实际的环境中,一个Dimension往往有很多Hierarchy。因此,上一小 节中关于“Cube就象一个坐标系,每一个Dimension代表一个坐标轴”这句话其实不够准确,准确的说应该是每一个Hierarchy代表了一个坐 标轴,而Hierarchy中每一个Member代表了坐标轴上的一个值。下图以时间维度为例展示了Dimension的内部结构。
clip_image002
此外,我们需要说明的是:

1) 上图中说明的是一般Dimension的结构,在实际的模型中,其实可以做很多自定义的工作。比如:我们可以修改Hierarchy的默认Member。
2) 一般情况下,SSAS中Hierarchy的默认Member是All(在你的模型中,可能叫其他名称)。换句话说在MDX中[时间].[财政]等价于[时间].[财政].[All],[时间].[财政].Children等价于[时间].[财政].[All].Children。
3) Dimension_Name.Hierarchy_Name.Level_Name等价于Dimension_Name.Hierarchy_Name.Level_Name.Members。比如:[时间].[财政].[半年]等价于[时间].[财政].[半年].Members。Level的Members是该级别的所有元素(对于[时间].[财政].[半年].Members={[上半年],[下半年],[上半年],[下半年]},其中前两个是2001年下的,后两个属于2002年),而Hierarchy的Members包含了该Hierarchy下所有的内容。
4) 当且仅当一个Dimension下只有一个Hierarchy,则Dimension_Name等价于Dimension_Name.Hierarchy_Name纬度。比方说:时间维度只有一个财务Hierarchy,则[时间]等价[时间].[财务]。
5) Attribute Hierarchy中Members的层次是两层(MSDN的说法更加准确,这里简化了一些):第一层:All,第二层:叶子节点。也就是说它和多层的Hierarchy相比,两者结构完全相同,这是统一维度模型(Unified Dimensional Model)一个方面的体现。
注意:采用Attribute Hierarchy能够使编写MDX更加容易,但同时也增加了Cube的容量,加大了Cells的个数,对性能有负面影响。因此,在建模的时候,我们可以把一些Attribute Hierarchy的AttributeHierarchyEnabled属性设置False同时在编写MDX时,以Member Property的方式来引用,这样可以在满足需求的前提下提高性能。
6) Measures Dimension是一个特殊的维度,它的Members中没有All这个成员,它的默认Member可以在建模时指定。
7)对于一般的维度,其第一层Level的默认是“(All)”。
TurpleSet
如 果说Cube好像一个坐标系,那么Turple、Set的关系就好比点和面的关系。Turple由Cube中每个Hierarchy的一个Member组 成。由于Hierarchy的个数非常多,所以一般不可能在Turple表达式中把所有的Member都明确指定,故此,为了简化开发,所有没有明确指定 Member的Hierarchy,用该Hierarchy的默认Member代替。也就是说:([时间].[财政].[2001].[上半年]) 等价于([时间].[财政].[2001].[上半年],[时间].[日历].[All])。另外我们需要注意的:

1) 有的说法认为:Turple是“Cube 上的一个子集(不断开的子Cube),这个看法是不准确的,因为Turple只是一个点,不是面,它仅仅由每个Hierarchy的一个Member组成的。

2) 外面()起来的表达式不一定是Turple。比如:([时间].[财政].[半年].Members,[时间].[日历].[2001].[上半年])就不是一个Turple,而是一个Set,其原因在于,Turple是点,它仅仅由每个Hierarchy的一个Member组成,如果在任何一个Hierarchy上有两个成员,则其就变成Set了。

注意:([时间].[财政].[半年].Members,[时间].[日历].[2001].[上半年])等价于Crossjoin([时间].[财政].[半年].Members,[时间].[日历].[2001].[上半年])或{[时间].[财政].[半年].Members}*{[时间].[日历].[2001].[上半年]},在SSAS的MDX中,我们可以在()中定义多个用逗号分隔开的表达式,编译器会进行分析,如果发现是Set的话,就把它转化成多个Set相乘的形式。

3) Set中的Turple可以重复。比如:{[时间].[日历].[2001].[上半年],[时间].[日历].[2001].[上半年]}并不等于{[时间].[日历].[2001].[上半年]},因为前者有两个Turple,后者只有一个。

4) SSAS能够根据上下文的需要,自动把Turple变成Set,单个Member变成Turple,多个Member变成Set。这也是我们常常混淆Turple和Set的原因。详细的例子如下:

a)上下文需要Set时,([时间].[日历].[2001].[上半年])自动转化成{[时间].[日历].[2001].[上半年]}。
b)上下文需要Turple时,[时间].[日历].[2001].[上半年]自动转化成([时间].[日历].[2001].[上半年])。
c)上下文需要Set时,[时间].[日历].[2001].Children自动转化成{[时间].[日历].[2001].Children}。

总结
总体来看,SSAS中的Cube的内部结构非常的清晰,在实际开发中,只要多注意一下默认的一些转化,使用起来是很容易的。

Monday, August 08, 2011

Func Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Func Add = (x, y) => x + y;
Func Minus = (x, y) => x - y;

var richard = Minus;

var jx = OperatreWithFunc(3, 5, Add);
var jx2 = OperatreWithFunc(6, 3,richard);
var jx3 = OperatreWithFunc(3, 53, GetFunc());

}


static int OperatreWithFunc(int x ,int y,Func fun)
{
return fun(x, y);

}

static Func GetFunc()
{
return (a, b) => a + b;
}

}
}

Thursday, August 04, 2011

OLAP (ZT)

OLAP从字面上是指及时的数据分析。但实用中,OLAP更多的是指针对数据分析的一种解决方案。OLAP的软件把数据预处理成数据立方。基本是把有可能的汇总都预先算出来。在用户选择显示某种汇总时,OLAP可以在预先的计算出来的结果基础上很快地计算出结果。从而可以很好地支持极大量数据的及时分析。

也就是说OLAP实际上包括了两个方面。

第一,OLAP提供了一个互动的数据分析的模式。OLAP把数据分成维(DIMENSION)和度量(MEASURE)。用户通过选择DIMENSION和MEASURE,可以及时地计算数据在不同DIMENSION的组合下的汇总。在这里可能有一个误区。其实OLAP本身并没有定义一个用户端的互动模式。比如大部分传统的OLAP是通过一个交叉表的界面,但是新的界面更多的采取可视化的图表形式。

第二点,可能是更本质的,是OLAP定义的一种对数据的处理方式。这包括了对数据预计算的思路和方式。在国内厂家里,很少有自己开发OLAP后端数据处理的。在国外厂家中,有自己OLAP后端的有IBM,MICROSOFT,ORACLE等。

作为一个有着十几年实际应用历史的技术,OLAP是当之无愧的数据分析的元老。但在OLAP的应用中,用户也遇到了很多问题。其中最为关键的就是OLAP在灵活性上的缺陷。OLAP的使用要求用户事先对数据有一个分析,把所有的DIMENSION和MEASURE都罗列出来。这样OLAP处理器就可以把相关的值预先计算出来。

而由于数据立方需要很大的空间。它的大小是以指数增长的。所以在设计OLAP的时候,用户又必须把DIMENSION的数量限制在一个相对比较小的范围。在实际应用时,用户只能在这个预先选择好的DIMENSION之内来进行分析。如果用户需要从不同的角度来看这些数据,这个OLAP就必须重新设计和生成。

近年来,BI领域出现了几项大的技术革新,在很大程度上弥补了OLAP的不足。在前端的显示上,数据分析更多地采用了可视化的模式。这里并不是简单地指通过图表来显示OLAP的结果。新的可视化数据分析工具在通过图表对数据进行显示的基础上,实现了一个完整的互动模式,让用户可以任意的通过不同的角度和方法对数据进行检验。

在后端的处理上,以列为主导的数据库结构,及 云计算的概念,使及时分析可以不再依靠预计算就可以处理极大的数据量。这样既解决了大数据的运算,也避免了OLAP所带来的不灵活的问题。

STYLESCOPE是一个综合了可视化理论,和以列为主的云计算的数据处理结构,达到了一个完美的结合。用户不再需要面对复杂的OLAP系统,可以十分快捷地创建起一个强大的数据分析系统。