1
My intention is to recover mine Entity and the Repository by means of a string passed to the method.
With that I’ll make sure he can execute the GetAll()
of my generic repository independent of the class that was passed, but I need to instantiate it.
So far the code is like this, but gives a null reference message on the line:
var entidades = (repository as RepositoryBase<EntityBase>).GetByFilter("1=1 AND " + filter);
Method:
public static MvcHtmlString SearchComboBox(string nameIdKey, object value, string controller, string tableName, string descriptionField, string filter = "1=1", bool obrigatorio = false)
{
string options = string.Empty;
Type type = AppDomain.CurrentDomain.GetAssemblies()
.ToList()
.FirstOrDefault(x => x.FullName.IndexOf("LaioMVC.Core") > -1)
.GetTypes()
.Where(x => x.Name.ToUpper() == tableName.ToUpper())
.Single();
EntityBase entity = null;
if (type != null)
{
entity = (EntityBase)Activator.CreateInstance(type);
Type typeRep = AppDomain.CurrentDomain.GetAssemblies()
.ToList()
.FirstOrDefault(x => x.FullName.IndexOf("LaioMVC.Data") > -1)
.GetTypes()
.Where(x => x.Name == tableName + "Repository")
.Single();
if (typeRep != null)
{
var repository = Activator.CreateInstance(typeRep);
var entidades = (repository as RepositoryBase<EntityBase>).GetByFilter("1=1 AND " + filter);
for (int i = 0; i < entidades.Count(); i++)
{
Type campoKey = entity.GetType();
PropertyInfo info = campoKey.GetProperty(nameIdKey);
Type campoDescricao = entity.GetType();
PropertyInfo infoDesc = campoKey.GetProperty(nameIdKey);
options += Environment.NewLine;
options += string.Format("<option value='{0}'> {1} </option>", info.ToString(), infoDesc.ToString());
}
}
}
return MvcHtmlString.Create(options);
}
Repository:
using Dapper;
using LaioMVC.Core;
using LaioMVC.Core.Attributes;
using LaioMVC.Core.Models;
using LaioMVC.Core.ViewModels;
using LaioMVC.Data.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace LaioMVC.Data.Repositorios
{
public abstract class RepositoryBase<T> : IRepositoryBase<T>
where T : EntityBase
{
public List<T> GetAll()
{
var query = $"select * from {typeof(T).Name} ";
using (var connection = new SqlConnection(Constants.ConnectionString))
{
connection.Open();
return connection.Query<T>(query).ToList();
}
}
}
}
Entitybase:
namespace LaioMVC.Core.Models
{
public class EntityBase
{
public int Id { get; set; }
}
}
Recalling that all entities inherit from EntityBase
.
I wonder if someone could help me?
Sincerity, just a hint, you are complicating an item that can be simply send values to this method.
– novic
It is because I will pass the values to the method through my View using Razor. So I can use to assemble the HTML code of any entity, so it would have to be something generic
– Victor Laio
You’re putting a responsibility on your
View
unnecessary, because, who sends information is hisController
... I don’t understand what you want to ride, apparently it’s aSelect
that if it is already exists the classes to do this. This generates an encapsulation and should pay attention to it.– novic
My intention is to ride a
Select
with the data of the entity I search in the database, passing to my custom component (Searchcombobox()) through the parameters (strings) which data in the database I will have to search for. Then with the parameters I want to instantiate the class passed through the string and return the HTML of the combobox with theoptions
mounted.– Victor Laio
Yes, but yours is
SearchComboBox()
has bank code and could not, it should only have data! Well I think so, maybe for you it is different.– novic
Anyway it would only migrate the code to some service not? But still it would need the logic to work :/
– Victor Laio