Microsoft의 .NET Core 팀은 인기 있는 라이브러리인 Json.NET(Newtonsoft)을 제거하고 새로운 Json 라이브러리인 System.Text.Json(네임스페이스)을 추가하였다. 아래는 이를 활용한 간단한 기본 예제이다.
// 기본 사용법
class Student{}
JsonSerializer.Serialize(Student.GetList(), options);
JsonSerializer.Deserialize<List<Student>>(jsonString, options);
아래는 콘솔 모드에서 작성한 전체 소스이다.
WPF-MVVM 방식에 있어서 ICommand는 필수요소인 데 일반적으로 xaml의 Element(Button, TextBox 등)와 연결된 명령 동작의 이벤트를 처리한다. 우선 ICommand를 상속받은 RelayCommand 클래스를 만들고 ViewModel에서 이를 활용한다. 아래의 소스는 RelayCommand 클래스와 이를 활용한 이벤트 처리 예제이다.
<!-- <Fluent:Button Header="프로그램 종료" Command="{Binding TestCommand}" CommandParameter="{Binding ElementName=TestButton, Path=Header}" x:Name="TestButton"/> -->
<Fluent:Button Header="프로그램 종료" Command="{Binding TestCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" x:Name="TestButton"/>
// ViewModel: Binding Path에 따라 object -> string
public ICommand TestCommand => new RelayCommand<object>(TestRun, TestCheck);
private void TestRun(object x)
{
MessageBox.Show((x as Button).Name);
}
private bool TestCheck(object x)
{
return x is Button;
}
Autofac is an IoC container for Microsoft .NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity(GitHub-Autofac 2019). Autofac is an addictive Inversion of Control container for .NET Core, ASP.NET Core, .NET 4.5.1+, Universal Windows apps, and more(autofac.org 2019).
닷넷 프레임워크를 사용하다 보면 의존성 주입이란 단어가 자주 등장한다. ASP.NET Core MVC, WPF MVVM 그리고 Caliburn.Micro에서 Ioc.Get<T>() 에서 DI를 사용하는 예를 볼 수 있는 데 이번 글에서는 일반적인 개발환경에서 DI를 자동으로 그리고 쉽게 사용할 수 있는 Autofac 패키지의 사용법을 간단하게 작성해보았다. 참고 : 일반적인 DI, Interface 사용법 보기
// Program.cs
using Autofac;
using System;
namespace ConsoleUI
{
class Program
{
static void Main()
{
var container = ContainerConfig.Configure();
using(var scope = container.BeginLifetimeScope())
{
var app = scope.Resolve<IApplication>();
app.Execute();
}
Console.ReadLine();
}
}
}