Created
August 4, 2016 12:23
-
-
Save kwestground/2d34d3ea36acc2dc5c8934f751bcf070 to your computer and use it in GitHub Desktop.
Syncronize entities helper class for nopCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using Nop.Core; | |
using Nop.Core.Data; | |
using Nop.Core.Domain.Common; | |
using Nop.Core.Infrastructure; | |
using Nop.Services.Common; | |
namespace Nop.Plugin.MyPlugin | |
{ | |
public static class SyncHelper | |
{ | |
private static IRepository<GenericAttribute> GenericAttributeRepository | |
=> EngineContext.Current.Resolve<IRepository<GenericAttribute>>(); | |
private static IGenericAttributeService GenericAttributeService | |
=> EngineContext.Current.Resolve<IGenericAttributeService>(); | |
public static T GetEntityBySyncId<T>(string syncName, string syncKey) where T : BaseEntity | |
{ | |
syncName = $"syncmap-{syncName}"; | |
var entityRepository = EngineContext.Current.Resolve<IRepository<T>>(); | |
var entityName = typeof (T).Name; | |
var genericAttribute = (from ga in GenericAttributeRepository.TableNoTracking | |
where ga.Key == syncName && ga.KeyGroup == entityName && ga.Value == syncKey | |
select ga).FirstOrDefault(); | |
if (genericAttribute == null) return null; | |
var entity = entityRepository.GetById(genericAttribute.EntityId); | |
return entity; | |
} | |
public static ICollection<GenericAttribute> GetSyncMappings<T>(string syncName) where T : BaseEntity | |
{ | |
var entityName = typeof(T).Name; | |
var genericAttributes = (from ga in GenericAttributeRepository.TableNoTracking | |
where ga.Key == syncName && ga.KeyGroup == entityName | |
select ga).ToList(); | |
return genericAttributes; | |
} | |
public static void AddSyncMap(this BaseEntity entity, string syncName, string syncId) | |
{ | |
syncName = $"syncmap-{syncName}"; | |
GenericAttributeService.SaveAttribute(entity, syncName, syncId); | |
} | |
public static T GetEntity<T>(this GenericAttribute genericAttribute) where T : BaseEntity | |
{ | |
var repository = EngineContext.Current.Resolve<IRepository<T>>(); | |
var baseEntity = repository.GetById(genericAttribute.EntityId); | |
return baseEntity; | |
} | |
public static void DeleteSyncMapping(this GenericAttribute genericAttribute) | |
{ | |
GenericAttributeService.DeleteAttribute(genericAttribute); | |
} | |
public static GenericAttribute GetSyncMap<T>(this T entity, string syncName) where T : BaseEntity | |
{ | |
syncName = $"syncmap-{syncName}"; | |
var entityName = typeof(T).Name; | |
var genericAttribute = (from ga in GenericAttributeRepository.TableNoTracking | |
where ga.Key == syncName && ga.KeyGroup == entityName && ga.EntityId == entity.Id | |
select ga).FirstOrDefault(); | |
return genericAttribute; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment