Build error due to lack of constructor

Asked

Viewed 116 times

2

I have the following classes:

class Disciplina:

public class Disciplina
{
    public int Id { get; set; }
    public string Nome { get; set; }
}

interface IGenericaDAO:

public interface IGenericaDAO<T>
{
    bool Add(T e);
    bool Update(T e);
    bool Delete(T e);
    List<T> GetAll();
    T Get(int id);
}

interface IDisciplinaDAO:

public interface IDisciplinaDAO : IGenericaDAO<Disciplina>
{
}

class GenericaDAO:

public class GenericaDAO<T> : IGenericaDAO<T> where T : class
{
    internal ApplicationDbContext Context  { get; set; }

    protected DbSet<T> DbSet { get; set; }

    public GenericaDAO()
    {
        Context = new ApplicationDbContext();
        DbSet = Context.Set<T>();
    }

    public bool Add(T e)
    {
        try
        {
            Context.Entry(e).State = EntityState.Added;
            Context.SaveChanges();
            return true;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public bool Update(T e)
    {
        try
        {
            Context.Entry(e).State = EntityState.Modified;
            Context.SaveChanges();
            return true;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public bool Delete(T e)
    {
        try
        {
            Context.Entry(e).State = EntityState.Deleted;
            Context.SaveChanges();
            return true;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public List<T> GetAll()
    {
        return DbSet.ToList();
    }

    public T Get(int id)
    {
        return DbSet.Find(id);
    }
}

class DisciplinaDAO:

public class DisciplinaDAO : GenericaDAO<Disciplina>, IDisciplinaDAO
{
}

interface IGenericaBLO:

public interface IGenericaBLO<T>
{
    bool Add(T e);
    bool Update(T e);
    bool Delete(T e);
    List<T> GetAll();
    T Get(int id);
}

public interface IGenericaBLO<T>
{
    bool Add(T e);
    bool Update(T e);
    bool Delete(T e);
    List<T> GetAll();
    T Get(int id);
}

interface IDisciplinaBLO:

public interface IDisciplinaBLO : IGenericaBLO<Disciplina>
{
}

class GenericaBLO:

public class GenericaBLO<T> : IGenericaBLO<T> where T : class
{
    private IGenericaDAO<T> dao;

    public GenericaBLO(IGenericaDAO<T> _dao)
    {
        dao = _dao;
    }

    public bool Add(T e)
    {
        bool resultado = dao.Add(e);
        return resultado;
    }

    public bool Update(T e)
    {
        bool resultado = dao.Update(e);
        return resultado;
    }

    public bool Delete(T e)
    {
        bool resultado = dao.Delete(e);
        return resultado;
    }

    public List<T> GetAll()
    {
        return dao.GetAll();
    }

    public T Get(int id)
    {
        return dao.Get(id);
    }
}

class DisciplinaBLO:

public class DisciplinaBLO : GenericaBLO<Disciplina>, IDisciplinaBLO
{}

I am getting the following build error in the class DisciplinaBLO:

Error 1 'Core.BLL.Base.Genericablo' does not contain a constructor that takes 0 Arguments Disciplinablo.Cs 12 18 Core

  • There are many things that seem to be repeated there, edit the question and remove if it really is.

  • Hello, it doesn’t seem to me to have something repeated no. I put all the classes and interfaces to help find the error. I already found, missed declaring the constructor of the Genericablo class, because once you create a constructor with parameters, the default constructor (no parameters) is not created automatically.

  • There’s a lot of bad stuff in this code. Where are you making that mistake? In a call or in a class you’re inheriting GenericaBLO, right? Show us this.

  • I’m getting the following build error in the Disciplinablo class. Helps me, which is bad in code?

  • @Leandroduarte And what’s in DisciplinaBLO? Nothing? Then why is there such a class?

  • Because it will have an implementation later. I am specializing for reuse.

Show 1 more comment

1 answer

4


The solution is simple, add this in the class:

public GenericaBLO() {}

Like DisciplinaBLO inherits from GenericaBLO, she needs to call a standard builder base class and it does not exist, you need to create one. The default constructor is only created automatically if no other constructor is created.

Understand What good is a builder?.

When you have an implementation you may have a better solution.

I don’t like those names either, but it’s probably just me.

I took advantage and improve the style and solve the main problem of this code:

public class GenericaBLO<T> : IGenericaBLO<T> where T : class {
    private IGenericaDAO<T> dao;

    public GenericaBLO() {}
    public GenericaBLO(IGenericaDAO<T> dao) { //se usar C#7 pode fazer igual aos métodos abaixo
        this.dao = dao;
    }

    public bool Add(T e) => dao.Add(e);
    public bool Update(T e) => dao.Update(e);
    public bool Delete(T e) => dao.Delete(e);
    public List<T> GetAll() => dao.GetAll();
    public T Get(int id) => dao.Get(id);
}

To tell the truth I hate this kind of architecture, creating a class just to delegate to another is almost always a mistake, but this is a broader subject.

And I also think that these methods are not being useful by returning a bool, I just don’t talk to take this bool because he’s probably right, the mistake is that it never returns a false one, and should.

Take advantage and solve the problem in the other class:

public class GenericaDAO<T> : IGenericaDAO<T> where T : class {
    internal ApplicationDbContext Context  { get; set; }
    protected DbSet<T> DbSet { get; set; }

    public GenericaDAO() {
        Context = new ApplicationDbContext();
        DbSet = Context.Set<T>();
    }

    public bool Add(T e) {
        Context.Entry(e).State = EntityState.Added;
        Context.SaveChanges();
        return true;
    }

    public bool Update(T e) {
        Context.Entry(e).State = EntityState.Modified;
        Context.SaveChanges();
        return true;
    }

    public bool Delete(T e) {
        Context.Entry(e).State = EntityState.Deleted;
        Context.SaveChanges();
        return true;
    }

    public List<T> GetAll() => DbSet.ToList();

    public T Get(int id) => DbSet.Find(id);
}

These exception shots don’t make any sense and even harm the code. I even think the exception is useful (since the API doesn’t have a better way), but if I did this:

public bool Add(T e) {
    try {
        Context.Entry(e).State = EntityState.Added;
        Context.SaveChanges();
        return true;
    } catch (ExceptionMaisEspecica ex) { //isto é importante, não capture Exception
        return false;
    }
}

I would do so:

public class GenericaDAO<T> : IGenericaDAO<T> where T : class {
    internal ApplicationDbContext Context  { get; set; }
    protected DbSet<T> DbSet { get; set; }

    public GenericaDAO() {
        Context = new ApplicationDbContext();
        DbSet = Context.Set<T>();
    }

    public bool Add(T e) => ChangeState(e, EntityState.Added);

    public bool Update(T e) => ChangeState(e, EntityState.Modified);

    public bool Delete(T e) => ChangeState(e, EntityState.Deleted);

    public List<T> GetAll() => DbSet.ToList();

    public T Get(int id) => DbSet.Find(id);

    private bool ChangState<T>(T e, EntityStat state) {
        Context.Entry(e).State = state;
        Context.SaveChanges();
        return true;
    }
}

I put in the Github for future reference.

I hate this code repetition, but this is something subjective of mine.

Read and research more about exceptions:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.