2016-05-26 6 views
7

मेरे पास टेम्पलेट 10 के आधार पर एक ऐप है और IoC का उपयोग करके मेरे निर्भरता इंजेक्शन को संभालना चाहता हूं। मैं इसके लिए एकता का उपयोग करने की ओर झुका रहा हूँ। मेरा ऐप तीन विधानसभाओं में विभाजित है:मैं टेम्पलेट 10 के साथ एकता आईओसी कंटेनर का उपयोग कैसे करूं?

  1. यूआई (यूनिवर्सल एप्लिकेशन)
  2. यूआई तर्क (यूनिवर्सल पुस्तकालय)
  3. कोर तर्क (पोर्टेबल पुस्तकालय)।

    1. मैं पूरी अनुप्रयोग के लिए एक ही कंटेनर का उपयोग करना चाहिए, या प्रत्येक विधानसभा के लिए एक बनाने के लिए:

    मैं इन प्रश्न हैं?

  4. मुझे कंटेनर कहां बनाना चाहिए और मेरी सेवाओं को पंजीकृत करना चाहिए?
  5. विभिन्न असेंबली में विभिन्न वर्गों को कंटेनर तक कैसे पहुंचाया जाना चाहिए? सिंगलटन पैटर्न?

मैंने डीआई और आईओसी के बारे में बहुत कुछ पढ़ा है, लेकिन मुझे यह जानने की जरूरत है कि अभ्यास में सभी सिद्धांतों को विशेष रूप से टेम्पलेट 10 में कैसे लागू किया जाए।

कोड रजिस्टर करने के लिए:

// where should I put this code? 
var container = new UnityContainer(); 
container.RegisterType<ISettingsService, RoamingSettingsService); 

और फिर कोड उदाहरणों को पुनः प्राप्त करने:

var container = ??? 
var settings = container.Resolve<ISettingsService>(); 
+0

संभावित डुप्लिकेट - क्यों मैं सभी परतों को संदर्भित करने की क्या ज़रूरत है/प्रविष्टि में विधानसभाओं आवेदन?] (http://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry- appplication) – NightOwl888

उत्तर

3

मैं Unity Container से परिचित नहीं।

मेरा उदाहरण LightInject का उपयोग कर रहा है, आप Unity का उपयोग कर समान अवधारणा को लागू कर सकते हैं। ViewModel पर DI को सक्षम करने के लिए आपको अपनी परियोजना पर App.xaml.cs पर ResolveForPage ओवरराइड करने की आवश्यकता है।

public class MainPageViewModel : ViewModelBase 
{ 
    ISettingsService _setting; 
    public MainPageViewModel(ISettingsService setting) 
    { 
     _setting = setting; 
    } 
} 


[Bindable] 
sealed partial class App : Template10.Common.BootStrapper 
{ 
    internal static ServiceContainer Container; 

    public App() 
    { 
     InitializeComponent(); 
    } 

    public override async Task OnInitializeAsync(IActivatedEventArgs args) 
    { 
     if(Container == null) 
      Container = new ServiceContainer(); 

     Container.Register<INavigable, MainPageViewModel>(typeof(MainPage).FullName); 
     Container.Register<ISettingsService, RoamingSettingsService>(); 

     // other initialization code here 

     await Task.CompletedTask; 
    } 

    public override INavigable ResolveForPage(Page page, NavigationService navigationService) 
    { 
     return Container.GetInstance<INavigable>(page.GetType().FullName); 
    } 
} 

Template10 automaticaly, MainPageViewModel को DataContext सेट हो जाएगा आप का उपयोग करने के {x:bind}MainPage.xaml.cs पर चाहते हैं:

public class MainPage : Page 
{ 
    MainPageViewModel _viewModel; 

    public MainPageViewModel ViewModel 
    { 
     get { return _viewModel??(_viewModel = (MainPageViewModel)this.DataContext); } 
    } 
} 
+0

उत्तर के लिए धन्यवाद! यह मेरे लिए अच्छा काम किया! शैल व्यू में वीएम इंजेक्ट करने का कोई तरीका है? –

+0

@NickGoloborodko मैं आपके प्रश्न के बारे में काफी समझ नहीं पा रहा हूं, शायद आपको इसके बारे में नया SO प्रश्न बनाना चाहिए? –

0

यहाँ एक छोटा सा उदाहरण मैं कैसे एकता का उपयोग करें और खाका 10.

1 है एक ViewModel

I लोगों की सूची बनाने के लिए एक डेटा सेवा वर्ग भी बनाया। [निर्भरता] एनोटेशन पर एक नज़र डालें।

public class UnityViewModel : ViewModelBase 
{ 
    public string HelloMessage { get; } 

    [Dependency] 
    public IDataService DataService { get; set; } 

    private IEnumerable<Person> people; 
    public IEnumerable<Person> People 
    { 
     get { return people; } 
     set { this.Set(ref people, value); } 
    } 

    public UnityViewModel() 
    { 
     HelloMessage = "Hello !"; 
    } 

    public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, 
     IDictionary<string, object> suspensionState) 
    { 
     await Task.CompletedTask; 
     People = DataService.GetPeople(); 
    } 
} 

2. बना सकते हैं और भरने के अपने UnityContainer

UnityViewModel और unityContainer को DataService जोड़ें एक कक्षा बनाएँ। UnityViewModel को हल करने के लिए एक संपत्ति बनाएं।

public class UnitiyLocator 
{ 
    private static readonly UnityContainer unityContainer; 

    static UnitiyLocator() 
    { 
     unityContainer = new UnityContainer(); 
     unityContainer.RegisterType<UnityViewModel>(); 
     unityContainer.RegisterType<IDataService, RuntimeDataService>(); 
    } 

    public UnityViewModel UnityViewModel => unityContainer.Resolve<UnityViewModel>(); 
} 

3. आपके ऐप पर UnityLocator जोड़ें।XAML

<common:BootStrapper x:Class="Template10UWP.App" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:common="using:Template10.Common" 
       xmlns:mvvmLightIoc="using:Template10UWP.Examples.MvvmLightIoc" 
       xmlns:unity="using:Template10UWP.Examples.Unity"> 


<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Styles\Custom.xaml" /> 
      <ResourceDictionary> 
       <unity:UnitiyLocator x:Key="UnityLocator" /> 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 

     <!-- custom resources go here --> 

    </ResourceDictionary> 
</Application.Resources> 

4. नियंत्रण के लिए DataContext के रूप में UnityViewModel गुण सेट और बाध्य करने के लिए पेज

उपयोग UnityLocator बनाएं

<Page 
x:Class="Template10UWP.Examples.Unity.UnityMainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:Template10UWP.Examples.Unity" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
DataContext="{Binding Source={StaticResource UnityLocator}, Path=UnityViewModel}" 
mc:Ignorable="d"> 

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <TextBlock Text="{Binding HelloMessage}" HorizontalAlignment="Center" 
       VerticalAlignment="Center" /> 

    <ListBox Grid.Row="1" ItemsSource="{Binding People}" DisplayMemberPath="FullName"> 

    </ListBox> 
</Grid> 

जब पेज UnityViewModel को हल करता है तो डेटा सेवा स्वचालित रूप से इंजेक्शन दी जाएगी।

अब आपके प्रश्नों

  1. को इस तरह की परियोजनाओं के एक दूसरे पर निर्भर पर निर्भर करता है। मुझे यकीन नहीं है कि सबसे अच्छा समाधान क्या है, लेकिन मुझे लगता है कि मैं एक यूनिटीकंटनर का उपयोग करने की कोशिश करता हूं और इसे मूल पुस्तकालय में रखता हूं।

  2. मुझे आशा है कि मेरे उदाहरण इस सवाल का जवाब

  3. मुझे आशा है कि मेरे उदाहरण इस सवाल का जवाब

[आईओसी/DI के
संबंधित मुद्दे