2011-11-21 106 views
0

我對C#/ WPF相當陌生,但這是我用於此項目的內容。在不同程序集中共享應用程序數據

讓我來介紹一下上下文:我有一個允許用戶登錄的應用程序。我有一些基本的信息數據(物理位置)和一些動態信息(用戶名...)。我用單身經理來處理所有這些事情。

該基本應用程序與兩個不同的項目一起使用:登錄後,應用程序必須顯示當前正在運行的項目的xaml頁面。

一些配置枚舉允許我通過一個通用接口從正確的項目程序集實例化正確的對象(我想我應該使用WCF ...我們稍後會看到:))。

現在我需要一些由單身管理者在項目組裝中處理的信息。 我該怎麼辦?

我不能分享單身...我應該寫一個共同的數據庫嗎?或者擺脫單身?我有點失落。這當然是一個簡單的架構問題,但我無法弄清楚。

非常感謝您的幫助!

+0

我使用MEF在我的模塊導出和導入登錄信息 – blindmeis

回答

1

我建議你使用MEF來導入和導出不同DLL的信息。每個DLL可以有資源(如XAML文件)以供查看。所以,我的選擇是MVVM + MEF模塊化+ Prism EventAggregator用於交互模塊。 一些樣品,只是一個概念演示:

'單經理' 的種類:

public class AddinsManager : IAddinsManager 
{ 
    [ImportMany(typeof(IModule))] 
    private OrderingCollection<IModule, IOrderMetadata> modules = new OrderingCollection<IModule, IOrderMetadata>(lazyRule => lazyRule.Metadata.Order); 

    private CompositionContainer container; 
    private bool isInitialized = false; 

    /// <summary> 
    /// Gets available application modules (add-ins) 
    /// </summary> 
    public List<IModule> Modules { get; private set; } 

    /// <summary> 
    /// Initialize manager 
    /// </summary> 
    public void Initialize() 
    { 
     Assembly oAssembly = Assembly.GetAssembly(Application.Current.GetType()); 
     this.container = new CompositionContainer(GetCatalog(oAssembly)); 
     this.container.ComposeParts(this, Context.Current); 

     this.Modules = this.modules 
          .Select(lazy => lazy.Value) 
          .ToList(); 

     this.isInitialized = true; 
    } 

    /// <summary> 
    /// Initialize modules 
    /// </summary> 
    public void InitializeModules() 
    { 
     foreach (IModule oModule in this.Modules) 
     { 
      oModule.Initialize(); 
     } 
    } 

    /// <summary> 
    /// Injects dependencies in specified container 
    /// </summary> 
    /// <param name="host">Container to inject in</param> 
    public void Compose(object host) 
    { 
     if (host == null) 
     { 
      throw new ArgumentNullException(); 
     } 

     this.EnsureInitialize(); 

     this.container.ComposeParts(host); 
    } 

    /// <summary> 
    /// Register views of the modules 
    /// </summary> 
    public void RegisterViews() 
    { 
     this.EnsureInitialize(); 

     foreach (IModule oModule in this.Modules) 
     { 
      foreach (Uri oUri in oModule.GetViewPath().ToArray()) 
      { 
       ResourceDictionary oDictionary = new ResourceDictionary(); 
       oDictionary.Source = oUri; 
       Application.Current.Resources.MergedDictionaries.Add(oDictionary); 
      } 
     } 
    } 

    /// <summary> 
    /// Get catalog for modules load 
    /// </summary> 
    /// <param name="assembly">Assembly to search modules</param> 
    /// <returns>Catalog for modules load</returns> 
    private static AggregateCatalog GetCatalog(Assembly assembly) 
    { 
     string sDirName = Path.GetDirectoryName(assembly.Location); 
     DirectoryCatalog oDirCatalog = new DirectoryCatalog(sDirName, "Company.*"); 
     AssemblyCatalog oAssemblyCatalog = new AssemblyCatalog(assembly); 

     AggregateCatalog oCatalog = new AggregateCatalog(oAssemblyCatalog, oDirCatalog); 
     return oCatalog; 
    } 

    /// <summary> 
    /// Ensure if manager was initialized 
    /// </summary> 
    private void EnsureInitialize() 
    { 
     if (!this.isInitialized) 
     { 
      throw new Exception("Add-ins Manager Component not initialized"); 
     } 
    } 
} 

模塊(DLL單獨的項目)實施

[Export(typeof(IModule))] 
public class LayoutsModule : AModule 
{ 
    private static LayoutsVM viewModel; 

    /// <summary> 
    /// Gets reporting add-in main view model 
    /// </summary> 
    public static LayoutsVM ViewModel 
    { 
     get 
     { 
      if (viewModel == null) 
      { 
       viewModel = Context 
           .Current 
           .LayoutManager 
           .ModulesVM 
           .OfType<LayoutsVM>() 
           .FirstOrDefault(); 
      } 

      return viewModel; 
     } 
    } 

    /// <summary> 
    /// Initialize module 
    /// </summary> 
    public override void Initialize() 
    { 
     Context 
      .Current 
      .EventAggregator 
      .GetEvent<MenuInitializing>() 
      .Subscribe(this.InitializeMenu); 

     base.Initialize(); 
    } 
} 

查看嵌入式資源(View.xaml )在每個DLL上。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:vm="clr-namespace:Company.Client.ViewModels"> 

    <DataTemplate DataType="{x:Type vm:ApplicationVM}"> 
     <ContentPresenter Content="{Binding AddinsViewModel}" /> 
    </DataTemplate> 

    <DataTemplate DataType="{x:Type vm:MenuVM}">   
     <ContentPresenter Content="{Binding RibbonData}" /> 
    </DataTemplate> 

</ResourceDictionary> 
+0

由於人很多:)我不知道MEF,但它確實看起來像什麼,我想實現。非常感謝 ! – Hellin

相關問題