using System.Collections.Generic;
using NRules.Integration.Autofac;
public static void Main()
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterRuleRepository(x => x.AssemblyOf(typeof(Program)));
containerBuilder.RegisterSessionFactory();
containerBuilder.RegisterSession();
containerBuilder.RegisterType<UserRepository>().As<IUserRepository>();
containerBuilder.RegisterType<LeadRepository>().As<ILeadRepository>();
var container = containerBuilder.Build(new ContainerBuildOptions());
var rawData = new List<RawData>
new RawData { Sid1 = "userId1" },
new RawData { Sid1 = "leadId1" },
new RawData { Sid1 = "nothing" }
foreach (var data in rawData)
var session = container.Resolve<ISession>();
var result = new CanonicalEvent();
Console.WriteLine("Sid1: '{0}', UserId: '{1}'", data.Sid1, result.UserId);
public string Sid1 { get; set; }
public class CanonicalEvent
public string UserId { get; set; }
public class MapSid1ToUserId : Rule
public override void Define()
IUserRepository userRepository = null;
ILeadRepository leadRepository = null;
RawData rawData = default;
CanonicalEvent canonicalEvent = default;
.Resolve(() => userRepository)
.Resolve(() => leadRepository);
.Match<RawData>(() => rawData, data => data != null && data.Sid1 != null)
.Match<CanonicalEvent>(() => canonicalEvent, canonicalEvent => canonicalEvent != null);
.Do(ctx => AssignUserId(rawData, canonicalEvent, userRepository, leadRepository));
private void AssignUserId(RawData rawData, CanonicalEvent canonicalEvent, IUserRepository userRepository, ILeadRepository leadRepository)
if (userRepository.UserIdExists(rawData.Sid1))
canonicalEvent.UserId = rawData.Sid1;
var userId = leadRepository.GetUserIdFromLeadId(rawData.Sid1);
canonicalEvent.UserId = userId;
canonicalEvent.UserId = null;
public interface IUserRepository
bool UserIdExists(string userId);
public class UserRepository : IUserRepository
public bool UserIdExists(string userId)
return userId == "userId1";
public interface ILeadRepository
string GetUserIdFromLeadId(string leadId);
public class LeadRepository : ILeadRepository
public string GetUserIdFromLeadId(string leadId)
if (leadId == "leadId1") return "userId2";