NLayerApp分层架构学习笔记

1、crossdomain.xml主要是为了让RIA(Flash、Silverlight)跨域来访问文件。

 

2、SOAPAction HTTP request header被用来标识SOAP HTTP请求的目的地,其值是个URI地址。SOAP发送并不限制格式、URI特征或其必须可解析,那么在这种情况下,发送一个HTTP SOAP请求时,其HTTP客户端必须使用/指明SOAPAction HTTP request header。

SOAPAction header的内容可以被用在服务端,诸如:防火墙适当的过滤基于HTTP的SOAP请求消息等场景。SOAPAction header的值为空串("")表示SOAP消息的目的地由HTTP请求的URI标识;无值则表示没有指定这条消息的目的地。

 

3、MVC.Client中

Global.asax注册路由映射;

routes.MapRoute( 
"Paged", // Route name 
"{controller}/{action}/{page}/{pageSize}", // URL with parameters 
new { controller = "Home", action = "Index" }, // Parameter defaults 
new { page = @"\d+", pageSize = @"\d+"} 
);

page和pageSize正则约束;

IoCFactory(IOC工厂)单例类,默认创建一个IoCUnityContainer类;

IBootStrapper Boot包装接口,默认创建一个DefaultBootStrapper类,里面可以通过Boot方法来注册MV的相关绑定,包括Controller,Model,以及工厂。

public void Boot() 
{ 
    //Register controllers into MVC infrastructure 
    RegisterControllers();

    //Register new model binders 
    RegisterModelBinders();

    //register factories 
    RegisterFactories(); 
}

RegisterControllers注册相关Controller类。

其中RegisterModelBinders注册SelfTrackingEntityModelBinder<Customer>,SelfTrackingEntityModelBinder作为一个自定义的模型绑定器。将Hidden的Base64值反序列化为实体。然后通过Action的参数获取该实体,如Edit/Customer的实现。

RegisterFactories

IControllerFactory默认使用IoCControllerFactory,并且设置ControllerBuilder.Current.SetControllerFactory(factory);

MvcHandler类通过使用单例类ControllerBuilder的GetControllerFactory方法获取当前指定的IControllerFactory对象,通过此对象来生成具体的IController控制器。

通过ControllerBuilder的SetControllerFactory方法我们可以指定自定义的控制器工厂,自定义工厂可以直接实现IControllerFactory接口,也可以从DefaultControllerFactory类继承

通过IoCUnityContainer,注册其他层组件中的相关服务以及仓储:

public IoCUnityContainer() 
{ 
    _ContainersDictionary = new Dictionary();

        //Create root container 
    IUnityContainer rootContainer = new UnityContainer(); 
    _ContainersDictionary.Add("RootContext", rootContainer);

        //Create container for real context, child of root container 
    IUnityContainer realAppContainer = rootContainer.CreateChildContainer(); 
    _ContainersDictionary.Add("RealAppContext", realAppContainer);

        //Create container for testing, child of root container 
    IUnityContainer fakeAppContainer = rootContainer.CreateChildContainer(); 
    _ContainersDictionary.Add("FakeAppContext", fakeAppContainer);

   
    ConfigureRootContainer(rootContainer); 
    ConfigureRealContainer(realAppContainer); 
    ConfigureFakeContainer(fakeAppContainer); 
}

RootContainer作为公共的容器。RealAppContainer作为真实场景的容器;FakeAppContainer作为模拟场景的容器。

这里采用层级容器的好处是我们可以对于有不同生命周期的对象放在不同的容器中,如果一个子容器被释放,不会影响到其它子容器中的对象,但是如果根节点处父容器释放后,所有的子容器都将被释放。
这样以后可以直接通过Resolve<T>获得接口服务。

ISalesManagementService salesManagement = IoCFactory.Instance.CurrentContainer.Resolve();

 

4、使用Html.RenderAction方法

呈现用户控件内容的方式和一般情况下呈现视图的方式相同,即通过控制器决定呈现哪个具体的用户控件内容。RenderAction方法可以灵活的通过控制器选择呈现特定的视图、用户控件或部分内容。

 

5、<%= Html.DisplayFor(x => x.PageItems) %>

由于在IPagedView中,有一个属性:

///  
/// Gets the page items. 
///  
/// The page items. 
[UIHint("IEnumerable")] 
IEnumerable PageItems { get; }

这样视图引擎就会去遍历目录,从Shared的DisplayTemplate中去查找IEnumerable.ascx:

1 <% foreach (var item in Model) 
2 
3 ViewData["item"= item; 
4 string templateName = (string)string.Concat(item.GetType().Name,"Item"); 
5 %> 
6 <%: Html.Display("item",templateName) %> 
7 <% } %>

 

 

6、tml.Display("item",templateName)从而从CustomerItem.ascx模板中去加载表达式为item的数据(ViewData[“item”]=item;

 

7、File(photo.Photo, "img") Controller基类中的File方法可以返回一个文件内容和文件类型的FileContentResult对象。

 

8、使用的 SQL Server 版本不支持数据类型“datetime2”的解决办法

错误原因,在使用ado.net entity的时候,entity使用的数据库是sqlserver 2008,

但后来实际使用中使用的数据库是sqlserver 2005,

使用的 SQL Server 版本不支持数据类型“datetime2”

The version of SQL Server in use does not support datatype ‘datetime2
解决办法
Open your EDMX in a file editor (or “open with…” in Visual Studio and select XML Editor). At the top you will find the storage model and it has an attribute ProviderManifestToken. This has should have the value 2008. Change that to 2005, recompile and everything works.

就是打开你的edmx文件,搜索ProviderManifestToken值,改为2005就ok了

 

9、T4模板引擎的使用,自动生成ADO.NET 实体数据类型类。

 

10、Silverlight项目

MenuControl.xaml为右上角的菜单条;((MainPage)App.Current.RootVisual).actualState 可以获得MainPage中的actureState变量。VisualStateManager.GoToState(mainPage, "ToCustomer", true);对当前功能状态进行切换。

 

11、MainPage中的初始化函数中

this.MouseWheel += new MouseWheelEventHandler(MainPage_MouseWheel); 
actualState = "ToCustomer";

MouseWheel方法对用鼠标滚轮使用时候,对当前功能状态的切换。

 

12、M-V-VM模式,例如视图中CustomerListView后台中与VMCustomerListView进行双向绑定,其中VMCustomerListView为ViewModels中的类

ListBox中的属性SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"绑定VMCustomerListView的SelectedCustomer;

属性ItemsPanel="{StaticResource ItemsPanelTemplate}"设置每一条项(Customer)的UI显示方式(这里为StackPanel);

ItemTemplate="{StaticResource CustomerDataTemplate}"设置他的自定义数据模板,其中CustomerDataTemplate对应:

 1 <DataTemplate x:Key="CustomerDataTemplate"> 
 2 <Border Padding="2" Width="786"> 
 3 <Grid ToolTipService.ToolTip="{Binding ContactName}" Cursor="Hand"> 
 4 <Grid.ColumnDefinitions> 
 5 <ColumnDefinition/> 
 6 <ColumnDefinition Width="Auto" MinWidth="295"/> 
 7 </Grid.ColumnDefinitions> 
 8 <Image Height="91" Margin="0,0,23,0" Source="/Microsoft.Samples.NLayerApp.Presentation.Silverlight.Client;component/Resources/Images/ListBackground.png" Stretch="Fill" VerticalAlignment="Bottom" Grid.ColumnSpan="2"/> 
 9 <Grid Margin="5,21,12,21" Grid.Column="1"> 
10 <Rectangle Fill="#FF007DAC" Margin="0,0,0,9"/> 
11 <Image Margin="0" Source="/Microsoft.Samples.NLayerApp.Presentation.Silverlight.Client;component/Resources/Images/TitleRibbon.png" Stretch="Fill" Height="49" VerticalAlignment="Center" Width="157" HorizontalAlignment="Right"> 
12 <Image.OpacityMask> 
13 <LinearGradientBrush EndPoint="0.026,0.5" StartPoint="0.981,0.5"> 
14 <GradientStop Color="Black" Offset="0"/> 
15 <GradientStop Color="#33FFFFFF" Offset="1"/> 
16 </LinearGradientBrush> 
17 </Image.OpacityMask> 
18 </Image> 
19 <TextBlock Foreground="White" Text="{Binding CompanyName}" FontSize="21.333" FontFamily="/Microsoft.Samples.NLayerApp.Presentation.Silverlight.Client;component/Resources/Fonts/HelveticaThn.ttf#Helvetica35-Thin" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" d:LayoutOverrides="HorizontalAlignment" Margin="9,2,2,12"/> 
20 </Grid> 
21 <StackPanel Grid.Row="2" Margin="118,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" d:LayoutOverrides="GridBox"> 
22 <TextBlock d:LayoutOverrides="Width" Foreground="White" Text="{Binding ContactName}" FontSize="26.667" FontFamily="/Microsoft.Samples.NLayerApp.Presentation.Silverlight.Client;component/Resources/Fonts/HelveticaThn.ttf#Helvetica35-Thin" TextWrapping="Wrap"/> 
23 <TextBlock Foreground="#FF959595" Text="{Binding Address.City}" FontSize="18.667" FontFamily="/Microsoft.Samples.NLayerApp.Presentation.Silverlight.Client;component/Resources/Fonts/HelveticaThn.ttf#Helvetica35-Thin" FontWeight="Bold" HorizontalAlignment="Left" Margin="0,2,0,0" VerticalAlignment="Bottom"/> 
24 </StackPanel> 
25 </Grid> 
26 </Border> 
27 </DataTemplate>

体现了页面的UI展示。

ItemsSource="{Binding Customers}" ListBox的数据源绑定到Customers中,其中Customers的类型为ObservableCollection<Customer>,它是个动态数据集合,当列表有变化时,可以发送通知,通过RaisePropertyChanged来触发变动事件。

VMCustomerListView继承ObservableObject基类,ObservableObject继承INotifyPropertyChanged接口。

 1 <Button x:Name="BTN_AddCustomer" 
 2 VerticalAlignment="Top" 
 3 Style="{StaticResource ButtonStyle2}" 
 4 Foreground="White" 
 5 FontFamily="/Microsoft.Samples.NLayerApp.Presentation.Silverlight.Client;component/Resources/Fonts/HelveticaThn.ttf#Helvetica35-Thin" 
 6 FontSize="12" 
 7 Height="40" 
 8 Margin="0,0,62,0" 
 9 HorizontalAlignment="Right" 
10 Width="40" Cursor="Hand"> 
11 <i:Interaction.Triggers> 
12 <i:EventTrigger EventName="Click"> 
13 <i:InvokeCommandAction Command="{Binding AddCommand}"/> 
14 </i:EventTrigger> 
15 </i:Interaction.Triggers> 
16 <Grid> 
17 <Path x:Name="Shape_AddCustomer" 
18 Data="F1M36.622,28.377C36.622,28.377 43.42,28.377 43.42,28.377 43.42,28.377 43.42,34.35 43.42,34.35 43.42,34.35 49.394,34.35 49.394,34.35 49.394,34.35 49.394,41.147 49.394,41.147 49.394,41.147 43.42,41.147 43.42,41.147 43.42,41.147 43.42,47.533 43.42,47.533 43.42,47.533 36.622,47.533 36.622,47.533 36.622,47.533 36.622,41.147 36.622,41.147 36.622,41.147 30.855,41.147 30.855,41.147 30.855,41.147 30.855,34.35 30.855,34.35 30.855,34.35 36.622,34.35 36.622,34.35 36.622,34.35 36.622,28.377 36.622,28.377z M23.868,1.019C30.444,1.019 35.775,6.35 35.775,12.926 35.775,18.982 32.448,23.97 26.597,24.723 28.748,24.917 31.102,25.503 33.399,26.387 33.399,26.387 33.399,32.519 33.399,32.519 33.399,32.519 27.481,32.519 27.481,32.519 27.481,32.519 27.481,43.698 27.481,43.698 27.481,43.698 33.399,43.698 33.399,43.698 33.399,43.698 33.399,49.304 33.399,49.304 30.83,49.235 27.689,49.35 23.864,49.327 1.204,49.189 3.217,51.085 3.217,39.255 3.217,31.681 13.217,25.455 21.248,24.724 15.395,23.974 11.962,18.984 11.962,12.926 11.962,6.35 17.293,1.019 23.868,1.019z" 
19 Fill="#FFFFFFFF" 
20 UseLayoutRounding="False" 
21 Stretch="Fill" 
22 HorizontalAlignment="Center" 
23 VerticalAlignment="Center" 
24 Width="20" 
25 Height="20" /> 
26 </Grid> 
27 </Button>

当点击AddCustomer按钮时候,这里使用了Interaction.Triggers可以绑定自定义事件。这里事件名为“Click”,命令动作绑定AddCommand,在VMCustomerListView中,它调用了AddExecute方法:

private void AddExecute(Object o) 
{

Customer current = (Customer)o; 
((MainPage)App.Current.RootVisual).addCustomer.DataContext = new ViewModels.VMAddCustomer(); 
((MainPage)App.Current.RootVisual).GotoAddCustomer.Begin();

Debug.WriteLine("Add Customer."); 
}

GotoAddCustomer是个Storyboard动画对象的Name名称,它的动画主要把当前CustomerList的Grid隐藏,将AddCustomer的Grid显示,然后就可以同样通过DataContext对应的VMAddCustomer去绑定视图中的AddCustomerView。

FlowGrid继承Grid,作为Silverlight中间内容的自定义控件,它加入了一些页面的特效,例如当鼠标移入该区域的时候,有一个悬浮蠕动的效果。

MouseMove += (FloatingGrid_MouseMove); 
MouseEnter += (FloatingGrid_MouseEnter); 
MouseLeave += (FloatingGrid_MouseLeave);

这些代码产生鼠标移动触发的事件。

 

 

13、BinaryImageConverter通过二进制字节的图像类型绑定图片的显示,它继承IValueConverter。具体它的原理可以参考我之前的一篇文章:《Silverlight渐进学习系列(1)——IValueConverter的使用

 

14、CustomerListView中TextBlock属性Text="{Binding CurrentPage, Converter={StaticResource CurrentPageConverter}}",通过当前页面绑定页码。

 

(NLayerApp相关文章索引:http://kb.cnblogs.com/tag/NLayerApp/

后面再补充!

 

posted @ 2011-07-07 09:07  Leepy  阅读(2943)  评论(1编辑  收藏  举报