Skip to content

Instantly share code, notes, and snippets.

View jpd21122012's full-sized avatar
:octocat:

Ing. Jorge Perales Díaz jpd21122012

:octocat:
View GitHub Profile
public class AppTests
{
private readonly TestHost _host;
public AppTests()
{
_host = new TestHost()
.ConfigureServices(services =>
{
services.AddSingleton<INavigationService, TestNavigationService>();
[Test]
public void ProfileViewModel_UpdatesProfileCorrectly()
{
// Arrange
var mockService = new Mock<IUserService>();
mockService.Setup(x => x.UpdateProfile(It.IsAny<Profile>()))
.ReturnsAsync(true);
var vm = new ProfileViewModel(mockService.Object);
public class DbUnitOfWork : IDisposable
{
private readonly SqliteConnection _connection;
public DbUnitOfWork(IConfiguration config)
{
_connection = new SqliteConnection(config.GetConnectionString());
_connection.Open();
}
services.AddScoped<IAuthService, AuthService>();
services.AddSingleton<ITokenCache, SecureTokenCache>();
services.AddTransient<ILoginValidator, LoginValidator>();
services.AddSingleton<ILazyService>(sp =>
new LazyServiceWrapper(() =>
sp.GetRequiredService<IOtherService>()));
// Log service lifetimes
builder.Services.AddLogging(log =>
log.AddFilter("Microsoft.Extensions.DependencyInjection", LogLevel.Debug));
// Inspect container
var serviceDescriptors = builder.Services
.Where(s => s.ServiceType == typeof(IMyService));
public interface IDeviceInfo
{
string Model { get; }
string Platform { get; }
}
// Platform implementations
#if ANDROID
public class AndroidDeviceInfo : IDeviceInfo { ... }
#elif IOS
builder.Services.AddUserProfileModule();
builder.Services.AddCheckoutModule();
// Module extension method
public static IServiceCollection AddUserProfileModule(this IServiceCollection services)
{
services.AddScoped<IUserService, UserService>();
services.AddTransient<UserProfileViewModel>();
return services;
}
// Lightweight service
public class AnalyticsService : IAnalyticsService
{
private readonly ILogger _logger;
// Dependencies injected
public AnalyticsService(ILogger<AnalyticsService> logger)
{
_logger = logger;
}
public class UserSession : IDisposable
{
public Guid SessionId { get; } = Guid.NewGuid();
public DateTime Created { get; } = DateTime.UtcNow;
public void Dispose()
{
// Cleanup session resources
}
}