public class TestViewModel { private readonly ITestService _testService; public TestViewModel(ITestService testService) //تزریق وابستگی در سازنده کلاس { _testService = testService; }
نام تمام Viewهای برنامه به View ختم میشوند و نام ViewModelها به ViewModel. برای مثال TestViewModel و TestView معرف یک ViewModel و View متناظر خواهند بود.
ساختار کلاسهای لایه سرویس برنامه
namespace DI07.Services { public interface ITestService { string Test(); } } namespace DI07.Services { public class TestService: ITestService { public string Test() { return "برای آزمایش"; } } }
علامتگذاری ViewModelها
در ادامه یک اینترفیس خالی را به نام IViewModel مشاهده میکنید:
namespace DI07.Core { public interface IViewModel // از این اینترفیس خالی برای یافتن و علامتگذاری ویوو مدلها استفاده میکنیم { } }
برای نمونه کلاس TestViewModel برنامه، با پیاده سازی IViewModel، به نوعی نشانه گذاری نیز شده است:
using DI07.Services; using DI07.Core; namespace DI07.ViewModels { public class TestViewModel : IViewModel // علامتگذاری ویوو مدل { private readonly ITestService _testService; public TestViewModel(ITestService testService) //تزریق وابستگی در سازنده کلاس { _testService = testService; } public string Data { get { return _testService.Test(); } } } }
تنظیمات آغازین IoC Container مورد استفاده
در کلاس استاندارد App برنامه WPF خود، کار تنظیمات اولیه StructureMap را انجام خواهیم داد:
using System.Windows; using DI07.Core; using DI07.Services; using StructureMap; namespace DI07 { public partial class App { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); ObjectFactory.Configure(cfg => { cfg.For<ITestService>().Use<TestService>(); cfg.Scan(scan => { scan.TheCallingAssembly(); // Add all types that implement IView into the container, // and name each specific type by the short type name. scan.AddAllTypesOf<IViewModel>().NameBy(type => type.Name); scan.WithDefaultConventions(); }); }); } } }
همچنین در ادامه از قابلیت اسکن این IoC Container برای یافتن کلاسهایی که IViewModel را در اسمبلی جاری پیاده سازی کردهاند، استفاده شده است. متد NameBy، سبب میشود که بتوان به این نوعهای یافت شده از طریق نام کلاسهای متناظر دسترسی یافت.
اتصال خودکار ViewModelها به Viewهای برنامه
using System.Windows.Controls; using StructureMap; namespace DI07.Core { /// <summary> /// Stitches together a view and its view-model /// </summary> public static class ViewModelFactory { public static void WireUp(this ContentControl control) { var viewName = control.GetType().Name; var viewModelName = string.Concat(viewName, "Model"); //قرار داد نامگذاری ما است control.Loaded += (s, e) => { control.DataContext = ObjectFactory.GetNamedInstance<IViewModel>(viewModelName); }; } } }
استفاده از کلاس ViewModelFactory
در ادامه کدهای TestView و پنجره اصلی برنامه را مشاهده میکنید:
<UserControl x:Class="DI07.Views.TestView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <TextBlock Text="{Binding Data}" /> </Grid> </UserControl> <Window x:Class="DI07.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Views="clr-namespace:DI07.Views" Title="MainWindow" Height="350" Width="525"> <Grid> <Views:TestView /> </Grid> </Window>
using DI07.Core; namespace DI07.Views { public partial class TestView { public TestView() { InitializeComponent(); this.WireUp(); //تزریق خودکار وابستگیها و یافتن ویوو مدل متناظر } } }
دریافت پروژه کامل این قسمت
DI07.zip
.NET Core + Angular Dashboard
Topics Covered:
- Building a dashboard application in Angular
- Building a Web API in .NET Core 2.0
- Using Chart.js to build stunning charts of different types
- Making HTTP requests using Angular to query a Web API
- Using Postman to send requests
- Working with Observables
- Using Input and Output decorators in Angular
- Using PostgreSQL and pgAdmin
- Automatically seeding a database with large amounts of sample data
- Styling an application using custom CSS and Bootstrap 4
- Using Map, Filter, and Reduce in Javascript
- Creating Routes in Angular
- Get, Put, Post, Patch Web API Controller Action request types
- Configuring your API for CORS
ASP.NET Core و سازمانها
However, it is clear that Microsoft’s future direction is in the Core space . Yes, the last update to the full .NET framework did include improvements for ASP.NET and WebForms, but clearly the future innovation and hard work will be in the new core frameworks like .NET core, ASP.NET Core, Entity Framework core, and whatever other cores come along in the future.
عدم ارسال ایمیل در هاست
Someone recently used your password to try to sign in to your Google Account MyEmail@gmail.com. This person was using an application such as an email client or mobile device. We prevented the sign-in attempt in case this was a hijacker trying to access your account. Please review the details of the sign-in attempt: if you do not recognize this sign-in attempt, someone else might be trying to access your account. You should sign in to your account and reset your password immediately.
This project uses mocha, chai, typescript and vs code debug toghether, so you can write your api tests easily.
This is useful for scenarios when you've some api developed in python for example, but you'd prefer to develop tests in typescript/javascript.
Another use case is when you've some legacy api and you want to develop some tests to make [risk free change] of your legacy api possibel!