Unable to resolve service for type 'BookEvent.Project.UserDomain.UoW.ICommentUnitOfWork' while attempting to activate 'CommentAppService'.) - asp.net-mvc-3

Hi I am Getting this error BookEvent.Project.UserDomain.AppServices.ICommentAppService Lifetime: Transient ImplementationType: BookEvent.Project.UserDomain.AppServices.CommentAppService': Unable to resolve service for type 'BookEvent.Project.UserDomain.UoW.ICommentUnitOfWork' while attempting to activate 'BookEvent.Project.UserDomain.AppServices.CommentAppService'.) when I run my application. Since I not able to figure out what I am doing work. I have no idea how to resolve it. Can anybody help me please.
CommentAppService.cs
public class CommentAppService:AppService, ICommentAppService
{
private IMapper _mapper;
private ICommentRepository _commentRepository;
public CommentAppService(ICommentUnitOfWork unitOfWork, ICommentRepository commentRepository, IMapper mapper, IExceptionManager exceptionManager) : base(unitOfWork, exceptionManager)
{
this._mapper = mapper;
this._commentRepository = commentRepository;
}
}
CommentUnitOfWork.cs
public class CommentUnitOfWork : UnitOfWork, ICommentUnitOfWork
{
public CommentUnitOfWork(BookDomainDbContext dbContext, IExceptionManager exceptionManager) : base(dbContext, exceptionManager)
{
}
}
Startup.cs
services.AddTransient<IUserAppService, UserAppService>();
services.AddTransient<IEventAppService, EventAppService>();
services.AddTransient<ICommentAppService, CommentAppService>();
CommentController.cs
public class CommentController : Controller
{
private readonly ILogger<CommentController> _logger;
private readonly ICommentAppService _commentAppService;
private readonly IMapper _mapper;
public string Message { get; set; }
public CommentController(ILogger<CommentController> logger, ICommentAppService commentAppService, IMapper mapper)
{
_logger = logger;
_commentAppService = commentAppService;
_mapper = mapper;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult CreateComment(CommentDTO item)
{
item.Date = DateTime.Now;
var result = _commentAppService.Create(item);
if (result.IsSuccess)
{
this._logger.LogInformation(result.MainMessage.Text);
}
else
{
Message = $"About page visited at {DateTime.UtcNow.ToLongTimeString()}";
this._logger.LogError(Message);
return View("CreateEvent");
}
return RedirectToAction("Index", "Home");
}
public IActionResult CreateComment()
{
return View(new CommentViewModel());
}
Thanks in Advance.

The exception alredy tells you exacly what is going wrong:
Unable to resolve service for type 'BookEvent.Project.UserDomain.UoW.ICommentUnitOfWork' while attempting to activate 'BookEvent.Project.UserDomain.AppServices.CommentAppService'.)
So when DI is trying to instanciate CommentAppService it is looking for ICommentUnitOfWork because it is expected in the constructor of CommentAppService but it cannot find an implementation for ICommentUnitOfWork.
In your startup you are registering three things
services.AddTransient<IUserAppService, UserAppService>();
services.AddTransient<IEventAppService, EventAppService>();
services.AddTransient<ICommentAppService, CommentAppService>();
but none of them is ICommentUnitOfWork. Just register it in the startup and it will work fine.

Related

ASP.NET Core Web API : dependency injection based on runtime parameter value

I am working on an ASP.NET Core Web API application. My API will accept a country name as one of the input parameter from request body.
Due to nature of the application, we have country wise database with same schema. I have created DbContext for one of the databases and want to initialize the DbContext by the passing the connection string based on input request parameter value.
I have created factory method to return the needed database context based on the parameter passed to the factory method. However, the challenge I am facing is, while initializing the factory class as DI from controller, object of factory class is instantiated before the controller action is called. Hence, parameter value in factory method is empty.
How can I pass a parameter in runtime to initialize an object using dependency injection?
Here is the code...
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
public class MyDBContext : DbContext
{
public MyDBContext(DbContextOptions<MyDBContext> options)
: base(options)
{
}
public virtual DbSet<Student> Students { get; set; }
}
public interface IDbContextFactory
{
public MyDBContext GetDbContext(string
connectionString);
}
public class DbContextFactory : IDbContextFactory
{
public MyDBContext GetDbContext(string connectionString)
{
MyDBContext context = null;
if (!string.IsNullOrWhiteSpace(connectionString))
{
DbContextOptionsBuilder<MyDBContext> _dbContextOptionsBuilder = new DbContextOptionsBuilder<MyDBContext>().UseSqlServer(connectionString);
context = new MyDBContext(_dbContextOptionsBuilder.Options);
}
return context;
}
}
public interface IRepository
{
Student GetData();
}
public class Repository : IRepository
{
private MyDBContext _context;
public Repository(IDbContextFactory dbContextFactory)
{
// Here I need connection string based on input parameter (country) from request to dynamically generate country specific connection string
string connectionString = string.Empty;
_context = dbContextFactory.GetDbContext(connectionString);
}
public Student GetData()
{
return _context.Students.FirstOrDefault();
}
}
public interface IServiceAgent
{
Student GetData();
}
public class ServiceAgent : IServiceAgent
{
IRepository _repository;
public ServiceAgent(IRepository repository)
{
_repository = repository;
}
public Student GetData()
{
return _repository.GetData();
}
}
[ApiController]
[Route("[controller]")]
public class HomeController : ControllerBase
{
private readonly IServiceAgent _serviceAgent;
public HomeController(IServiceAgent serviceAgent)
{
_serviceAgent = serviceAgent;
}
[HttpGet]
public Student Get(string country)
{
return _serviceAgent.GetData();
}
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<IServiceAgent, ServiceAgent>();
services.AddScoped<IRepository, Repository>();
services.AddScoped<IDbContextFactory, DbContextFactory>();
services.AddScoped<DetermineCountryFilter>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}

EF Core 5.0 How to manage multiple entity class with one generic repository

First question here, I hope I'm doing it right.
I'm using Entity Framework Core 5.0 (Code First) with an onion architecture (data/repo/service/mvc) and so I have a service for each table (almost).
It's work well but now I need to manage (get, insert, update, delete) about 150 tables which all have the same structure (Id, name, order).
I have added each of them as Entity class and their DbSet too in my DbContext, but I don't want to make 150 services, I would like to have a generic one .
How can I bind it to my generic repository ?
public class Repository<T> : IRepository<T> where T : BaseEntity
{
private readonly ApplicationContext context;
private DbSet<T> entities;
private readonly RepositorySequence repoSequence;
private string typeName { get; set; }
public Repository(ApplicationContext context)
{
this.context = context;
entities = context.Set<T>();
this.repoSequence = new RepositorySequence(context);
this.typeName = typeof(T).Name;
}
public T Get(long plng_Id)
{
return entities.SingleOrDefault(s => s.Id == plng_Id);
}
[...]
}
In an ideal world, would like to have something like this :
public async Task Insert(dynamic pdyn_Entity)
{
Type DynamicType = Type.GetType(pdyn_Entity);
Repository<DynamicType> vobj_Repo = new Repository<DynamicType>(mobj_AppContext);
long Id = await vobj_Repo.InsertAsync(pdyn_Entity);
}
But I can try to get type from DbSet string Name too, I just managed to retrieve some data :
public IEnumerable<object> GetAll(string pstr_DbSetName)
{
return ((IEnumerable<BaseEntity>)typeof(ApplicationContext).GetProperty(pstr_DbSetName).GetValue(mobj_AppContext, null));
}
I've tried the following method (2.0 compatible apparently) to get the good DbSet, not working neither (no Query) : https://stackoverflow.com/a/48042166/10359024
What am I missing?
Thanks a lot for your help
Not sure why you need to get type?
You can use something like this.
Repository.cs
public class Repository<T> : IRepository<T> where T : BaseEntity
{
private readonly ApplicationContext context;
private DbSet<T> entities;
public Repository(ApplicationContext context)
{
this.context = context;
entities = context.Set<T>();
}
public List<T> Get()
=> entities.ToList();
public T Get(long plng_Id)
=> entities.Find(plng_Id);
public long Save(T obj)
{
if (obj.ID > 0)
entities.Update(obj);
else
entities.Add(obj);
return obj.ID;
}
public void Delete(T obj)
=> entities.Remove(obj);
}
Then you can use either one of these 2 options you want
Multiple repositories following your tables
UserRepository.cs
public class UserRepository : Repository<User> : IUserRepository
{
private readonly ApplicationContext context;
public UserRepository(ApplicationContext context)
{
this.context = context;
}
}
BaseService.cs
public class BaseService : IBaseService
{
private readonly ApplicationContext context;
private IUserRepository user;
private IRoleRepository role;
public IUserRepository User { get => user ??= new UserRepository(context); }
public IRoleRepository Role { get => user ??= new RoleRepository(context); }
public BaseService(ApplicationContext context)
{
this.context = context;
}
}
If you are lazy to create multiple repositories, can use this way also. Your service just simple call Repository with entity name.
BaseService.cs
public class BaseService : IBaseService
{
private readonly ApplicationContext context;
private IRepository<User> user;
private IRepository<Role> role;
public IRepository<User> User { get => user ??= new Repository<User>(context); }
public IRepository<Role> Role { get => role ??= new Repository<Role>(context); }
public BaseService(ApplicationContext context)
{
this.context = context;
}
}
Finally, you can call service like this. You can use multiple services instead of BaseService if you want.
HomeController.cs
public class HomeController : Controller
{
private readonly IBaseService service;
public HomeController(IBaseService service)
{
this.service = service;
}
public IActionResult Index()
{
var user = service.User.Get();
return View(user);
}
public IActionResult Add(User user)
{
var id = service.User.Save(user);
return View();
}
}
I suggest to use first option (multiple repositories) because you may need to customise functions in own repository in future. And create service class following your controller name. For example, you have HomeController, UserController, etc. Create HomeService, UserService and link them with BaseService so that you can create customised functions in their own service class.
I assume you have a base entity like this:
public class BaseEntity
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Order { get; set; }
}
Then you can do CRUD operations in your generic repository like this:
public int Create(T item)
{
if (item == null) return 0;
entities.Add(item);////SaveChanges
return item.Id;
}
public void Update(T updatedItem)
{
context.SetModified(updatedItem);//SaveChanges
}
public IQueryable<T> All()
{
return entities();
}
And in each of the methods you have access to your 3 common fields in BaseEntity
Thank you all for your responses.
I need to have the type because I am using a blazor component which automatically binds to these tables. This component has the name of the desired entity class (in string) as a parameter. Thanks to #Asherguru's response I was able to find a way to do this:
1 - I made a 'SedgmentEntity' Class :
public abstract class SegmentEntity : ISegmentEntity
{
public abstract long Id { get; set; }
public abstract string Name { get; set; }
public abstract short? Order { get; set; }
}
2 - A SegmentRepository which is typed via Reflection:
public class SegmentRepository : ISegmentRepository
{
private readonly ApplicationContext context;
private readonly RepositorySequence repoSequence;
public SegmentRepository(ApplicationContext context)
{
this.context = context;
this.repoSequence = new RepositorySequence(context);
}
public async Task<long> Insert(string pstr_EntityType, SegmentEntity pobj_Entity)
{
Type? vobj_EntityType = Assembly.GetAssembly(typeof(SegmentEntity)).GetType("namespace.Data." + pstr_EntityType);
if (vobj_EntityType != null)
{
// create an instance of that type
object vobj_Instance = Activator.CreateInstance(vobj_EntityType);
long? nextId = await repoSequence.GetNextId(GetTableName(vobj_EntityType));
if (nextId == null)
{
throw new TaskCanceledException("Sequence introuvable pour " + vobj_EntityType.FullName);
}
PropertyInfo vobj_PropId = vobj_EntityType.GetProperty("Id");
vobj_PropId.SetValue(vobj_Instance, nextId.Value, null);
PropertyInfo vobj_PropName = vobj_EntityType.GetProperty("Name");
vobj_PropName.SetValue(vobj_Instance, pobj_Entity.Name, null);
PropertyInfo vobj_PropOrder = vobj_EntityType.GetProperty("Order");
vobj_PropOrder.SetValue(vobj_Instance, pobj_Entity.Order, null);
return ((SegmentEntity)context.Add(vobj_Instance).Entity).Id;
}
}
public IEnumerable<object> GetAll(string pstr_EntityType)
{
Type? vobj_EntityType = Assembly.GetAssembly(typeof(SegmentEntity)).GetType("namespace.Data." + pstr_EntityType);
if (vobj_EntityType != null)
{
PropertyInfo vobj_DbSetProperty = typeof(ApplicationContext).GetProperties().FirstOrDefault(prop =>
prop.PropertyType.FullName.Contains(vobj_EntityType.FullName));
return (IEnumerable<object>)vobj_DbSetProperty.GetValue(context, null);
}
return null;
}
}
I still have to handle the Get and the Delete functions but it should be fine.
Then I will be able to create a single service which will be called by my component.
Thanks again !

Is it correct to get a scoped service from singleton using IHttpContextAccessor?

What are the possible implications or side effects of this "request-scoped service provider" implementation and usage?
public interface IScopedServiceProvider : IServiceProvider
{
}
public class ScopedServiceProvider : IScopedServiceProvider
{
private readonly IHttpContextAccessor _httpContextAccessor;
public ScopedServiceProvider(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public object GetService(Type serviceType)
{
return _httpContextAccessor.HttpContext.RequestServices.GetService(serviceType);
}
}
The objective is basically to access a scoped service from a given singleton service:
Registered in the container like this:
services.AddHttpContextAccessor();
services.AddScoped<IScopedServiceProvider, ScopedServiceProvider>();
services.AddScoped<IMyScopedService, MyScopedService>();
services.AddSingleton<IMySingletonService, MySingletonService>();
Used like this:
public class MySingletonService : IMySingletonService
{
private readonly IScopedServiceProvider _scopedServiceProvider;
public MySingletonService(IScopedServiceProvider scopedServiceProvider)
{
_scopedServiceProvider = scopedServiceProvider;
}
public void DoWork()
{
var scopedService = _scopedServiceProvider.GetService<IMyScopedService>();
scopedService.DoAnotherWork();
}
}

Service isn't implementing interface member noobie

I am re engineering an MVC3 app to take all linq out of controllers and in to proper layers.
I have got this as my structure SQL --> EF --> Repository --> Service --> Controller. I am using interfaces.
When compiling I am getting this error:
gpc.data.service.roleService does not implement interface member gpc.data.interfaces.iroleservice.HolderNamesbyRoleID(int).
I am totally new to proper architecture so apologies if this is blindingly obvious lol. Here is some code:
Repository:
namespace gpc.Data.Repositories
{
public class roleRepository :gpc.Data.Interfaces.IRoleRepository
{
private gpc.Models.gpcEntities _entities = new Models.gpcEntities();
public HolderNames HolderNamesbyRoleID(int roleid)
{
return (from i in _entities.HolderNames
where i.roleid == roleid select i).FirstOrDefault();
}
}
}
I then have an interface:
namespace gpc.Data.Interfaces
{
public interface IRoleRepository
{
HolderNames HolderNamesbyRoleID(int roleid);
}
}
Then I have the service:
namespace gpc.Data.Service
{
public class roleService : gpc.Data.Interfaces.IRoleService
{
private ModelStateDictionary _modelState;
private gpc.Data.Interfaces.IRoleRepository _repository;
public roleService(ModelStateDictionary modelState)
{
_modelState = modelState;
_repository = new gpc.Data.Repositories.roleRepository();
}
public roleService(ModelStateDictionary modelState,
gpc.Data.Repositories.roleRepository repository)
{
_modelState = modelState;
_repository = repository;
}
public HolderNames HolderNames(int roleid)
{
return _repository.HolderNamesbyRoleID(roleid);
}
}
}
I then have another interface:
namespace gpc.Data.Interfaces
{
public interface IRoleService
{
HolderNames HolderNamesbyRoleID(int roleid);
}
}
I created a very simple ienumerable in this structure and I was able to get data on to the view through the controller as i would expect. I guess that as this one is a bit more complicated that a select everything and throw it at a view I must have missed something. I don't know if it makes a difference, but "holdernames" is a SQL view as opposed to a table.
Any help greatly appreciated
It's basically just what your compiler error shows. Your IRoleService interface defines a method named HolderNamesbyRoleID, but in your implementation you only have a method named HolderNames.
I assume this is just a mistype on your part.
Interface only contains the signature. You have to write actual implementation in class where you have implemented your interface. In your case you have define method HolderNamesbyRoleID in IRoleRepository but you have not implemented this method in roleService class. You must have to implement HolderNamesbyRoleID in roleService class.
Your roleService class code becomes like below.
namespace gpc.Data.Service
{
public class roleService : gpc.Data.Interfaces.IRoleService
{
private ModelStateDictionary _modelState;
private gpc.Data.Interfaces.IRoleRepository _repository;
public roleService(ModelStateDictionary modelState)
{
_modelState = modelState;
_repository = new gpc.Data.Repositories.roleRepository();
}
public roleService(ModelStateDictionary modelState,
gpc.Data.Repositories.roleRepository repository)
{
_modelState = modelState;
_repository = repository;
}
public HolderNames HolderNamesbyRoleID(int roleid)
{
return _repository.HolderNamesbyRoleID(roleid);
}
}
}
Refer interface for more info.

Ninject, Repository and DAL

I am new to MVC, repository concept and dependency injection.
My repository and DAL looks like
public interface IRepository<TEntity> where TEntity : class
{
List<TEntity> FetchAll();
IQueryable<TEntity> Query { get; }
void Add(TEntity entity);
void Delete(TEntity entity);
void Save();
}
public class Repository<T> : IRepository<T> where T : class
{
private readonly DataContext _db;
public Repository(DataContext db)
{
_db = db;
}
#region IRepository<T> Members
public IQueryable<T> Query
{
get { return _db.GetTable<T>(); }
}
public List<T> FetchAll()
{
return Query.ToList();
}
public void Add(T entity)
{
_db.GetTable<T>().InsertOnSubmit(entity);
}
public void Delete(T entity)
{
_db.GetTable<T>().DeleteOnSubmit(entity);
}
public void Save()
{
_db.SubmitChanges();
}
#endregion
}
In Global.asax file I have
private void RegisterDependencyResolver()
{
var kernel = new StandardKernel();
kernel.
Bind(typeof(IRepository<>)).
To(typeof(Repository<>))
.WithConstructorArgument("db", new DataContext(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()));
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
but when I am trying to access repository I get "Object reference not set to an instance of an object". Do I understand correctly how Repository and Injection should work?
public class AdminController : Controller
{
private readonly IRepository<User> _userRepository;
public ActionResult Index()
{
var a = _userRepository.FetchAll(); //I get exception here
return View();
}
}
You get nullref because you don't set _userRepository. Set it in the AdminControllers constructor and Niject will inject it happily:
public class AdminController : Controller
{
private readonly IRepository<User> _userRepository;
public AdminController(IRepository<User> userRepository)
{
_userRepository = userRepository;
}
//...
}
You can read here more about the injection patterns with Ninject and how injection works.
In the web config file
<appSettings>
<add key="RepoSetting" value="Solution.DAL.OrderRepository"/>
</appSettings>
In the ninject web common class
private static void RegisterServices(Ikernel Kernel)
{
//kernl.Bind<Irepo>().To<CustRepo>();
string name = WebConfigurationManager.AppSettings["RepoSetting"];
Type repoToInject = Assembly.GetExecutingAssembly().GetType(name);
kernel.Bind<ICustomerRepository>().To(repoToInject
}

Resources