Exemplo n.º 1
0
        public ModuleModel(String model, String controller, String?area)
        {
            ModelShortName = Regex.Split(model, "(?=[A-Z])").Last();
            ModelVarName   = ModelShortName.ToLower();
            Models         = model.Pluralize(false);
            Model          = model;

            View = $"{Model}View";

            ServiceTests = $"{Model}ServiceTests";
            IService     = $"I{Model}Service";
            Service      = $"{Model}Service";

            ValidatorTests = $"{Model}ValidatorTests";
            IValidator     = $"I{Model}Validator";
            Validator      = $"{Model}Validator";

            ControllerTestsNamespace = $"MvcTemplate.Controllers.{(!String.IsNullOrWhiteSpace(area) ? $"{area}." : "")}Tests";
            ControllerNamespace      = $"MvcTemplate.Controllers{(!String.IsNullOrWhiteSpace(area) ? $".{area}" : "")}";
            ControllerTests          = $"{controller}ControllerTests";
            Controller = $"{controller}Controller";

            Area = area;

            Type modelType = typeof(BaseModel).Assembly.GetType($"MvcTemplate.Objects.{Model}") ?? typeof(BaseModel);
            Type viewType  = typeof(BaseView).Assembly.GetType($"MvcTemplate.Objects.{View}") ?? typeof(BaseModel);

            PropertyInfo[] modelProperties = modelType.GetProperties();

            AllModelProperties = modelProperties.Where(property => property.PropertyType.Namespace == "System").ToArray();
            ViewProperties     = viewType.GetProperties().Where(property => property.DeclaringType?.Name == View).ToArray();
            ModelProperties    = AllModelProperties.Where(property => property.DeclaringType?.Name == Model).ToArray();
            AllViewProperties  = viewType.GetProperties();
            Indexes            = ModelProperties
                                 .Where(property =>
                                        ViewProperties.Any(prop => prop.Name == property.Name) &&
                                        property.GetCustomAttribute <IndexAttribute>()?.IsUnique == true)
                                 .OrderByDescending(property => property.Name.Length)
                                 .ThenByDescending(property => property.Name)
                                 .ToArray();
            Relations = AllViewProperties
                        .ToDictionary(
                property => property,
                property => modelProperties.SingleOrDefault(relation =>
                                                            property.Name.EndsWith("Id") &&
                                                            relation.PropertyType.Assembly == modelType.Assembly &&
                                                            relation.Name == property.Name[..^ 2])?.PropertyType.Name);
        }