How to test Singleton class that has a static dependency - asp.net-mvc-3

I have a Singleton class that uses the thread-safe Singleton pattern from Jon Skeet as seen in the TekPub video. The class represents a cached list of reference data for dropdowns in an MVC 3 UI.
To get the list data the class calls a static method on a static class in my DAL.
Now I'm moving into testing an I want to implement an interface on my DAL class but obviously cannot because it is static and has only one static method so there's no interface to create. So I want to remove the static implementation so I can do the interface.
By doing so I can't call the method statically from the reference class and because the reference class is a singleton with a private ctor I can't inject the interface. How do I get around this? How do I get my interface into the reference class so that I can have DI and I can successfully test it with a mock?
Here is my DAL class in current form
public static class ListItemRepository {
public static List<ReferenceDTO> All() {
List<ReferenceDTO> fullList;
... /// populate list
return fullList;
}
}
This is what I want it to look like
public interface IListItemRepository {
List<ReferenceDTO> All();
}
public class ListItemRepository : IListItemRepository {
public List<ReferenceDTO> All() {
List<ReferenceDTO> fullList;
... /// populate list
return fullList;
}
}
And here is my singleton reference class, the call to the static method is in the CheckRefresh call
public sealed class ListItemReference {
private static readonly Lazy<ListItemReference> instance =
new Lazy<ListItemReference>(() => new ListItemReference(), true);
private const int RefreshInterval = 60;
private List<ReferenceDTO> cache;
private DateTime nextRefreshDate = DateTime.MinValue;
public static ListItemReference Instance {
get { return instance.Value; }
}
public List<SelectListDTO> SelectList {
get {
var lst = GetSelectList();
lst = ReferenceHelper.AddDefaultItemToList(lst);
return lst;
}
}
private ListItemReference() { }
public ReferenceDTO GetByID(int id) {
CheckRefresh();
return cache.Find(item => item.ID == id);
}
public void InvalidateCache() {
nextRefreshDate = DateTime.MinValue;
}
private List<SelectListDTO> GetSelectList() {
CheckRefresh();
var lst = new List<SelectListDTO>(cache.Count + 1);
cache.ForEach(item => lst.Add(new SelectListDTO { ID = item.ID, Name = item.Name }));
return lst;
}
private void CheckRefresh() {
if (DateTime.Now <= nextRefreshDate) return;
cache = ListItemRepository.All(); // Here is the call to the static class method
nextRefreshDate = DateTime.Now.AddSeconds(RefreshInterval);
}
}
}

You can use the singleton based on instance(not based on static), for which you can declare interface like this.
public interface IListItemRepository
{
List<ReferenceDTO> All();
}
public class ListItemRepository : IListItemRepository
{
static IListItemRepository _current = new ListItemRepository();
public static IListItemRepository Current
{
get { return _current; }
}
public static void SetCurrent(IListItemRepository listItemRepository)
{
_current = listItemRepository;
}
public List<ReferenceDTO> All()
{
.....
}
}
Now, you can mock IListItemRepository to test.
public void Test()
{
//arrange
//If Moq framework is used,
var expected = new List<ReferneceDTO>{new ReferneceDTO()};
var mock = new Mock<IListItemRepository>();
mock.Setup(x=>x.All()).Returns(expected);
ListItemRepository.SetCurrent(mock.Object);
//act
var result = ListItemRepository.Current.All();
//Assert
Assert.IsSame(expected, result);
}

Which DI framework are you using? Depending on your answer, IOC container should be able to handle single-instancing so that you don't have to implement your own singleton pattern in the caching class. In your code you would treat everything as instanced classes, but in your DI framework mappings you would be able to specify that only one instance of the cache class should ever be created.

One way to test it would be if you refactor your ListItemReference by adding extra property:
public sealed class ListItemReference {
...
public Func<List<ReferenceDTO>> References = () => ListItemRepository.All();
...
private void CheckRefresh() {
if (DateTime.Now <= nextRefreshDate) return;
cache = References();
nextRefreshDate = DateTime.Now.AddSeconds(RefreshInterval);
}
}
And then in your test you could do:
ListItemReference listReferences = new ListItemReference();
listReferences.References = () => new List<ReferenceDTO>(); //here you can return any mock data
Of course it's just temporary solution and I would recommend getting rid of statics by using IoC/DI.

Related

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 !

ObjectContext not retrieving recent changes

This code works fine, However, if I run execute a storedprocedure in my unit of work class (or any update, delete, add operation), I'm still getting original the data. Actually, I have already a solution(posted below in controller) but Im sure this is not the most elegant way, I hope someone can help me refactor the code. please help. Thanks
My unit of work
public class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly ObjectContext _context;
private BookRepository _books;
public UnitOfWork(ObjectContext context)
{
if (context == null)
{
throw new ArgumentNullException("Context was not supplied");
}
_context = context;
}
public IRepository<Book> Books
{
get
{
if (_books== null)
{
_books= new BookRepository (_context);
}
return _books;
}
}
public void UpdateAuthor(int id)
{
_context.ExecuteStoreCommand("sp_UpdateAuthor #param1",
new SqlParameter("param1", id));
}
public void Commit()
{
_context.SaveChanges();
}
Book Repository
public class BookRepository : Repository<Book>
{
public BookRepository (ObjectContext context)
: base(context)
{
}
public override Machine GetById(object id)
{
return _objectSet.SingleOrDefault(s => s.Id== (int)id);
}
}
Generic Repository
public abstract class Repository<T> : IRepository<T>
where T : class, IAuditEntity
{
protected IObjectSet<T> _objectSet;
public Repository(ObjectContext context)
{
_objectSet = context.CreateObjectSet<T>();
}
public abstract T GetById(object id);
public IEnumerable<T> GetAll()
{
return _objectSet;
}
public IEnumerable<T> Query(Expression<Func<T, bool>> filter)
{
return _objectSet.Where(filter);
}
public void Add(T entity)
{
_objectSet.AddObject(entity);
}
public void Remove(T entity)
{
_objectSet.DeleteObject(entity);
}
}
Controller Code
public class HomeController : Controller
{
private IUnitOfWork _unitOfWork;
#region Core Action Methods
public HomeController()
{
this._unitOfWork = new UnitOfWork(((IObjectContextAdapter)new BookContext()).ObjectContext);
}
private IEnumerable<BookViewModel> GetBookdsViewModels(int id)
{
//THE CODE WHERE ITS NOT RETURNING THE UPDATED VLAUES
//var query = _unitOfWork.Books.GetAll().Where(d => d.Id== id);
//I WANT TO CHANGE THIS CODE
ObjectContext objectContext = ((IObjectContextAdapter)new BookContext()).ObjectContext;
ObjectSet<Book> set = objectContext.CreateObjectSet<Book>();
set.MergeOption = MergeOption.OverwriteChanges;
var query = from a in set
where a.Id== id && !a.IsDeleted
select a;
return query
.Select(
c => new BookViewModel
{
Id = c.Id ,
Name = c.Name
});
}
I believe the problem is because you're executing things directly against your database, and then trying to refer back to the local copy that's stored in your repository and they're different.
public void UpdateAuthor(int id)
{
_context.ExecuteStoreCommand("sp_UpdateAuthor #param1",
new SqlParameter("param1", id));
}
When you run this query, you're making a change in your db instead of your local - why dont you do something like this:
public void UpdateAuthor(int id)
{
var book = Books.GetById(id);
/* make changes to your book object */
_unit.Save();
}
If you're wanting to make changes using store procs, you're going to have to dispose of your context, and recreate the repo's so that you're working with data from the DB instead of the local copy.
Expanding on Mark Oreta's answer, you need to ensure your Model is updated after manually executing a stored proc on your database. If you must manually call that stored proc then try this afterward:
_context.Entry<Book>(instanceOfAuthor).Reload();
Which might be:
_context.Entry<Book>(_context.Books.GetById(id)).Reload();

MVC3 Generic CRUD Controllers, Services and Repositories

We are attempting to create a generic CRUD controller in our MVC3 application which can handle the following actions Edit (Create/Edit), Delete and List (I have already looked here). The project has the Web (MVC) layer, Service (BLL) layer and a Repository (DB) layer. We are using constructor dependency injection as follows:
SERVICE LAYER
public class MyService : IMyService
{
private IMyRepository _myRepository;
public MySerivce(IMyRepository myRepository)
{
this._myRepository = myRepository;
}
public IEnumerable<IMyObject> GetObjects(int intParentKey)
{
return (IMyObject)_myRepository.GetObjects(intParentKey).Select(o => new MyObject(o));;
}
}
CONTROLLER
public class MyController
{
private IMyService _myService;
public MyController(IMyService myService)
{
this._myService = myService;
}
public ActionResult Index()
{
return View("Index", new List<MyFormModel>(_myService.GetObjects(intKeyValue).Select(a => new MyFormModel(a))));
}
}
The goal is to have a generic CRUD controller, but the difficulty is that the controllers will need to have knowledge of the MyFormModel object as well as a specific service. We would also like to have a generic service which handles the GetObjects, SaveObject, DeleteObject functions, but then that would need knowledge of the IMyObject interface, the MyObject implementation of IMyObject and the IMyRepository type. It appears right now that doing all of this with dependency injection is somewhat complicated.
FYI, the Service layer needs to change on the fly within the WEB portion of the tool depending on the current configuration.
In response to the generic CRUD comment, here is a BASE controller which accepts a BASE service through dependency injection:
public abstract class BaseCRUDController<T>
where T : class
{
private IBaseService _baseService;
public BaseCRUDController(IBaseService baseService)
{
this._baseService = baseService;
}
public ActionResult Index()
{
Type classType = typeof(T);
ConstructorInfo ciClass = classType.GetConstructor(new Type[] {});
return View("Index", new List<T>(_baseService.GetObjects(intKey).Select(o => (T)ciClass.Invoke(new object[] { o }))));
}
}
Obviously this generic controller is fairly straightforward, but the difficulty comes from the service side where the IMyObject and MyObject objects reside. The definition of the generic BaseService will require you to include the types of all of the objects in the base controller which is what we are trying to avoid. The code below could be way off the mark which is why I am asking for help:
SERVICE CLASS
public class MySerivce : BaseService<IMyObject, MyObject, DBObject>
{
public MySerivce(IMyRepository myRepository) :
base(myRepository)
{
}
}
BASE SERVICE INTERFACE
public interface IBaseService<IMT, MT, DT>
where IMT : class
where MT : class
where DT : class
{
IEnumerable<ValidationResult> Save(IMT model);
void Delete(int intKey);
IMT Get(int? intKey);
IEnumerable<IMT> GetObjects(int intParentKey);
}
BASE SERVICE IMPLEMENTATION
public class BaseService<IMT, MT, DT> : IBaseService<IMT, MT, DT>
where IMT : class
where MT : class
where DT : class
{
private IBaseRepository _baseRepository;
public BaseService(IBaseRepository baseRepository)
{
this._baseRepository = baseRepository;
}
public IEnumerable<ValidationResult> Save(IMT model)
{
List<ValidationResult> lResults = new List<ValidationResult>();
if (Validator.TryValidateObject(model, new ValidationContext(model, serviceProvider: null, items: null), lResults))
{
Type tDT = typeof(DT);
ConstructorInfo ciDTClass = tDT.GetConstructor(new Type[] { });
DT dtObject = (DT)ciDTClass.Invoke(new object[] { });
Mapper.CreateMap<IMT, DT>();
Mapper.Map(model, dtObject);
_baseRepository.Save<DT>(dtObject);
}
return lResults;
}
public void Delete(int intKey)
{
_baseRepository.Delete<DT>(intKey);
}
public IMT Get(int? intKey)
{
Type tMT = typeof(MT);
ConstructorInfo ciMTClass = tMT.GetConstructor(new Type[] { });
DT dtObject = _baseRepository.Get<DT>(intKey);
if (dtObject == null)
{
Type tDT = typeof(DT);
ConstructorInfo ciDTClass = tDT.GetConstructor(new Type[] { });
dtObject = (DT)ciDTClass.Invoke(new object[] { });
}
return (IMT)ciMTClass.Invoke(new object[] { dtObject });
}
public IEnumerable<IMT> GetObjects(int intParentKey)
{
Type tMT = typeof(MT);
ConstructorInfo ciMTClass = tMT.GetConstructor(new Type[] { });
IEnumerable<DT> dbObjects = _baseRepository.GetObjects<DT>(intParentKey);
return (IEnumerable<IMT>)dbObjects.Select(o => (MT)ciMTClass.Invoke(new object[] { o })); ;
}
}

Problem using Stateful EJB in ManagedBean with RequestScope

I'm using JSF 2.0 and EJB 3.1 in the Glassfish v3 app server. And I'm facing actually the follwing problem:
In a MenagedBean with RequestScope I want to access a session object (an EJB with #Stateful) which should store some session relevant information as the seleced category, the seleced page (with a datatable paginator for each category), etc. - nothing special I think.
The first time a category is selected, the datatable is created and displayed. Allright so far.
Now, if an item (row) is clicked (to show the item's details) or if the next page should be displayed, the session (the stateful EJB) is recreated and again the default values are used to display and render the page.
The code looks like:
#ManagedBean
#RequestScoped
public class TableViewBean {
#EJB
SessionBean session;
public DataModel getTable() {
return session.getDataModel();
}
public SessionBean getSession(){
return session;
}
public void next() {
session.getPaginator().nextPage();
session.resetList();
}
public void previous() {
session.getPaginator().previousPage();
session.resetList();
}
// some other code
}
and the session EJB:
#Stateful
public class SessionBean {
private String selectedType = "Entity";
private DataModel dataModel;
private int rowsPerPage = 5;
private Paginator paginator;
public void setSelectedType(String type){
if(!type.equalsIgnoreCase(selectedType)){
selectedType = type;
updateService();
}
resetList();
}
public void resetList() {
dataModel = null;
}
public void resetPagination() {
paginator = null;
}
public int getRowsPerPage() {
return rowsPerPage;
}
public void setRowsPerPage(int rowsPerPage) {
this.rowsPerPage = rowsPerPage;
resetList();
resetPagination();
}
public Paginator getPaginator() {
if (paginator == null) {
paginator = new Paginator(rowsPerPage) {
#Override
public int getItemsCount() {
return selectedService.getCount();
}
#Override
public DataModel createPageDataModel() {
DataModel model = null;
if(selectedService != null){
model = new ListDataModel(....);
}
return model;
}
};
}
return paginator;
}
public DataModel getDataModel() {
if(dataModel == null){
dataModel = getPaginator().createPageDataModel();
}
return dataModel;
}
}
If I change the Scope of the ManagedBean to SessionScope everything works fine, but I don't like this, because of use of memory concerns.
What's wrong with my code...please help me.
Greetz, Gerry
Your RequestScoped ManagedBean is re-instantiated for each request (that's what RequestScoped means after all). Therefore, with each instantiation it gets injected with a new SFSB instance.

linq populate custom collection

I have a collection defined as:
public class MyCollection : List<MyItem>
{
...
}
public class MyItem
{
...
}
Using linq, I can use the Select method to return a IEnumerable, and I can call .ToList on that to get an IList but is there some way of getting back a type of MyCollection? Because I am tring to instantiate a class which has a property of type MyCollection, and I want to use object initialization.
For example:
public class MyClass
{
MyCollection TestCollection {get;set}
...
}
MyClass test = new MyClass()
{
...
TestCollection = SomeObject.Select(item => new MyItem()
{
...
}).ToList();
}
I get a compile error because ToList returns List and it can't cast to a MyCollection object. Is there a way to do that?
You'll need to construct your MyCollection instance with the IEnumerable<MyItem>. Add a constructor like:
public MyCollection(IEnumerable<MyItem> items) : base(items) {}
Then, when you go to use this, you can do:
TestCollection = new MyCollection(SomeObject.Select(item => new MyItem());
You could make your own extension method that builds your collection from an IEnumerable, like
public static class Extensions
{
public static MyCollection ToMyCollection(this IEnumerable<MyItem> items)
{
//build and return your collection
}
}
or if MyItem is a placeholder for generic types in your context :
public static class Extensions
{
public static MyCollection ToMyCollection<T>(this IEnumerable<T> items)
{
//build and return your collection
}
}
Just mimic ToList:
public static class MyCollectionExtensions {
public static MyCollection ToMyCollection(this IEnumerable<MyItem> source) {
if (source == null) throw new NullReferenceException();
return new MyCollection(source);
}
}
MyCollection needs a new constructor:
public class MyCollection : List<MyItem> {
public MyCollection() : base() { }
public MyCollection(IEnumerable<MyItem> source) : base(source) { }
...
}
Also, it's generally not advisable to expose a setter for a collection. You should encapsulate mutation of the collection inside the class:
public class MyClass {
public MyCollection TestCollection { get { ... } } // should return a read-only collection
public void Add(MyItem item) {
_testCollection.Add(item);
}
MyCollection _testCollection = ...;
...
}

Resources