I understand the concept of how to create generic class in C# quit well but what I'm trying to archive is a little bit different from what I've done before.
I use to create Service Management classes and write and all the methods I'll be needing in the controller layer using dependency Injections but i figured that am doing the same thing over and over for all my entities so I wanted to leverage the concept of Generic class which am already using as my Repository in the service layer as well so I'll cut the pain of rewriting the same thing over and over.
So this is what I've done so far, I went ahead and create the a concrete Generic Class
/// <summary>
/// Base Service Generic Class
/// </summary>
/// <typeparam name="TEntity"> Domain Entity</typeparam>
/// <typeparam name="TDataObject"> Data Object</typeparam>
public class BaseServices<TEntity, TDataObject> where TEntity : class, IDataObject, new()
{
public FMDbContext _context;
public IFactoryInit _factory;
public IUnitOfWork _uow;
public BaseServices(IUnitOfWork uow, IFactoryInit factory)
{
_uow = uow;
_factory = factory;
}
private Repository<TEntity> _repository { get; }
private ServiceFactory _serviceFactory { get; }
public Repository<TEntity> Repository
{
get { return _repository ?? new Repository<TEntity>(_context); }
}
public ServiceFactory SFactory
{
get { return _serviceFactory ?? new ServiceFactory(); }
}
public class ServiceFactory : BaseFactory<TEntity, TDataObject>
{
}
public TDataObject GetSingle(Guid id)
{
try
{
TEntity _entity = Repository.GetById(id);
var _obj = SFactory.SingleMappingEntityToObject(_entity);
return _obj;
}
catch (Exception ex)
{
throw ex;
}
}
public IEnumerable<TDataObject> GetMany()
{
try
{
var entities = Repository.Get(null, null, false);
var _objs = SFactory.ListMappingEntitiesToObjects(entities);
return _objs;
}
catch (Exception ex)
{
throw ex;
}
}
public Task<TDataObject> CreateAsync(TDataObject obj)
{
return Task.Run(() =>
{
try
{
var entity = SFactory.SingleMappingObjectToEntity(obj);
entity.Id = IdentityGenerator.NewSequentialGuid();
Repository.Insert(entity: entity);
_context.SaveChangesAsync();
var _resp = SFactory.SingleMappingEntityToObject(entity);
return _resp;
throw new InvalidOperationException("Internal Server Error!");
}
catch (Exception ex)
{
throw ex;
}
});
}}
As you've seen I brought in my IUnitOfWork
where am initialising all my repository inside.
And now this is how am calling it on my service
public class ProductService : BaseServices<Product, ProductObject>
{
public ProductService(IUnitOfWork uow, IFactoryInit factory) : base(uow, factory)
{
}
}
Then I went ahead to bring all the servies in one class so I can use .Net Core Dependency Injection to bind it once
public class ServiceInit : IServiceInit
{
private IFactoryInit _factory;
private IUnitOfWork _uow;
private ProductService _productService;
public ProductService ProductServices
{
get { return _productService ?? new ProductService(_uow, _factory); }
}
}
That's all I've done but I keep getting Null Reference Exception in my UnitOfWork initialisation and I even tried this
public IUnitOfWork UnitOfWork
{
get
{
if (_uow == null)
{
_uow = new UnitOfWork(_context);
}
return _uow;
}
}
Explicitly and am still get null reference
But this pattern is still working perfectly well, which is the pattern of creating services repeatedly for all my entity which am very tired of
{
public class VendorManagementServices : IVendorManagementServices
{
private IFactoryInit _factory;
private IUnitOfWork _uow;
public VendorManagementServices(IUnitOfWork uow, IFactoryInit factory)
{
_uow = uow;
_factory = factory;
}
}
So, I dont know what am missing in my BaseService
that keeps throwing
System.NullReferenceException: Object reference not set to an instance of an object.
Any Ideas guys?