using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Services;
///
/// Resolves instance unique names to site identifiers using the configuration database.
///
public class InstanceLocator : IInstanceLocator
{
private readonly ScadaBridgeDbContext _context;
/// Initializes the locator with the EF Core database context.
/// The database context used to look up instances and sites.
public InstanceLocator(ScadaBridgeDbContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
///
public async Task GetSiteIdForInstanceAsync(
string instanceUniqueName,
CancellationToken cancellationToken = default)
{
// One untracked projected join: this sits on the inbound-API Route.To hot path and
// only needs a single string, so it must not materialise (and track) two entities.
return await (
from i in _context.Set().AsNoTracking()
join s in _context.Set().AsNoTracking() on i.SiteId equals s.Id
where i.UniqueName == instanceUniqueName
select s.SiteIdentifier)
.FirstOrDefaultAsync(cancellationToken);
}
}