/// <summary> /// Saves the <see cref="IAnonymousCustomer"/> /// </summary> /// <param name="anonymous"> /// The anonymous customer /// </param> /// <param name="raiseEvents"> /// TOptional boolean indicating whether or not to raise events /// </param> public void Save(IAnonymousCustomer anonymous, bool raiseEvents = true) { if (raiseEvents) { if (Saving.IsRaisedEventCancelled(new SaveEventArgs <IAnonymousCustomer>(anonymous), this)) { return; } } using (new WriteLock(Locker)) { var uow = UowProvider.GetUnitOfWork(); using (var repository = RepositoryFactory.CreateAnonymousCustomerRepository(uow)) { repository.AddOrUpdate(anonymous); uow.Commit(); } } if (raiseEvents) { Saved.RaiseEvent(new SaveEventArgs <IAnonymousCustomer>(anonymous), this); } }
/// <summary> /// The get anonymous customers created before a certain date. /// </summary> /// <param name="createdDate"> /// The created Date. /// </param> /// <returns> /// The collection of <see cref="IAnonymousCustomer"/> older than a certain number of days. /// </returns> /// <remarks> /// For maintenance routines /// </remarks> public IEnumerable <IAnonymousCustomer> GetAnonymousCustomersCreatedBefore(DateTime createdDate) { using (var repository = RepositoryFactory.CreateAnonymousCustomerRepository(UowProvider.GetUnitOfWork())) { var query = Query <IAnonymousCustomer> .Builder.Where(x => x.CreateDate <= createdDate); return(repository.GetByQuery(query)); } }
/// <summary> /// Gets an <see cref="ICustomer"/> or <see cref="IAnonymousCustomer"/> object by its 'UniqueId' /// </summary> /// <param name="entityKey">GUID key of either object to retrieve</param> /// <returns><see cref="ICustomerBase"/></returns> public ICustomerBase GetAnyByKey(Guid entityKey) { ICustomerBase customer; // try retrieving an anonymous customer first as in most situations this will be what is being queried using (var repository = _repositoryFactory.CreateAnonymousCustomerRepository(_uowProvider.GetUnitOfWork())) { customer = repository.Get(entityKey); } if (customer != null) { return(customer); } // try retrieving an existing customer using (var repository = _repositoryFactory.CreateCustomerRepository(_uowProvider.GetUnitOfWork())) { return(repository.Get(entityKey)); } }
/// <summary> /// The create anonymous customer with key. /// </summary> /// <returns> /// The <see cref="IAnonymousCustomer"/>. /// </returns> public IAnonymousCustomer CreateAnonymousCustomerWithKey() { var anonymous = new AnonymousCustomer(); if (Creating.IsRaisedEventCancelled(new Events.NewEventArgs <IAnonymousCustomer>(anonymous), this)) { anonymous.WasCancelled = true; return(anonymous); } using (new WriteLock(Locker)) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateAnonymousCustomerRepository(uow)) { repository.AddOrUpdate(anonymous); uow.Commit(); } } Created.RaiseEvent(new Events.NewEventArgs <IAnonymousCustomer>(anonymous), this); return(anonymous); }
/// <summary> /// Deletes the <see cref="IAnonymousCustomer"/> /// </summary> /// <param name="anonymous"> /// The anonymous customer /// </param> public void Delete(IAnonymousCustomer anonymous) { if (Deleting.IsRaisedEventCancelled(new DeleteEventArgs <IAnonymousCustomer>(anonymous), this)) { return; } using (new WriteLock(Locker)) { var uow = UowProvider.GetUnitOfWork(); using (var repository = RepositoryFactory.CreateAnonymousCustomerRepository(uow)) { repository.Delete(anonymous); uow.Commit(); } } Deleted.RaiseEvent(new DeleteEventArgs <IAnonymousCustomer>(anonymous), this); }
/// <summary> /// Crates an <see cref="IAnonymousCustomer"/> and saves it to the database /// </summary> /// <returns><see cref="IAnonymousCustomer"/></returns> public IAnonymousCustomer CreateAnonymousCustomerWithKey() { var anonymous = new AnonymousCustomer(); using (new WriteLock(Locker)) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateAnonymousCustomerRepository(uow)) { repository.AddOrUpdate(anonymous); uow.Commit(); } } return(anonymous); }
/// <summary> /// Deletes a collection of <see cref="IAnonymousCustomer"/> /// </summary> /// <param name="anonymouses"> /// The anonymous customers to be deleted /// </param> public void Delete(IEnumerable <IAnonymousCustomer> anonymouses) { var anonymousArray = anonymouses as IAnonymousCustomer[] ?? anonymouses.ToArray(); Deleting.RaiseEvent(new DeleteEventArgs <IAnonymousCustomer>(anonymousArray), this); using (new WriteLock(Locker)) { var uow = UowProvider.GetUnitOfWork(); using (var repository = RepositoryFactory.CreateAnonymousCustomerRepository(uow)) { foreach (var anonymous in anonymousArray) { repository.Delete(anonymous); } uow.Commit(); } } Deleted.RaiseEvent(new DeleteEventArgs <IAnonymousCustomer>(anonymousArray), this); }