2010-12-22 15 views
7

का संयोजन यह एक प्रश्न की तुलना में अधिक टिप्पणी है, हालांकि फीडबैक अच्छा होगा। मुझे एक नई परियोजना के लिए यूजर इंटरफेस बनाने का काम सौंपा गया है जो हम कर रहे हैं। हम डब्ल्यूपीएफ का उपयोग करना चाहते हैं और मैं उपलब्ध आधुनिक यूआई डिजाइन तकनीकों को सीखना चाहता हूं। चूंकि मैं डब्ल्यूपीएफ के लिए बिल्कुल नया हूं, मैं शोध कर रहा हूं कि क्या उपलब्ध है। मुझे लगता है कि मैंने एमवीवीएम लाइट टूलकिट (मुख्य रूप से इसकी "ब्लेंडेबिलिटी" और इवेंट टॉममंड व्यवहार के कारण) पर काफी हद तक बस गए हैं, लेकिन मैं आईओसी भी शामिल करना चाहता था। तो, यहां मैं यही आया हूं। मैंने निर्भरता इंजेक्शन को संभालने के लिए एक यूनिटीकॉन्टेनर का उपयोग करने के लिए एमवीवीएम लाइट प्रोजेक्ट में डिफॉल्ट व्यूमोडेलोकेटर क्लास को संशोधित किया है। ध्यान में रखते हुए मुझे नहीं पता था कि इन शर्तों में से 9 0% का मतलब 3 महीने पहले था, मुझे लगता है कि मैं सही रास्ते पर हूं।एमवीवीएम लाइट टूलकिट और यूनिटी 2.0

// Example of MVVM Light Toolkit ViewModelLocator class that implements Microsoft 
// Unity 2.0 Inversion of Control container to resolve ViewModel dependencies. 

using Microsoft.Practices.Unity; 

namespace MVVMLightUnityExample 
{ 
    public class ViewModelLocator 
    { 
     public static UnityContainer Container { get; set; } 

     #region Constructors 
     static ViewModelLocator() 
     { 
      if (Container == null) 
      { 
       Container = new UnityContainer(); 

       // register all dependencies required by view models 
       Container 
        .RegisterType<IDialogService, ModalDialogService>(new ContainerControlledLifetimeManager()) 
        .RegisterType<ILoggerService, LogFileService>(new ContainerControlledLifetimeManager()) 
        ; 
      } 
     } 

     /// <summary> 
     /// Initializes a new instance of the ViewModelLocator class. 
     /// </summary> 
     public ViewModelLocator() 
     { 
      ////if (ViewModelBase.IsInDesignModeStatic) 
      ////{ 
      //// // Create design time view models 
      ////} 
      ////else 
      ////{ 
      //// // Create run time view models 
      ////} 

      CreateMain(); 
     } 

     #endregion 

     #region MainViewModel 

     private static MainViewModel _main; 

     /// <summary> 
     /// Gets the Main property. 
     /// </summary> 
     public static MainViewModel MainStatic 
     { 
      get 
      { 
       if (_main == null) 
       { 
        CreateMain(); 
       } 
       return _main; 
      } 
     } 

     /// <summary> 
     /// Gets the Main property. 
     /// </summary> 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", 
      "CA1822:MarkMembersAsStatic", 
      Justification = "This non-static member is needed for data binding purposes.")] 
     public MainViewModel Main 
     { 
      get 
      { 
       return MainStatic; 
      } 
     } 

     /// <summary> 
     /// Provides a deterministic way to delete the Main property. 
     /// </summary> 
     public static void ClearMain() 
     { 
      _main.Cleanup(); 
      _main = null; 
     } 

     /// <summary> 
     /// Provides a deterministic way to create the Main property. 
     /// </summary> 
     public static void CreateMain() 
     { 
      if (_main == null) 
      { 
       // allow Unity to resolve the view model and hold onto reference 
       _main = Container.Resolve<MainViewModel>(); 
      } 
     } 

     #endregion 

     #region OrderViewModel 

     // property to hold the order number (injected into OrderViewModel() constructor when resolved) 
    public static string OrderToView { get; set; } 

     /// <summary> 
     /// Gets the OrderViewModel property. 
     /// </summary> 
     public static OrderViewModel OrderViewModelStatic 
     { 
      get 
      { 
       // allow Unity to resolve the view model 
       // do not keep local reference to the instance resolved because we need a new instance 
       // each time - the corresponding View is a UserControl that can be used multiple times 
       // within a single window/view 
       // pass current value of OrderToView parameter to constructor! 
       return Container.Resolve<OrderViewModel>(new ParameterOverride("orderNumber", OrderToView)); 
      } 
     } 

     /// <summary> 
     /// Gets the OrderViewModel property. 
     /// </summary> 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", 
      "CA1822:MarkMembersAsStatic", 
      Justification = "This non-static member is needed for data binding purposes.")] 
     public OrderViewModel Order 
     { 
      get 
      { 
       return OrderViewModelStatic; 
      } 
     } 
     #endregion 

     /// <summary> 
     /// Cleans up all the resources. 
     /// </summary> 
     public static void Cleanup() 
     { 
      ClearMain(); 
      Container = null; 
     } 
    } 
} 

और MainViewModel वर्ग दिखा निर्भरता इंजेक्शन उपयोग:

using GalaSoft.MvvmLight; 
using Microsoft.Practices.Unity; 

namespace MVVMLightUnityExample 
{ 
    public class MainViewModel : ViewModelBase 
    { 
     private IDialogService _dialogs; 
     private ILoggerService _logger; 

     /// <summary> 
     /// Initializes a new instance of the MainViewModel class. This default constructor calls the 
     /// non-default constructor resolving the interfaces used by this view model. 
     /// </summary> 
     public MainViewModel() 
      : this(ViewModelLocator.Container.Resolve<IDialogService>(), ViewModelLocator.Container.Resolve<ILoggerService>()) 
     { 
      if (IsInDesignMode) 
      { 
       // Code runs in Blend --> create design time data. 
      } 
      else 
      { 
       // Code runs "for real" 
      } 
     } 

     /// <summary> 
     /// Initializes a new instance of the MainViewModel class. 
     /// Interfaces are automatically resolved by the IoC container. 
     /// </summary> 
     /// <param name="dialogs">Interface to dialog service</param> 
     /// <param name="logger">Interface to logger service</param> 
     public MainViewModel(IDialogService dialogs, ILoggerService logger) 
     { 
      _dialogs = dialogs; 
      _logger = logger; 

      if (IsInDesignMode) 
      { 
       // Code runs in Blend --> create design time data. 
       _dialogs.ShowMessage("Running in design-time mode!", "Injection Constructor", DialogButton.OK, DialogImage.Information); 
       _logger.WriteLine("Running in design-time mode!"); 
      } 
      else 
      { 
       // Code runs "for real" 
       _dialogs.ShowMessage("Running in run-time mode!", "Injection Constructor", DialogButton.OK, DialogImage.Information); 
       _logger.WriteLine("Running in run-time mode!"); 
      } 
     } 

     public override void Cleanup() 
     { 
      // Clean up if needed 
      _dialogs = null; 
      _logger = null; 

      base.Cleanup(); 
     } 
    } 
} 

और OrderViewModel वर्ग:

using GalaSoft.MvvmLight; 
using Microsoft.Practices.Unity; 

namespace MVVMLightUnityExample 
{ 
    /// <summary> 
    /// This class contains properties that a View can data bind to. 
    /// <para> 
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel. 
    /// </para> 
    /// <para> 
    /// You can also use Blend to data bind with the tool's support. 
    /// </para> 
    /// <para> 
    /// See http://www.galasoft.ch/mvvm/getstarted 
    /// </para> 
    /// </summary> 
    public class OrderViewModel : ViewModelBase 
    { 

     private const string testOrderNumber = "123456"; 
     private Order _order; 

     /// <summary> 
     /// Initializes a new instance of the OrderViewModel class. 
     /// </summary> 
     public OrderViewModel() 
      : this(testOrderNumber) 
     { 

     } 

     /// <summary> 
     /// Initializes a new instance of the OrderViewModel class. 
     /// </summary> 
     public OrderViewModel(string orderNumber) 
     { 
      if (IsInDesignMode) 
      { 
       // Code runs in Blend --> create design time data. 
       _order = new Order(orderNumber, "My Company", "Our Address"); 
      } 
      else 
      { 
       _order = GetOrder(orderNumber); 
      } 
     } 

     public override void Cleanup() 
     { 
      // Clean own resources if needed 
      _order = null; 

      base.Cleanup(); 
     } 
    } 
} 

और कोड जो किसी विशेष के लिए एक आदेश दृश्य प्रदर्शित करने के लिए इस्तेमाल किया जा सकता आदेश:

public void ShowOrder(string orderNumber) 
{ 
    // pass the order number to show to ViewModelLocator to be injected 
    //into the constructor of the OrderViewModel instance 
    ViewModelLocator.OrderToShow = orderNumber; 

    View.OrderView orderView = new View.OrderView(); 
} 

इन उदाहरणों को केवल आईओसी विचार दिखाने के लिए हटा दिया गया है। इसने समाधान के लिए आने के लिए यूनिटी 2.0 दस्तावेज की कमी (एक्सटेंशन के लिए नमूना कोड में) की कमी के बारे में बहुत कुछ परीक्षण और त्रुटि ली, और इंटरनेट का पता लगाया। अगर आपको लगता है कि इसे बेहतर किया जा सकता है तो मुझे बताएं।

+0

मुझे यह जानना अच्छा लगेगा कि आप कहां महसूस करते हैं कि यूनिटी दस्तावेज़ में छेद हैं। आपको और क्या चाहिए था जो वहां नहीं था? –

+0

यूनिटी प्रलेखन बहुत अच्छा है, मुझे 2.0 के लिए नमूना कोड स्निपेट खोजने में मुश्किल हो रही है, खासकर एक्सटेंशन का उपयोग करने के लिए। 1.x के लिए बहुत सारे उदाहरण कोड थे, लेकिन कई पुराने थे (मैंने संदेशों को सिंटैक्स का उपयोग करके चीजें नहीं करने के लिए संदेश प्राप्त किए क्योंकि एक्सटेंशन का उपयोग करके एक नया वाक्यविन्यास उपलब्ध था)। – acordner

+0

यह पहली बार पोस्ट करने के बाद लगभग एक साल हो गया है, एकॉर्डर। मैं उन विचारों के साथ संघर्ष करना शुरू कर रहा हूं जो आप वापस थे। मैं जानना चाहता हूं कि आपके साथ चीजें कहां खड़ी हैं, जहां तक ​​एमवीवीएम लाइट/यूनिटी जाती है। – Rod

उत्तर

1

सबसे पहले, आपको सेवा लोकेटर विरोधी पैटर्न से बचना चाहिए।

दूसरा, आपको ऑर्डर व्यू प्राप्त करने के लिए हर बार एक स्थिर चर सेट करना होगा? यह एक बहुत बदसूरत डिजाइन है।

+0

जैसा कि मैंने कहा, मैं इस सामान पर वास्तव में नया हूं और बस अपना रास्ता महसूस करने की कोशिश कर रहा हूं। मैं न केवल आलोचना के लिए खुला हूं, बल्कि इसे सुधारने के सुझाव भी हूं। मैंने अभी तक एक एमवीवीएम पैटर्न का उपयोग करके एक पूर्ण काम करने वाला ऐप नहीं रखा है और मैं सड़क से बहुत दूर जाने से पहले सबसे अच्छा संभव समाधान ढूंढना चाहता हूं। मुझे लगता है कि अन्य, अधिक अनुभवी डेवलपर्स इसे अलग करने के लिए सुधार अवसरों को ढूंढने का एक अच्छा तरीका है। मैं "सेवा लोकेटर एंटी-पैटर्न" में देखूंगा (क्योंकि मुझे पता नहीं है कि इस बिंदु पर इसका क्या अर्थ है) :) – acordner

+0

मैं ऑर्डर व्यू प्राप्त करने से पहले एक स्थिर चर सेट करने से सहमत हूं, यह एक अच्छा समाधान नहीं है, लेकिन जब तक मैं सभी को समझ नहीं पाता पैरामीटर ओवरराइडिंग और डी तकनीक, यही वह है जो मैं आया था। – acordner

+0

एक बेहतर समाधान एक अमूर्त कारखाना पैटर्न होगा जिसे इंजेक्शन दिया जा सकता है। – onof

संबंधित मुद्दे