Entity framework context is not refreshing on modification - c#

I have a WPF Project which is n-tier architecture and I use context per/call as it has Direct-database and web-service
Sorry a bit long question
Presentation => Business Layer => Data Layer
In datalayer i have UnitOfWork pattern implemented.
Initally when my DefaultAccessPoint was a Static property i had issues on multiple thread using the same context.
I had resolved it by changing the DefaultAccessPoint to a non-static property, and seems that multi thread issue had been Resolved .
after the fix =>(e.g) User could insert data into the Application on First tab(which takes approx. 3 mins) and simultaneously access the Second Tab(To fetch some data) while both are done in separate threads.
But after this Fix the Context doesn't refresh when a modification is done
The initial fix was done under DataProviderBase class were i changed static DataAccessPoint to Non-static and commented those lines that you see below.
How would i keep the context refresh with multi threading ?
Any Help is appreciated
Code Block
This is my dataproviderbaseclass every data-provider is inherited this base
public abstract class DataProviderBase
{
public DataProviderBase()
{
DefaultAccessPoint = new DataAccessAccessPoint(ConnectionString);
}
protected readonly ILogger logger = LoggerFactory.GetLogger();
private static string _connectionString;
public static string ConnectionString
{
get
{
return _connectionString;
}
set
{
_connectionString = value;
//COMMENTED as Fix for multiThreading
//_defaultAccessPoint = new DataAccessAccessPoint(ConnectionString);
}
}
private IDataAccessAccessPoint _defaultAccessPoint;
public IDataAccessAccessPoint DefaultAccessPoint
{
get
{
//COMMENTED as Fix for multi Threading
// Removed statis Default AccessPoint that was causing the issue
return _defaultAccessPoint; //?? (_defaultAccessPoint = new DataAccessAccessPoint(ConnectionString));
}
set { _defaultAccessPoint = value; }
}
}
This is my DataAccessPoint
public class DataAccessAccessPoint : IDataAccessAccessPoint
{
private string _connectionString;
public string ConnectionString
{
get
{
return _connectionString;
}
set
{
_connectionString = value;
}
}
private IDataContext context;
public DataAccessAccessPoint(string connectionString)
{
_connectionString = connectionString;
context = new MyDataContext(_connectionString);
}
public virtual bool Save()
{
return context.SaveChanges() > 0;
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
//Here i supply the context to my Data access layers
private IMyDataLayerDA _myDA;
public IMyDataLayerdDA MydDA
{
get { return _myDA ?? (_myDA = new MydDA(context)); }
set { _myDA = value; }
}
}
My DataProvider
public class PersonRoleDataProvider : DataProviderBase, IPersonRoleDataProvider
{
public MYDTOCLASS AuthenticateUser(string userId)
{
return DefaultAccessPoint.MydDA.AuthenticateUser(userId);
}
public IEnumerable<MYDTOCLASS> GetRoles(int personId)
{
return DefaultAccessPoint.MydDA.GetRoles(personId);
}
}
DataProviderAccessPoint
public class DataProviderAccessPoint
{
private static PersonRoleDataProvider _personRoleDataProvider;
public static PersonRoleDataProvider PersonRoleDataProvider
{
get
{
if(_personRoleDataProvider==null)
_personRoleDataProvider = new PersonRoleDataProvider();
return _personRoleDataProvider;
}
}
}

Related

Is it possible to have a singleton exist accross different projects within a solution?

I am attempting to communicate information between two different projects. One is a remoting server and the other a WPF application. I have created a singleton class but have noticed that each time one project calls the GetInstance method it creates a new instance of the singleton. Is it possible to have a singleton accross projects as it doesn't seem it is and if that is the case, why is that?
Below is my code for the singleton
public class CommunicationSingleton
{
private int jobCount = 0;
private Job jobInProgress;
private List<Job> jobs = new List<Job>();
private static CommunicationSingleton instance = new CommunicationSingleton();
static readonly object obj = new object();
private CommunicationSingleton() { }
public static CommunicationSingleton GetInstance()
{
lock (obj)
{
if (instance == null)
{
return instance = new CommunicationSingleton();
}
else
{
return instance;
}
}
}
public void AddJob(Job job)
{
jobs.Add(job);
}
public List<Job> GetJobs()
{
return jobs;
}
public void IncreaseJobCount(int jobCount)
{
this.jobCount += jobCount;
}
public int GetJobCount()
{
return jobCount;
}
public void JobInProgress(Job jobInProgress)
{
this.jobInProgress = jobInProgress;
}
public Job GetJobInProgress()
{
return jobInProgress;
}
}

Is there a way of centralizing the access to a DataContext in C# using DI pattern?

this is what I got so far, but have to keep passing the DataClassesDataContext around. I wonder if there is a better more centralized way of using the DataClassesDataContext and fill the connectionstring each time the data context is used... Thanks for all the help in advance
public interface ICustomerDataAccess
{
string GetCustomerName(int customerId);
}
public class CustomerDataAccess : ICustomerDataAccess
{
private readonly DataClassesDataContext _context;
public CustomerDataAccess(DataClassesDataContext ctx)
{
_context = ctx;
}
public string GetCustomerName(int id)
{
return _context.Customers.Where(i => i.id == id).FirstOrDefault().name;
}
}
public class DataAccessFactory
{
public static ICustomerDataAccess GetCustomerDataAccessObj(DataClassesDataContext ctx)
{
return new CustomerDataAccess(ctx);
}
}
public class CustomerService
{
CustomerBusinessLogic _customerBL;
public CustomerService(DataClassesDataContext ctx)
{
_customerBL = new CustomerBusinessLogic(new CustomerDataAccess(ctx));
}
public string GetCustomerName(int id, DataClasses1DataContext ctx)
{
return _customerBL.GetCustomerName(id,ctx);
}
}
public class CustomerBusinessLogic
{
ICustomerDataAccess _custDataAccess;
public CustomerBusinessLogic(ICustomerDataAccess custDataAccess)
{
_custDataAccess = custDataAccess;
}
public CustomerBusinessLogic(DataClassesDataContext ctx)
{
_custDataAccess = new CustomerDataAccess(ctx);
}
public string GetCustomerName(int id, DataClassesDataContext ctx)
{
_custDataAccess = DataAccessFactory.GetCustomerDataAccessObj(ctx);
return _custDataAccess.GetCustomerName(id);
}
}
// and using a code like this on the interface
private void button5_Click(object sender, EventArgs e)
{
using (var ctx = new DataClassesDataContext)
{
CustomerService customerSrv = new CustomerService(ctx);
textBox1.Text = customerSrv.GetCustomerName(1, ctx);
}
}
You can use Generic Repository with Dependency Injection. This is a bit complex structure for the first time but this can solve your problem for your problem.
Also I share with you a nice and detail example. That was created by me
https://github.com/EyupCanARSLAN/N-Tier-Architecture-with-Generic-Repository--Dependency-Injection-And-Ninject/security/dependabot
Also, an article about this topic
https://dotnettutorials.net/lesson/generic-repository-pattern-csharp-mvc/

How do I properly use of UnitOfWorks, Repository and Dispose

I have taken over the coding of a software and the way it is currently done, I am not sure that the Dispose() method in the UnitOfWorks is getting called. I have noticed that my SQL service tends to stop after a while (days) saying that all connections are being used up.
I would like to know if the below code is properly done and if not, how I can go about rectifying so that it calls the dispose() method.
UnitOfWorks.cs
public class UnitOfWorks
{
private KaprukaEntities context;
private GenericRepository<Transaction> transactionRepository;
private bool disposed;
public GenericRepository<Transaction> TransactionRepository
{
get
{
if (this.transactionRepository == null)
this.transactionRepository = new GenericRepository<Transaction>(this.context);
return this.transactionRepository;
}
}
public UnitOfWorks(KaprukaEntities entities)
{
this.context = entities;
}
public void Save()
{
this.context.SaveChanges();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed && disposing)
this.context.Dispose();
this.disposed = true;
}
public interface IDisposable
{
void Dispose();
}
}
The UnitOfWorks is getting called by a service class.
TransactionService.cs
public class TransactionService
{
private UnitOfWorks _unitOfWork;
private KaprukaEntities entities;
public TransactionService(UnitOfWorks unitOfWork)
{
this._unitOfWork = unitOfWork;
this.entities = new KaprukaEntities();
}
public bool Add(Transaction entity)
{
this._unitOfWork.TransactionRepository.Add(entity);
this._unitOfWork.Save();
var er = this.entities.GetValidationErrors();
return true;
}
public bool Update(Transaction entity)
{
this._unitOfWork.TransactionRepository.Update(entity);
this._unitOfWork.Save();
return true;
}
public bool Delete(Transaction entity)
{
this._unitOfWork.TransactionRepository.Delete(entity);
this._unitOfWork.Save();
return true;
}
public IEnumerable<Transaction> GetAll()
{
return this._unitOfWork.TransactionRepository.GetAll();
}
public IEnumerable<Transaction> GetAll(Expression<Func<Transaction, bool>> filter, Func<IQueryable<Transaction>, IOrderedQueryable<Transaction>> orderBy, string includeProperties)
{
return this._unitOfWork.TransactionRepository.Get(filter, orderBy, includeProperties);
}
}
The way this is getting called is as follows:
There is a file named updateTransaction.aspx and in its code behind, the code calls the service as such:
updateTransaction.aspx
TransactionService TransServ = new TransactionService(new UnitOfWorks(new KaprukaEntities()));
var TransDetails = TransServ.GetAll(x => x.TransactionID == intTID, null, "").SingleOrDefault();
I can't see any code to close or dispose anything.
Your transactionservice constructor creates an extra db context, when one is already in the unit of work class:
this.entities = new KaprukaEntities();

Ninject Factory Pattern and Bindings

I am trying to implement the Ninject.Extensions.Factory pattern and my program is telling me my bindings aren't right, but I can't figure out why. I keep getting an "Error activating IHashable. No matching bindings are available, and the type is not self-bindable" exception thrown. The relevant areas of my code are below:
public interface IHashable
{
FileInfo File { get; }
string ComputeHash();
}
public interface IHashableFactory
{
IHashable GetNew(FileInfo file);
}
public class MD5ChecksumProvider : IHashable
{
private FileInfo _file;
public FileInfo File
{
get { return _file; }
}
public MD5ChecksumProvider(FileInfo file)
{
if (file == null)
throw new ArgumentNullException("file");
_file = file;
}
public string ComputeHash()
{
// implementation
}
}
public class AppFileProvider : IAppFileProvider
{
private IHashableFactory _hashFactory;
public IHashableFactory HashProvider
{
get { return _hashFactory; }
}
public AppFileProvider(IHashableFactory hashProviderFactory)
{
_hashFactory = hashProviderFactory;
}
public string GetChecksum(FileInfo fileInfo)
{
var hasher = _hashFactory.GetNew(fileInfo);
return hasher.ComputeHash();
}
}
public class BindingProviders : NinjectModule
{
public override void Load()
{
Bind<IHashable>()
.To<MD5ChecksumProvider>();
}
}
public class BindingFactories : NinjectModule
{
public override void Load()
{
Bind<IHashableFactory>()
.ToFactory();
}
}
// my DI container
public sealed class Container : IDisposable
{
private bool _isDisposed;
private IKernel _kernel;
private BindingFactories _bindingFactories;
private BindingObjects _bindingObjects;
private BindingProviders _bindingProviders;
public Container()
{
_isDisposed = false;
_bindingFactories = new BindingFactories();
_bindingObjects = new BindingObjects();
_bindingProviders = new BindingProviders();
_kernel = new StandardKernel(_bindingObjects, _bindingProviders, _bindingFactories);
}
public T Get<T>()
{
return _kernel.Get<T>();
}
public void Dispose()
{
// nothing worth seeing
}
private void Dispose(bool disposing)
{
// nothing worth seeing
}
}
// the program (composition root)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
using (var container = new Container())
{
var fileProvider = container.Get<IAppFileProvider>();
foreach (var file in files)
{
string hash = fileProvider.GetChecksum(storePath, file); // this line throws "Error activating IHashable. No matching bindings are available, and the type is not self-bindable.""
}
}
}
}
I feel like my bindings are setup correctly but I must be missing something obvious. Any ideas why I'm getting the exception from the above code?
This is caused by a feature of Ninject.Extensions.Factory.
It treats methods which start with Get differently from those which don't.
If you rename IHashableFactory.GetNew to Create or Make everything works fine.
The "Get" feature is described here:
The default instace provider of the extension has the convention that it tries to return an instance using a named binding whenever a method starts with “Get”. E.g. IFoo GetMySpecialFoo() is equal to
resolutionRoot.Get<IFoo>("MySpecialFoo");
Since i think this is not obvious to the user and the exception isn't helpful at all in this regard, i have filed an issue report here

Sourcing AppSettings from database & cache

At my office, it has been deemed that we are to put our AppSettings for the Web.Config in the database. As such, I created the following, but have some doubts about a couple aspects of the code.
So my question is:
The line containing "Cache cache = new Cache()" in the UTILITY class is probably wrong because it creates a NEW cache object.
Q: So, what should I be doing for that line?
...any help is appreciated.
The overall objective was to be able to make a call like this:
Utility.GetConfigurationValue(ConfigurationSection.AppSettings, "myVariable");
...and have it retrieve from the cache or the database auto-magically.
THE UTILITY CODE:
public static class Utility
{
#region "Configurations"
public static String GetConfigurationValue(ConfigurationSection section, String key)
{
Configurations config = new Configurations();
Cache cache = new Cache(); // <--- This is probably wrong!!!!
if (!cache.TryGetItemFromCache<Configurations>(out config))
{
config.List(SNCLavalin.US.Common.Enumerations.ConfigurationSection.AppSettings);
cache.AddToCache<Configurations>(config, DateTime.Now.AddMinutes(15));
}
var result = (from record in config
where record.Key == key
select record).FirstOrDefault();
return (result == null) ? null : result.Value;
}
#endregion
}
THE EXTENSION CODE:
public static class Extensions
{
#region "System.Web.Caching"
public static void Remove<T>(this Cache cache) where T : class
{
cache.Remove(typeof(T).Name);
}
public static void AddToCache<T>(this Cache cache, object item, DateTime absoluteExpiration) where T : class
{
T outItem = null;
if (cache.TryGetItemFromCache<T>(out outItem))
return;
cache.Insert(typeof(T).Name,
item,
null,
absoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal,
null);
}
public static bool TryGetItemFromCache<T>(this Cache cache, out T item) where T : class
{
item = cache.Get(typeof(T).Name) as T;
return item != null;
}
#endregion
}
THE LIST-CLASS CODE:
public class Configurations : List<Configuration>
{
#region CONSTRUCTORS
public Configurations() : base()
{
initialize();
}
public Configurations(int capacity) : base(capacity)
{
initialize();
}
public Configurations(IEnumerable<Configuration> collection) : base(collection)
{
initialize();
}
#endregion
#region PROPERTIES & FIELDS
private Crud _crud;
#endregion
#region EVENTS
#endregion
#region METHODS
private void initialize()
{
_crud = new Crud("CurrentDbConnection");
}
public Configurations List(ConfigurationSection section)
{
using (DbCommand dbCommand = _crud.Db.GetStoredProcCommand("spa_LIST_SecConfiguration"))
{
_crud.Db.AddInParameter(dbCommand, "#Section", DbType.String, section.ToString());
_crud.List(dbCommand, PopulateFrom);
}
return this;
}
public void PopulateFrom(DataTable table)
{
this.Clear();
foreach (DataRow row in table.Rows)
{
Configuration instance = new Configuration();
instance.PopulateFrom(row);
this.Add(instance);
}
}
#endregion
}
THE ITEM-CLASS CODE:
public class Configuration
{
#region CONSTRUCTORS
public Configuration()
{
initialize();
}
#endregion
#region PROPERTIES & FIELDS
private Crud _crud;
public string Section { get; set; }
public string Key { get; set; }
public string Value { get; set; }
#endregion
#region EVENTS
#endregion
#region METHODS
private void initialize()
{
_crud = new Crud("CurrentDbConnection");
Clear();
}
public void Clear()
{
this.Section = "";
this.Key = "";
this.Value = "";
}
public void PopulateFrom(DataRow row)
{
Clear();
this.Section = row["Section"].ToString();
this.Key = row["Key"].ToString();
this.Value = row["Value"].ToString();
}
#endregion
}
You have correctly identified the problem - the current code creates a new Cache instance on each call to GetConfigurationValue which defeats the purpose of caching. You need to make the Cache instance static rather than creating a new instance each time.
public static class Utility
{
private static Cache cache = new Cache(); // Static class variable
#region "Configurations"
public static String GetConfigurationValue(ConfigurationSection section, String key)
{
Configurations config = new Configurations();
// Cache cache = new Cache(); --- removed
...
}
}
ADDENDUM TO ANSWER:
I did (in fact) need to point the cache variable to something else. It wouldn't work until I did the following in the Utility class.
private static Cache cache = System.Web.HttpRuntime.Cache;

Categories