Search code examples
c#genericsbase-class-library

Base class for Generic classes?


We are using EF6 in our MVC 4 application, so we created an abstract class for Business Objects
So, We have a generic abstract class as the following :

public abstract class Repository<TEntity, TIdentifier> : IRepository<TEntity, TIdentifier> where TEntity : class { ... }

in our application's BusinessLayer we have several classes (one class per an Entity) that implement above class.
e.g: as the following

    //Authorization is an entity here
    public class AuthorizationBusinessObject : MD.EntityFramework.Repository.Repository<Authorization, int>
    {
       ...
    }

We need to have a BaseBusinessObject for above classes.
Now, my question is here; how we can have a BaseBusinessObject class for above classes?

Edit :

Well, Repository<> is a base class for BusinessObjects, isn't it?
Actually, We need a non generic base class for BusinessObjects
Why?
We are using web API services in our MVC 4 application. in each web api controller we have to have the same actions as the following, only BusinessObject is different. So, if we could have a base business object, we could implement the following Actions in base web api controller.

// Setting is an Entity
private readonly SettingBusinessObject _settingBusinessObject;

public SettingController()
{
    _settingBusinessObject = new SettingBusinessObject(Entities);
}

public Setting Get(int id)
{
    return _settingBusinessObject.SelectBy(id);
}
public List<Setting> Get(IEnumerable<int> ids)
{
    return _settingBusinessObject.SelectAll(ids).ToList();
}

public Setting Put(Setting setting)
{
    return _settingBusinessObject.Update(setting);
}
public List<Setting> Put(List<Setting> entities)
{
    List<Setting> userList = new List<Setting>();
    userList.AddRange(entities.Select(_settingBusinessObject.Update));
    return userList;
}

public Setting Post(Setting entity)
{
    return _settingBusinessObject.Insert(entity);
}
public List<Setting> Post(List<Setting> entities)
{
    List<Setting> userList = new List<Setting>();
    userList.AddRange(entities.Select(_settingBusinessObject.Insert));
    return userList;
}

public void Delete(int id)
{
    _settingBusinessObject.Delete(id);
}
public void Delete(List<int> ids)
{
    _settingBusinessObject.Delete(ids);
}

Solution

  • Why can't the BaseBusinessObject class also be generic?

    public abstract class BaseBusinessObject<TEntity, TIdentifier> : Repository<TEntity, TIdentifier>
    {
        ... base stuff here
    }
    
    public class AuthorizationBusinessObject : BaseBusinessObject<Authorization, int>
    {
    }
    

    It's worth noting, though, that layering lots of per-entity stuff on top of EF can add a ton of boilerplate with little real benefit. It might be wortwhile considering whether just using EF directly will get you what you need or whether your business object classes should be organized around logical sets of operations rather than around individual entities.