Exemplo n.º 1
0
        public bool ActivateFor(IRow row)
        {
            attr = row.GetType().GetCustomAttribute <LocalizationRowAttribute>();
            if (attr == null)
            {
                return(false);
            }

            localRowType = attr.LocalizationRow;
            if (!typeof(ILocalizationRow).IsAssignableFrom(localRowType))
            {
                throw new ArgumentException(string.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't implement ILocalizationRow interface!",
                                                row.GetType().FullName, localRowType.FullName));
            }

            if (!typeof(IIdRow).IsAssignableFrom(localRowType))
            {
                throw new ArgumentException(string.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't implement IIdRow interface!",
                                                row.GetType().FullName, localRowType.FullName));
            }

            if (!(row is IIdRow))
            {
                throw new ArgumentException(string.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but row type itself doesn't implement IIdRow interface!",
                                                row.GetType().FullName));
            }

            var rowType = row.GetType();

            rowFactory      = () => (IIdRow)Activator.CreateInstance(rowType);
            localRowFactory = () => (ILocalizationRow)Activator.CreateInstance(localRowType);

            var localRow = localRowFactory();

            localRowInstance = localRow;

            rowPrefixLength = PrefixHelper.DeterminePrefixLength(row.EnumerateTableFields(),
                                                                 x => x.Name);
            localRowPrefixLength = PrefixHelper.DeterminePrefixLength(localRow.EnumerateTableFields(),
                                                                      x => x.Name);
            localRowIdField = localRow.IdField;
            cultureIdField  = localRow.CultureIdField;

            var foreignKeyFieldName = attr.MappedIdField ?? row.IdField.PropertyName;

            foreignKeyField = localRow.FindFieldByPropertyName(foreignKeyFieldName) ??
                              localRow.FindField(foreignKeyFieldName);

            if (foreignKeyField is null)
            {
                throw new ArgumentException(string.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't have a field with name '{2}'!",
                                                row.GetType().FullName, localRowType.FullName, foreignKeyFieldName));
            }

            var dictionaryType = typeof(Dictionary <,>).MakeGenericType(typeof(string), row.GetType());

            dictionaryFactory = () => (IDictionary)Activator.CreateInstance(dictionaryType);

            foreignKeyCriteria = new Criteria(foreignKeyField.PropertyName ?? foreignKeyField.Name);
            return(true);
        }