Creates ServerRegistrationService

This commit is contained in:
Shannon Deminick
2013-02-13 04:22:46 +06:00
parent 3b25214433
commit 6423914f01
9 changed files with 261 additions and 84 deletions

View File

@@ -14,9 +14,17 @@ namespace Umbraco.Core.Models.Rdbms
public int Id { get; set; }
[Column("address")]
[Length(100)]
[Length(500)]
public string Address { get; set; }
/// <summary>
/// A unique column in the database, a computer name must always be unique!
/// </summary>
[Column("computerName")]
[Length(255)]
[Index(IndexTypes.UniqueNonClustered, Name = "IX_computerName")]
public string ComputerName { get; set; }
[Column("registeredDate")]
[Constraint(Default = "getdate()")]
public DateTime DateRegistered { get; set; }
@@ -28,5 +36,7 @@ namespace Umbraco.Core.Models.Rdbms
[Column("isActive")]
[Index(IndexTypes.NonClustered)]
public bool IsActive { get; set; }
}
}

View File

@@ -16,31 +16,36 @@ namespace Umbraco.Core.Models
/// </summary>
/// <param name="id"></param>
/// <param name="serverAddress"></param>
/// <param name="computerName"></param>
/// <param name="createDate"></param>
/// <param name="updateDate"></param>
public ServerRegistration(int id, string serverAddress, DateTime createDate, DateTime updateDate)
public ServerRegistration(int id, string serverAddress, string computerName, DateTime createDate, DateTime updateDate)
{
UpdateDate = updateDate;
CreateDate = createDate;
Key = Id.ToString().EncodeAsGuid();
Id = id;
ServerAddress = serverAddress;
ComputerName = computerName;
}
/// <summary>
/// Creates a new instance for persisting a new item
/// </summary>
/// <param name="serverAddress"></param>
/// <param name="computerName"></param>
/// <param name="createDate"></param>
public ServerRegistration(string serverAddress, DateTime createDate)
public ServerRegistration(string serverAddress, string computerName, DateTime createDate)
{
CreateDate = createDate;
UpdateDate = createDate;
Key = 0.ToString().EncodeAsGuid();
ServerAddress = serverAddress;
ComputerName = computerName;
}
public string ServerAddress { get; set; }
public string ServerAddress { get; set; }
public string ComputerName { get; set; }
public bool IsActive { get; set; }
}
}

View File

@@ -9,8 +9,8 @@ namespace Umbraco.Core.Persistence.Factories
#region Implementation of IEntityFactory<Language,LanguageDto>
public ServerRegistration BuildEntity(ServerRegistrationDto dto)
{
return new ServerRegistration(dto.Id, dto.Address, dto.DateRegistered, dto.LastNotified);
{
return new ServerRegistration(dto.Id, dto.ComputerName, dto.Address, dto.DateRegistered, dto.LastNotified);
}
public ServerRegistrationDto BuildDto(ServerRegistration entity)
@@ -20,7 +20,8 @@ namespace Umbraco.Core.Persistence.Factories
Address = entity.ServerAddress,
DateRegistered = entity.CreateDate,
IsActive = entity.IsActive,
LastNotified = entity.UpdateDate
LastNotified = entity.UpdateDate,
ComputerName = entity.ComputerName
};
if (entity.HasIdentity)
dto.Id = short.Parse(entity.Id.ToString(CultureInfo.InvariantCulture));

View File

@@ -26,6 +26,7 @@ namespace Umbraco.Core.Persistence.Mappers
CacheMap<ServerRegistration, ServerRegistrationDto>(src => src.ServerAddress, dto => dto.Address);
CacheMap<ServerRegistration, ServerRegistrationDto>(src => src.CreateDate, dto => dto.DateRegistered);
CacheMap<ServerRegistration, ServerRegistrationDto>(src => src.UpdateDate, dto => dto.LastNotified);
CacheMap<ServerRegistration, ServerRegistrationDto>(src => src.ComputerName, dto => dto.ComputerName);
}
internal override string Map(string propertyName)

View File

@@ -9,6 +9,13 @@ namespace Umbraco.Core.Persistence
/// </summary>
public class RepositoryFactory
{
internal virtual ServerRegistrationRepository CreateServerRegistrationRepository(IDatabaseUnitOfWork uow)
{
return new ServerRegistrationRepository(
uow,
NullCacheProvider.Current);
}
internal virtual IUserTypeRepository CreateUserTypeRepository(IDatabaseUnitOfWork uow)
{
return new UserTypeRepository(

View File

@@ -18,7 +18,7 @@ using Umbraco.Core.Publishing;
namespace Umbraco.Core.Services
{
/// <summary>
/// <summary>
/// Represents the Content Service, which is an easy access to operations involving <see cref="IContent"/>
/// </summary>
public class ContentService : IContentService

View File

@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Services
{
/// <summary>
/// Service to manage server registrations in the database
/// </summary>
internal class ServerRegistrationService
{
private readonly RepositoryFactory _repositoryFactory;
private readonly IDatabaseUnitOfWorkProvider _uowProvider;
public ServerRegistrationService()
: this(new RepositoryFactory())
{}
public ServerRegistrationService(RepositoryFactory repositoryFactory)
: this(new PetaPocoUnitOfWorkProvider(), repositoryFactory)
{}
public ServerRegistrationService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory)
{
if (provider == null) throw new ArgumentNullException("provider");
if (repositoryFactory == null) throw new ArgumentNullException("repositoryFactory");
_uowProvider = provider;
_repositoryFactory = repositoryFactory;
}
/// <summary>
/// Called to 'call home' to ensure the current server has an active record
/// </summary>
/// <param name="address"></param>
public void EnsureActive(string address)
{
var uow = _uowProvider.GetUnitOfWork();
using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow))
{
//NOTE: we cannot use Environment.MachineName as this does not work in medium trust
// found this out in CDF a while back: http://clientdependency.codeplex.com/workitem/13191
var computerName = System.Net.Dns.GetHostName();
var query = Query<ServerRegistration>.Builder.Where(x => x.ComputerName.ToUpper() == computerName.ToUpper());
var found = repo.GetByQuery(query).ToArray();
ServerRegistration server;
if (found.Any())
{
server = found.First();
server.ServerAddress = address; //This should not really change but it might!
server.UpdateDate = DateTime.UtcNow; //Stick with Utc dates since these might be globally distributed
server.IsActive = true;
}
else
{
server = new ServerRegistration(address, computerName, DateTime.UtcNow);
}
repo.AddOrUpdate(server);
uow.Commit();
}
}
/// <summary>
/// Deactivates a server by name
/// </summary>
/// <param name="computerName"></param>
public void DeactiveServer(string computerName)
{
var uow = _uowProvider.GetUnitOfWork();
using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow))
{
var query = Query<ServerRegistration>.Builder.Where(x => x.ComputerName.ToUpper() == computerName.ToUpper());
var found = repo.GetByQuery(query).ToArray();
if (found.Any())
{
var server = found.First();
server.IsActive = false;
repo.AddOrUpdate(server);
uow.Commit();
}
}
}
/// <summary>
/// Return all active servers
/// </summary>
/// <returns></returns>
public IEnumerable<ServerRegistration> GetActiveServers()
{
var uow = _uowProvider.GetUnitOfWork();
using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow))
{
var query = Query<ServerRegistration>.Builder.Where(x => x.IsActive);
return repo.GetByQuery(query).ToArray();
}
}
}
}

View File

@@ -673,6 +673,7 @@
<Compile Include="Services\LocalizationService.cs" />
<Compile Include="Services\MacroService.cs" />
<Compile Include="Services\MediaService.cs" />
<Compile Include="Services\ServerRegistrationService.cs" />
<Compile Include="Services\ServiceContext.cs" />
<Compile Include="Services\UserService.cs" />
<Compile Include="Sync\DefaultServerMessenger.cs" />

View File

@@ -1,4 +1,5 @@
using System;
using System.Data.SqlServerCe;
using System.Linq;
using NUnit.Framework;
using Umbraco.Core.Models;
@@ -20,6 +21,42 @@ namespace Umbraco.Tests.Persistence.Repositories
CreateTestData();
}
[Test]
public void Cannot_Add_Duplicate_Computer_Names()
{
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
// Act
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
var server = new ServerRegistration("http://shazwazza.com", "COMPUTER1", DateTime.Now);
repository.AddOrUpdate(server);
Assert.Throws<SqlCeException>(unitOfWork.Commit);
}
}
[Test]
public void Cannot_Update_To_Duplicate_Computer_Names()
{
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
// Act
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
var server = repository.Get(1);
server.ComputerName = "COMPUTER2";
repository.AddOrUpdate(server);
Assert.Throws<SqlCeException>(unitOfWork.Commit);
}
}
[Test]
public void Can_Instantiate_Repository()
{
@@ -28,10 +65,11 @@ namespace Umbraco.Tests.Persistence.Repositories
var unitOfWork = provider.GetUnitOfWork();
// Act
var repository = new ServerRegistrationRepository(unitOfWork);
// Assert
Assert.That(repository, Is.Not.Null);
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
// Assert
Assert.That(repository, Is.Not.Null);
}
}
[Test]
@@ -40,15 +78,18 @@ namespace Umbraco.Tests.Persistence.Repositories
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
var repository = new ServerRegistrationRepository(unitOfWork);
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
// Act
var server = repository.Get(1);
// Act
var server = repository.Get(1);
// Assert
Assert.That(server, Is.Not.Null);
Assert.That(server.HasIdentity, Is.True);
Assert.That(server.ServerAddress, Is.EqualTo("http://localhost"));
}
// Assert
Assert.That(server, Is.Not.Null);
Assert.That(server.HasIdentity, Is.True);
Assert.That(server.ServerAddress, Is.EqualTo("http://localhost"));
}
[Test]
@@ -57,13 +98,15 @@ namespace Umbraco.Tests.Persistence.Repositories
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
var repository = new ServerRegistrationRepository(unitOfWork);
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
// Act
var servers = repository.GetAll();
// Act
var servers = repository.GetAll();
// Assert
Assert.That(servers.Count(), Is.EqualTo(3));
// Assert
Assert.That(servers.Count(), Is.EqualTo(3));
}
}
[Test]
@@ -72,14 +115,15 @@ namespace Umbraco.Tests.Persistence.Repositories
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
var repository = new ServerRegistrationRepository(unitOfWork);
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
// Act
var query = Query<ServerRegistration>.Builder.Where(x => x.ComputerName.ToUpper() == "COMPUTER3");
var result = repository.GetByQuery(query);
// Act
var query = Query<ServerRegistration>.Builder.Where(x => x.IsActive);
var result = repository.GetByQuery(query);
// Assert
Assert.AreEqual(1, result.Count());
// Assert
Assert.AreEqual(1, result.Count());
}
}
[Test]
@@ -88,14 +132,15 @@ namespace Umbraco.Tests.Persistence.Repositories
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
var repository = new ServerRegistrationRepository(unitOfWork);
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
// Act
var query = Query<ServerRegistration>.Builder.Where(x => x.ServerAddress.StartsWith("http://"));
int count = repository.Count(query);
// Act
var query = Query<ServerRegistration>.Builder.Where(x => x.ServerAddress.StartsWith("http://"));
int count = repository.Count(query);
// Assert
Assert.That(count, Is.EqualTo(2));
// Assert
Assert.That(count, Is.EqualTo(2));
}
}
[Test]
@@ -104,16 +149,17 @@ namespace Umbraco.Tests.Persistence.Repositories
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
var repository = new ServerRegistrationRepository(unitOfWork);
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
// Act
var server = new ServerRegistration("http://shazwazza.com", "COMPUTER4", DateTime.Now);
repository.AddOrUpdate(server);
unitOfWork.Commit();
// Act
var server = new ServerRegistration("http://shazwazza.com", DateTime.Now);
repository.AddOrUpdate(server);
unitOfWork.Commit();
// Assert
Assert.That(server.HasIdentity, Is.True);
Assert.That(server.Id, Is.EqualTo(4));//With 3 existing entries the Id should be 4
// Assert
Assert.That(server.HasIdentity, Is.True);
Assert.That(server.Id, Is.EqualTo(4));//With 3 existing entries the Id should be 4
}
}
[Test]
@@ -122,22 +168,23 @@ namespace Umbraco.Tests.Persistence.Repositories
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
var repository = new ServerRegistrationRepository(unitOfWork);
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
// Act
var server = repository.Get(2);
server.ServerAddress = "https://umbraco.com";
server.IsActive = true;
// Act
var server = repository.Get(2);
server.ServerAddress = "https://umbraco.com";
server.IsActive = true;
repository.AddOrUpdate(server);
unitOfWork.Commit();
repository.AddOrUpdate(server);
unitOfWork.Commit();
var serverUpdated = repository.Get(2);
var serverUpdated = repository.Get(2);
// Assert
Assert.That(serverUpdated, Is.Not.Null);
Assert.That(serverUpdated.ServerAddress, Is.EqualTo("https://umbraco.com"));
Assert.That(serverUpdated.IsActive, Is.EqualTo(true));
// Assert
Assert.That(serverUpdated, Is.Not.Null);
Assert.That(serverUpdated.ServerAddress, Is.EqualTo("https://umbraco.com"));
Assert.That(serverUpdated.IsActive, Is.EqualTo(true));
}
}
[Test]
@@ -146,18 +193,19 @@ namespace Umbraco.Tests.Persistence.Repositories
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
var repository = new ServerRegistrationRepository(unitOfWork);
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
// Act
var server = repository.Get(3);
Assert.IsNotNull(server);
repository.Delete(server);
unitOfWork.Commit();
// Act
var server = repository.Get(3);
Assert.IsNotNull(server);
repository.Delete(server);
unitOfWork.Commit();
var exists = repository.Exists(3);
var exists = repository.Exists(3);
// Assert
Assert.That(exists, Is.False);
// Assert
Assert.That(exists, Is.False);
}
}
[Test]
@@ -166,15 +214,16 @@ namespace Umbraco.Tests.Persistence.Repositories
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
var repository = new ServerRegistrationRepository(unitOfWork);
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
// Act
var exists = repository.Exists(3);
var doesntExist = repository.Exists(10);
// Act
var exists = repository.Exists(3);
var doesntExist = repository.Exists(10);
// Assert
Assert.That(exists, Is.True);
Assert.That(doesntExist, Is.False);
// Assert
Assert.That(exists, Is.True);
Assert.That(doesntExist, Is.False);
}
}
[TearDown]
@@ -185,13 +234,13 @@ namespace Umbraco.Tests.Persistence.Repositories
public void CreateTestData()
{
var provider = new PetaPocoUnitOfWorkProvider();
using(var unitOfWork = provider.GetUnitOfWork())
var provider = new PetaPocoUnitOfWorkProvider();
using (var unitOfWork = provider.GetUnitOfWork())
using (var repository = new ServerRegistrationRepository(unitOfWork))
{
repository.AddOrUpdate(new ServerRegistration("http://localhost", DateTime.Now) {IsActive = true});
repository.AddOrUpdate(new ServerRegistration("http://www.mydomain.com", DateTime.Now));
repository.AddOrUpdate(new ServerRegistration("https://www.another.domain.com", DateTime.Now));
repository.AddOrUpdate(new ServerRegistration("http://localhost", "COMPUTER1", DateTime.Now) { IsActive = true });
repository.AddOrUpdate(new ServerRegistration("http://www.mydomain.com", "COMPUTER2", DateTime.Now));
repository.AddOrUpdate(new ServerRegistration("https://www.another.domain.com", "Computer3", DateTime.Now));
unitOfWork.Commit();
}