Exemplo n.º 1
0
        public static ExecutionTrace <TReturn> Apply <TReturn, TSubject>(TSubject subject, Assembly policyLocation = null,
                                                                         IEnumerable <Type> policies = null)
        {
            if (policies == null)
            {
                policies = Enumerable.Empty <Type>();
            }

            Type type = (typeof(TSubject));
            IEnumerable <Assembly> policyLocations = RetrievePolicyLocations(ref policyLocation, policies, type);

            Exceptions = new List <ExceptionInformation>();

            var executionTrace = new ExecutionTrace <TReturn>(policyLocation);

            foreach (
                DotNetRulesContext mon in
                policyLocations.SelectMany(location => location.GetTypesWithPolicyAttribute(policies.Any(), type)
                                           .Select(item => new DotNetRulesContext(item))
                                           .Where(
                                               _ =>
                                               (!policies.Any()) ||
                                               policies.Any(type1 => type1 == _.CurrentPolicy))))
            {
                mon.Establish(subject);
                if (mon.Given() || mon.Or())
                {
                    executionTrace.Called++;
                    executionTrace.By.Enqueue(mon.CurrentPolicy);

                    MonitorThen(mon);
                }
                mon.Finally();
                executionTrace.ReturnType = mon.Return <TReturn>();
            }

            return(executionTrace);
        }
Exemplo n.º 2
0
        public static ExecutionTrace <TReturn> Apply <TReturn>(Assembly policyLocation, IEnumerable <Type> policies,
                                                               params dynamic[] values)
        {
            if (policies == null)
            {
                policies = Enumerable.Empty <Type>();
            }
            Type[] types = values.Select(_ => (Type)_.GetType()).ToArray();
            if (policyLocation == null)
            {
                policyLocation = types.First().Assembly;
            }
            Exceptions = new List <ExceptionInformation>();

            var target = new ExecutionTrace <TReturn>(policyLocation);

            foreach (
                DotNetRulesContext mon in policyLocation.GetTypesWithPolicyAttribute(policies.Any(), types.ToArray())
                .Select(item => new DotNetRulesContext(item))
                .Where(_ => !policies.Any() || policies.Any(type1 => type1 == _.CurrentPolicy)))
            {
                mon.EstablishMore(values);
                if (mon.Given() || mon.Or())
                {
                    target.Called++;
                    target.By.Enqueue(mon.CurrentPolicy);

                    MonitorThen(mon);
                }
                mon.Finally();
                for (int i = 0; i < values.Count(); i++)
                {
                    dynamic value = values[i];
                    target.ReturnType = mon.Return <TReturn>();
                }
            }
            return(target);
        }
Exemplo n.º 3
0
 internal void Initalize <T>(ExecutionTrace <T> executionTrace)
 {
     Called = executionTrace.Called;
     By     = executionTrace.By;
 }