Пример #1
0
        public void RegisterIdentitiesFromAssembly(Assembly assembly)
        {
            var sb = new StringBuilder();

            foreach (var ic in assembly.GetTypes().Where(x => typeof(IIdentity).IsAssignableFrom(x) && !x.IsAbstract && x.IsClass))
            {
                var tag = EventStoreIdentity.GetTagForIdentityClass(ic);

                {
                    var ctor = ic.Constructor(new Type[] { typeof(long) });
                    if (ctor == null)
                    {
                        var message = String.Format("Identity {0} must have the constructor {1}(long id)\n", ic.FullName, ic.Name);
                        Logger.Error(message);
                        sb.AppendFormat(message);
                        continue; //move to the next type, or everythign will crash.
                    }
                    var activator = FastReflectionHelper.GetActivator(ctor);

                    _longBasedFactories[tag] = (id) => (IIdentity)activator(new object[] { id });
                }
            }

            var errors = sb.ToString();

            if (!String.IsNullOrWhiteSpace(errors))
            {
                throw new JarvisFrameworkEngineException("Found identities with errors:\n" + errors);
            }
        }
Пример #2
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var nominalType = typeof(T);

            FastReflectionHelper.ObjectActivator activator;
            if (!_activators.TryGetValue(nominalType, out activator))
            {
                var ctor = nominalType.Constructor(new Type[] { typeof(string) });
                activator = FastReflectionHelper.GetActivator(ctor);
                _activators[nominalType] = activator;
            }
            return(activator(new object[] { (string)value }));
        }
Пример #3
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.String)
            {
                FastReflectionHelper.ObjectActivator activator;
                if (!_activators.TryGetValue(objectType, out activator))
                {
                    var ctor = objectType.Constructor(new Type[] { typeof(string) });
                    activator = FastReflectionHelper.GetActivator(ctor);
                    _activators[objectType] = activator;
                }

                var typedId = activator(new object[] { Convert.ToString((object)reader.Value) });
                return(typedId);
            }
            return(null);
        }
Пример #4
0
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            if (context.Reader.CurrentBsonType == BsonType.Null)
            {
                context.Reader.ReadNull();
                return(null);
            }

            var id = context.Reader.ReadString();

            FastReflectionHelper.ObjectActivator activator;
            if (!_activators.TryGetValue(args.NominalType, out activator))
            {
                var ctor = args.NominalType.Constructor(new Type[] { typeof(string) });
                activator = FastReflectionHelper.GetActivator(ctor);
                _activators[args.NominalType] = activator;
            }
            return(activator(new object[] { id }));
        }