Compiler error due to lack of constructor

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'm getting the following compile error in class DisciplinaBLO :

  

Error 1 'Core.BLL.Base.GenericaBLO' does not contain a constructor that takes 0 arguments DisciplinaBLO.cs 12 18 Core

    
asked by anonymous 23.05.2017 / 15:17

1 answer

4

The solution is simple, add this in class:

public GenericaBLO() {}

As DisciplinaBLO inherits from GenericaBLO , it needs to call a default constructor of the base class and it does not exist, need to create one. The default constructor is only created automatically if no other constructor is created.

Understand What is a builder for? .

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

I do not like those names either, but it should just be me.

I took advantage of and improved the style and solved 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 you 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.

I also think these methods are not being useful by returning a bool , I just do not talk to get this bool because it is probably right, the error is that it never returns a fake, and should.

Enjoy 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 catch exceptions do not make the least sense and even damage the code. I even think the exception is useful (since the API does not have a better way ), but if you did:

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 placed it on GitHub for future reference .

I hate this code repetition, but that's something subjective of mine.

Read and search more about exceptions:

23.05.2017 / 16:13