first edit
This commit is contained in:
10
XLIMS.SERVICES/AssemblyInfo.cs
Normal file
10
XLIMS.SERVICES/AssemblyInfo.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
74
XLIMS.SERVICES/DialogService.cs
Normal file
74
XLIMS.SERVICES/DialogService.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using XLIMS.CONTRACT;
|
||||
using XLIMS.PSV.ViewModels;
|
||||
using XLIMS.PSV.Windows;
|
||||
|
||||
namespace XLIMS.SERVICES;
|
||||
|
||||
public class DialogService : IDialogService
|
||||
{
|
||||
// Словарь: ViewModel → соответствующий Window
|
||||
private readonly Dictionary<Type, Type> _mappings = new();
|
||||
|
||||
public DialogService()
|
||||
{
|
||||
// Регистрируем соответствия (можно вынести в отдельный конфигуратор)
|
||||
Register<BrowseTprzViewModel, EditWindow>();
|
||||
Register<BrowseSerialViewModel, EditWindow>();
|
||||
Register<PsvViewModel, PsvWindow>();
|
||||
Register<GoodViewModel,EditWindow>();
|
||||
Register<BadViewModel,EditWindow>();
|
||||
// и т.д.
|
||||
}
|
||||
|
||||
public void Register<TViewModel, TWindow>()
|
||||
where TViewModel : class
|
||||
where TWindow : Window, new()
|
||||
{
|
||||
_mappings[typeof(TViewModel)] = typeof(TWindow);
|
||||
}
|
||||
|
||||
public void ShowDialog<TViewModel>(TViewModel viewModel)
|
||||
where TViewModel : class
|
||||
{
|
||||
ShowDialogInternal(viewModel, true);
|
||||
}
|
||||
|
||||
public bool? ShowDialog<TViewModel>(TViewModel viewModel, out object? result)
|
||||
where TViewModel : class
|
||||
{
|
||||
var dialogResult = ShowDialogInternal(viewModel, true);
|
||||
result = (dialogResult.Owner as Window)?.DialogResult;
|
||||
return dialogResult.DialogResult;
|
||||
}
|
||||
|
||||
// Немодальное окно (опционально)
|
||||
public void Show<TViewModel>(TViewModel viewModel)
|
||||
where TViewModel : class
|
||||
{
|
||||
ShowDialogInternal(viewModel, false);
|
||||
}
|
||||
|
||||
private Window ShowDialogInternal(object viewModel, bool modal)
|
||||
{
|
||||
var vmType = viewModel.GetType();
|
||||
if (!_mappings.TryGetValue(vmType, out var windowType))
|
||||
throw new InvalidOperationException($"Нет зарегистрированного окна для ViewModel {vmType}");
|
||||
|
||||
var window = (Window)Activator.CreateInstance(windowType)!;
|
||||
window.DataContext = viewModel;
|
||||
|
||||
// Если главное окно уже открыто — устанавливаем Owner (чтобы окно не уходило за него)
|
||||
if (Application.Current?.MainWindow != null && Application.Current.MainWindow != window)
|
||||
window.Owner = Application.Current.MainWindow;
|
||||
|
||||
if (modal)
|
||||
window.ShowDialog();
|
||||
else
|
||||
window.Show();
|
||||
|
||||
return window;
|
||||
}
|
||||
}
|
||||
115
XLIMS.SERVICES/GenericRepository.cs
Normal file
115
XLIMS.SERVICES/GenericRepository.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using XLIMS.CONTRACT;
|
||||
|
||||
namespace XLIMS.SERVICES
|
||||
{
|
||||
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
|
||||
{
|
||||
protected readonly DbContext _context;
|
||||
protected readonly DbSet<TEntity> _dbSet;
|
||||
public event Action SetChanged;
|
||||
|
||||
public GenericRepository(DbContext context)
|
||||
{
|
||||
_context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
_dbSet = context.Set<TEntity>();
|
||||
}
|
||||
|
||||
public async Task<TEntity> GetByIdAsync(int id)
|
||||
{
|
||||
return await _dbSet.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> GetAllAsync()
|
||||
{
|
||||
return await _dbSet.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> WhereAsync(Expression<Func<TEntity, bool>> predicate)
|
||||
{
|
||||
return await _dbSet.Where(predicate).ToListAsync();
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
|
||||
{
|
||||
return _dbSet.Where(predicate);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate)
|
||||
{
|
||||
return await _dbSet.Where(predicate).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<TEntity> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate)
|
||||
{
|
||||
return await _dbSet.SingleOrDefaultAsync(predicate);
|
||||
}
|
||||
|
||||
public async Task AddAsync(TEntity entity)
|
||||
{
|
||||
await _dbSet.AddAsync(entity);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task AddRangeAsync(IEnumerable<TEntity> entities)
|
||||
{
|
||||
await _dbSet.AddRangeAsync(entities);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(TEntity entity)
|
||||
{
|
||||
_dbSet.Update(entity);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task UpdateRangeAsync(IEnumerable<TEntity> entities)
|
||||
{
|
||||
_dbSet.UpdateRange(entities);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task RemoveAsync(TEntity entity)
|
||||
{
|
||||
_dbSet.Remove(entity);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task RemoveRangeAsync(IEnumerable<TEntity> entities)
|
||||
{
|
||||
_dbSet.RemoveRange(entities);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate = null)
|
||||
{
|
||||
if (predicate == null)
|
||||
return await _dbSet.CountAsync();
|
||||
|
||||
return await _dbSet.CountAsync(predicate);
|
||||
}
|
||||
|
||||
public async Task<int> SaveChangesAsync()
|
||||
{
|
||||
return await _context.SaveChangesAsync();
|
||||
|
||||
}
|
||||
|
||||
public int SaveChanges()
|
||||
{
|
||||
return _context.SaveChanges();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
49
XLIMS.SERVICES/LimsService.cs
Normal file
49
XLIMS.SERVICES/LimsService.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Threading.Tasks;
|
||||
using XLIMS.CONTRACT;
|
||||
using XLIMS.DATA.Models;
|
||||
|
||||
namespace XLIMS.SERVICES
|
||||
{
|
||||
public class LimsService : ILimsService
|
||||
{
|
||||
private readonly LimsdbContext _context;
|
||||
|
||||
public IGenericRepository<BookSet> Books { get; private set; }
|
||||
public IGenericRepository<DataSet> Datas { get; private set; }
|
||||
public IGenericRepository<DeviceSet> Devices { get; private set; }
|
||||
public IGenericRepository<DivisionSet> Divisions { get; private set; }
|
||||
public IGenericRepository<DocumentSet> Documents { get; private set; }
|
||||
public IGenericRepository<PersonalSet> Personals { get; private set; }
|
||||
public IGenericRepository<Spnmtp> Spnmtps { get; private set; }
|
||||
public IGenericRepository<Spoi> Spois { get; private set; }
|
||||
public IGenericRepository<Tip> Tips { get; private set; }
|
||||
public IGenericRepository<Tprz> Tprzs { get; private set; }
|
||||
|
||||
public LimsService(LimsdbContext context)
|
||||
{
|
||||
_context = context;
|
||||
|
||||
Books = new GenericRepository<BookSet>(_context);
|
||||
Datas = new GenericRepository<DataSet>(_context);
|
||||
Devices = new GenericRepository<DeviceSet>(_context);
|
||||
Divisions = new GenericRepository<DivisionSet>(_context);
|
||||
Documents = new GenericRepository<DocumentSet>(_context);
|
||||
Personals = new GenericRepository<PersonalSet>(_context);
|
||||
Spnmtps = new GenericRepository<Spnmtp>(_context);
|
||||
Spois = new GenericRepository<Spoi>(_context);
|
||||
Tips = new GenericRepository<Tip>(_context);
|
||||
Tprzs = new GenericRepository<Tprz>(_context);
|
||||
}
|
||||
|
||||
public async Task<int> SaveChangesAsync()
|
||||
{
|
||||
return await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public int SaveChanges()
|
||||
{
|
||||
return _context.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
40
XLIMS.SERVICES/XLIMS.SERVICES.csproj
Normal file
40
XLIMS.SERVICES/XLIMS.SERVICES.csproj
Normal file
@@ -0,0 +1,40 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWPF>true</UseWPF>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="BookService.cs" />
|
||||
<Compile Remove="DataSetSerivce.cs" />
|
||||
<Compile Remove="DeviceService.cs" />
|
||||
<Compile Remove="DivisionService.cs" />
|
||||
<Compile Remove="DocumentService.cs" />
|
||||
<Compile Remove="LimsService1.cs" />
|
||||
<Compile Remove="PersonalService.cs" />
|
||||
<Compile Remove="SpnmtpService.cs" />
|
||||
<Compile Remove="SpoiSevice.cs" />
|
||||
<Compile Remove="TipService.cs" />
|
||||
<Compile Remove="TprzService.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\XLIMS.CONTRACT\XLIMS.CONTRACT.csproj" />
|
||||
<ProjectReference Include="..\XLIMS.DATA\XLIMS.DATA.csproj" />
|
||||
<ProjectReference Include="..\XLIMS.DEV\XLIMS.DEV.csproj" />
|
||||
<ProjectReference Include="..\XLIMS.PSV\XLIMS.PSV.csproj" />
|
||||
<ProjectReference Include="..\XLIMS.SP\XLIMS.SP.csproj" />
|
||||
<ProjectReference Include="..\XLIMS.TPRZ\XLIMS.TPRZ.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
49
XLIMS.SERVICES/XLimsServicesDI.cs
Normal file
49
XLIMS.SERVICES/XLimsServicesDI.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Configuration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.IO;
|
||||
using XLIMS.DATA.Models;
|
||||
using XLIMS.CONTRACT;
|
||||
using XLIMS.SERVICES;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public static class XLimsServicesDI
|
||||
{
|
||||
public static IServiceCollection AddXLimsServices(this IServiceCollection services)
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||
.Build();
|
||||
|
||||
// Регистрируем DbContext с использованием строки из конфига
|
||||
services.AddDbContext<LimsdbContext>(options =>
|
||||
{
|
||||
var connectionString = configuration.GetConnectionString("DefaultConnection");
|
||||
options.UseSqlServer(connectionString);
|
||||
// Опционально: логирование запросов в Debug
|
||||
// options.LogTo(Console.WriteLine, LogLevel.Information);
|
||||
});
|
||||
|
||||
// Остальные сервисы
|
||||
// Фасад
|
||||
services.AddScoped<ILimsService, LimsService>();
|
||||
|
||||
// Сервисы
|
||||
services.AddSingleton<IDialogService, DialogService>();
|
||||
|
||||
// ViewModel'и
|
||||
services.AddTransient<XLIMS.PSV.ViewModels.MainViewModel>();
|
||||
services.AddTransient<XLIMS.SP.ViewModels.MainViewModel>();
|
||||
services.AddTransient<XLIMS.TPRZ.ViewModels.MainViewModel>();
|
||||
services.AddTransient<XLIMS.DEV.ViewModels.MainViewModel>();
|
||||
|
||||
// Сохраняем конфигурацию (если понадобится в других местах)
|
||||
services.AddSingleton<IConfiguration>(configuration);
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user