title: How to setup a C# Generic Base Service Class and use in ASP.Net Core Application
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>So this is what I've done so far, I went ahead and create the a concrete Generic Class</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Base Service Generic Class
  /// &lt;/summary&gt;
  /// &lt;typeparam name="TEntity"&gt; Domain Entity&lt;/typeparam&gt;
  /// &lt;typeparam name="TDataObject"&gt; Data Object&lt;/typeparam&gt;
  public class BaseServices&lt;TEntity, TDataObject&gt; 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&lt;TEntity&gt; _repository { get; }
      private ServiceFactory _serviceFactory { get; }
  
  
      public Repository&lt;TEntity&gt; Repository
      {
          get { return _repository ?? new Repository&lt;TEntity&gt;(_context); }
      }
  
      public ServiceFactory SFactory
      {
          get { return _serviceFactory ?? new ServiceFactory(); }
      }
  
  
      public class ServiceFactory : BaseFactory&lt;TEntity, TDataObject&gt;
      {
      }
  
      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&lt;TDataObject&gt; GetMany()
      {
          try
          {
              var entities = Repository.Get(null, null, false);
              var _objs = SFactory.ListMappingEntitiesToObjects(entities);
  
              return _objs;
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }
  
      public Task&lt;TDataObject&gt; CreateAsync(TDataObject obj)
      {
          return Task.Run(() =&gt;
          {
              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;
              }
          });
      }}
  </code></pre>
  
  <p>As you've seen I brought in my <code>IUnitOfWork</code> where am initialising all my repository inside.</p>
  
  <p>And now this is how am calling it on my service</p>
  
  <pre><code> public class ProductService : BaseServices&lt;Product, ProductObject&gt;
  {
  
      public ProductService(IUnitOfWork uow, IFactoryInit factory) : base(uow, factory)
      {
      }
  }
  </code></pre>
  
  <p>Then I went ahead to bring all the servies in one class so I can use .Net Core Dependency Injection to bind it once</p>
  
  <pre><code> public class ServiceInit : IServiceInit
  {
      private IFactoryInit _factory;
      private IUnitOfWork _uow;
  
      private ProductService _productService;
  
      public ProductService ProductServices
      {
          get { return _productService ?? new ProductService(_uow, _factory); }
      }
  }
  </code></pre>
  
  <p>That's all I've done but I keep getting Null Reference Exception in my UnitOfWork initialisation and I even tried this</p>
  
  <pre><code>public IUnitOfWork UnitOfWork
      {
          get
          {
              if (_uow == null)
              {
                  _uow = new UnitOfWork(_context);
              }
              return _uow;
          }
      }
  </code></pre>
  
  <p>Explicitly and am still get null reference</p>
  
  <p>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</p>
  
  <pre><code>{
  public class VendorManagementServices : IVendorManagementServices
  {
      private IFactoryInit _factory;
      private IUnitOfWork _uow;
  
      public VendorManagementServices(IUnitOfWork uow, IFactoryInit factory)
      {
          _uow = uow;
          _factory = factory;
      }
  }
  </code></pre>
  
  <p>So, I dont know what am missing in my <code>BaseService</code> that keeps throwing </p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
  </code></pre>
  
  <p>Any Ideas guys?</p>