Create a log everytime When methods in an interface class are called - asp.net

I want to update a log file(txt) everytime when methods in a an interface class are called?
Is there any way to do this other than writing code in every method to create log?

Here's my 30 mins. you'll have to implement the logging code somewhere so you have to create another abstraction for your code. thus an abstract class is needed. i think. this is very quick and dirty.
public interface IService<T>
{
List<T> GetAll();
bool Add(T obj);
}
then you'll need the abstract class where you'll need to implement your logging routine
public abstract class Service<T> : IService<T>
{
private void log()
{
/// TODO : do log routine here
}
public bool Add(T obj)
{
try
{
log();
return AddWithLogging(obj);
}
finally
{
log();
}
}
public List<T> GetAll()
{
try
{
log();
return GetAllWithLog();
}
finally
{
log();
}
}
protected abstract List<T> GetAllWithLog();
protected abstract bool AddWithLogging(T obj);
}
as for your concrete classes
public class EmployeeService : Service<Employee>
{
protected override List<Employee> GetAllWithLog()
{
return new List<Employee>() { new Employee() { Id = 0, Name = "test" } };
}
protected override bool AddWithLogging(Employee obj)
{
/// TODO : do add logic here
return true;
}
}
public class CompanyService : Service<Company>
{
protected override List<Company> GetAllWithLog()
{
return new List<Company>() { new Company() { Id = 0, Name = "test" } };
}
protected override bool AddWithLogging(Company obj)
{
/// TODO : do add logic here
return true;
}
}
public class Employee
{
public int Id {get;set;}
public string Name { get; set;}
}
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
}
then on your implementation you can just..
static void Main(string[] args)
{
IService<Employee> employee = new EmployeeService();
List<Employee> employees = employee.GetAll();
foreach (var item in employees)
{
Console.WriteLine(item.Name);
}
IService<Company> company = new CompanyService();
List<Company> companies = company.GetAll();
foreach (var item in companies)
{
Console.WriteLine(item.Name);
}
Console.ReadLine();
}
hope this helps!

I think you would have to use Aspect Oriented Programming to achieve that. Read http://www.sharpcrafters.com/aop.net

I think you meant class (instead of interface)
Two options I can think of:
Implementing INotifyPropertyChanged which is in lines of writing code in every method
or
to adopt on of the AOP frameworks in the article http://www.codeproject.com/KB/cs/AOP_Frameworks_Rating.aspx if that is not a major leap

Related

How to apply Command Design pattern with Dependency Injection using Generic Class?

i want to apply command Design pattern with dependency Injection in the following situation: i have three types of reports in my system (SubscriptionReport, SalesReport, SpechialistReport) so i created one interface IReportService
public interface IReportService<T> where T: class
{
public Task<GenericResponse<List<T>>> GetReport(string searchKey, DateTime from, DateTime to);
}
and to apply OCP i have implemented the GetReport function tree times for (SubscriptionReport, SalesReport, SpechialistReport)
public class SpechialistReportService : IReportService<SpechialistReportDTO>
{
public Task<GenericResponse<List<SpechialistReportDTO>>> Report(string searchKey, DateTime from, DateTime to)
{
throw new NotImplementedException(); // to be implemented later
}
}
public class SubscriptionReportService : IReportService<SubscriptionReportDTO>
{
public Task<GenericResponse<List<SubscriptionReportDTO>>> Report(string searchKey, DateTime from, DateTime to)
{
throw new NotImplementedException(); // to be implemented later
}
}
public class SalesReportService : IReportService<SalesReportDTO>
{
public Task<GenericResponse<List<SalesReportDTO>>> Report(string searchKey, DateTime from, DateTime to)
{
throw new NotImplementedException(); // to be implemented later
}
}
after that i have added the dependency
services.AddScoped(typeof(IReportService<SpechialistReportDTO>), typeof(SpechialistReportService));
services.AddScoped(typeof(IReportService<SubscriptionReportDTO>), typeof(SubscriptionReportService));
services.AddScoped(typeof(IReportService<SalesReportDTO>), typeof(SalesReportService));
the problem is in calling the dependency in the controller constructor
private readonly IEnumerable<IReportService> _reportService; // Should be IReportService<dont know what class should i specify here>
public ReportController(IReportService<T> reportService)
{
this._reportService = reportService;
}
Any help would be appreciated thanks in advance,
Okay i solved this problem by removing the Generic and adding marker interface to the DTOs classes
public interface ReportRoot
{
}
public class SubscriptionReportDTO : ReportRoot
{
// Some data here
}
public class SalesReportDTO: ReportRoot
{
// Some data here
}
In ReportService Interface
public interface IReportService
{
public Task<GenericResponse<List<ReportRoot>>> Report();
}
public class SubscriptionReportService : IReportService {
public async Task<GenericResponse<List<ReportRoot>>> Report()
{
List<ReportRoot> subscriptionReportDTO = new List<ReportRoot>();
SubscriptionReportDTO test = new SubscriptionReportDTO();
test.SalesTax = "1000";
subscriptionReportDTO.Add(test);
return new GenericResponse<List<ReportRoot>>("1", subscriptionReportDTO.Count, "Success", subscriptionReportDTO);
}
}
public class SalesReportService : IReportService {
public async Task<GenericResponse<List<ReportRoot>>> Report()
{
List<ReportRoot> salesReportDTO = new List<ReportRoot>();
SalesReportDTO test = new SalesReportDTO ();
test.SalesTax = "1000";
salesReportDTO .Add(test);
return new GenericResponse<List<ReportRoot>>("1", salesReportDTO.Count, "Success", salesReportDTO );
}
}
In controller
private readonly IEnumerable<IReportService> _reportService;
public ReportController(IEnumerable<IReportService> reportService)
{
this._reportService = reportService;
}

Simple repository asp.net mvc with entity framework

I am about to start a small/medium sized project. I am by no means a software architect. But i tend to question every move i make at times. Since i want to do things correct.
I found a way to implement a simple repository, and i wanted to know if this is a "correct" way of doing it. I came to this solution, since i know what is going on, and not taking in something to complex before i have the knowledge :)
Here it goes.
Unit of work
Where i make sure i to keep all my repositories under the same dbcontext. In my uof i can access all repo's when calling it from the controller.
public class UnitOfWork : IDisposable
{
private ContactRepository _contactRepo;
private ApplicationDbContext _entities;
public UnitOfWork(ApplicationDbContext entities)
{
_entities = entities;
}
public ContactRepository ContactRepo
{
get
{
if (_contactRepo == null)
{
_contactRepo = new ContactRepository(_entities);
}
return _contactRepo;
}
}
public void Save()
{
_entities.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_entities.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
ContactRepository
This is a short example of a repository, where it recieves the dbcontext and uses it to grab whatever data i want
public class ContactRepository
{
private ApplicationDbContext _entities;
public ContactRepository(ApplicationDbContext entities)
{
_entities = entities;
}
public IEnumerable<Contact> GetAll()
{
return _entities.Contacts;
}
}
Controller
Short example of controller
public class ContactController : Controller
{
UnitOfWork uow = new UnitOfWork(new ApplicationDbContext());
public ActionResult Index()
{
var contacts = uow.ContactRepo.GetAll();
return View(contacts);
}
protected override void Dispose(bool disposing)
{
uow.Dispose();
base.Dispose(disposing);
}
}
In this way i will have access to all my repositories under the same dbcontext which i was aiming for.
I know things can be done smarter/different. With for example a extendable generic repo. But in this case i am aiming for something simple and understandable. But still dont want to make a huge mistake, if there is a major flaw.
Do you see any major flaws with this way of handling data trough entity framework?
If you're aiming for something simple then just use Entity Framework but if you're going to use the repository pattern I would encourage you to do it properly.
Two of the biggest motivators for using repository are:
You want to simplify CRUD applications to your database.This is
done through the use of interfaces and generics
You want to the ability to test the business logic in isolation
from external dependencies.Again, this is done through the use of
interfaces
Below will take you two minutes to implement but then at least you know you're doing it right, because at the moment you're trying to implement a great pattern in an ineffective way.
Generic interface:
public interface IRepository<T>
{
T GetById(int id);
IEnumerable<T> List();
IEnumerable<T> List(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Delete(T entity);
void Update(T entity);
}
Generic repository:
public abstract class EntityBase
{
}
public class DBRepository<T> : IRepository<T> where T : EntityBase
{
private readonly DbContext _dbContext;
public DBRepository(DbContext dbContext)
{
_dbContext = dbContext;
}
public virtual T GetById(int id)
{
return _dbContext.Set<T>().Find(id);
}
public virtual IEnumerable<T> List()
{
return _dbContext.Set<T>().AsEnumerable();
}
public virtual IEnumerable<T> List(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
return _dbContext.Set<T>()
.Where(predicate)
.AsEnumerable();
}
public void Add(T entity)
{
_dbContext.Set<T>().Add(entity);
}
public void Update(T entity)
{
_dbContext.Entry(entity).State = EntityState.Modified;
}
public void Delete(T entity)
{
_dbContext.Set<T>().Remove(entity);
}
Unit of work:
public class UnitOfWork : IDisposable
{
private bool disposed = false;
private ApplicationDbContext context = new ApplicationDbContext();
private IRepository<Contact> _contactRepository;
public IRepository<Contact> ContactRepository
{
get
{
if (this._contactRepository == null)
this._contactRepository = new DBRepository<Contact>(context);
return _contactRepository;
}
}
public void Save()
{
context.SaveChanges();
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
context.Dispose();
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}

DbSet, DbContext, EntityFramework

I am new to ASP.NET and very new to EF. I am trying to develop an application and after reading some sites I've decided I'm going to create a 3-tier application (DAL, BL, a website as the frontend).
For the DAL layer I've taken inspiration from here
http://codefizzle.wordpress.com/2012/07/26/correct-use-of-repository-and-unit-of-work-patterns-in-asp-net-mvc/
public interface IGenericRepository<T> where T : class
{
void Add(T a);
}
public interface IUnitOfWork:IDisposable
{
IGenericRepository<UserInfo> UserInfoRepository { get; }
void Commit();
}
public class EfGenericRepository<T> : IGenericRepository<T> where T : class
{
private DbSet<T> _dbSet;
public EfGenericRepository(DbSet<T> dbSet)
{
_dbSet = dbSet;
}
public void Add(T a)
{
_dbSet.Add(a);
}
}
public class EfUnitOfWork : DbContext, IUnitOfWork
{
private readonly EfGenericRepository<UserInfo> _userInfoRepo;
public DbSet<UserInfo> UserInfos { get; set; }
public EfUnitOfWork()
{
_userInfoRepo = new EfGenericRepository<UserInfo>(UserInfos);
}
public IGenericRepository<UserInfo> UserInfoRepository
{
get { return _userInfoRepo; }
}
public void Commit()
{
this.SaveChanges();
}
}
and my BL looks like this:
public interface IBussinessLogic
{
void AddUserInfo(string c);
}
public class BusinessLogic: IBussinessLogic
{
private IUnitOfWork _unitOfWork;
public BusinessLogic()
{
_unitOfWork = new EfUnitOfWork();
}
public void AddUserInfo(string c)
{
_unitOfWork.UserInfoRepository.Add(new UserInfo()
{
Address = c
});
_unitOfWork.Commit();
}
}
Now I am using web-forms but I don't think that should be an issue.
On click i execute this:
IBussinessLogic businessLogic = new BusinessLogic();
businessLogic.AddUserInfo(address.Text);
But nothing happens,my data is not saved in the db.
Can anyone please help me?

multiple IStateManager in StateManagedCollection

I was reading a post at VS 2008, ASP.NET: Generate Local Resources.
Mehdi Golchin showed us a beautiful job of StateManagedCollection.
However I was wondered about using multiple classes of IStateManager in one StateManagedCollection.
As you can see below:
public class MenuItemCollection : StateManagedCollection
{
public MenuItem this[int index]
{
get { return (MenuItem)((IList)this)[index]; }
}
public int Add(MenuItem item)
{
return ((IList)this).Add(item);
}
public void Remove(MenuItem item)
{
((IList)this).Remove(item);
}
// Write Insert and RemoveAt methods
protected override void SetDirtyObject(object o)
{
((MenuItem)o).SetDirty();
}
}
This MenuItemCollection class can have only one child class("MenuItem").
If I want to use another class as well as MenuItem class, for example MenuItem2 class, how do I have to write the codes?
Anyone can help me?
Thanks in advance.
Write a generic version - for example,
public class GenericStateManagedCollection<T> : StateManagedCollection
where T: IStateManager, new()
{
public T this[int index]
{
get { return (T)((IList)this)[index]; }
}
public int Add(T item)
{
return ((IList)this).Add(item);
}
public void Remove(T item)
{
((IList)this).Remove(item);
}
// Write Insert and RemoveAt methods
protected override void SetDirtyObject(object o)
{
((T)o).SetDirty();
}
protected override object CreateKnownType(int index)
{
return Activator.CreateInstance<T>();
}
protected override Type[] GetKnownTypes()
{
return new Type[] { typeof(T) };
}
}
And use it as
public class MenuItemCollection : GenericStateManagedCollection<MenuItem> { }
public class XyzItemCollection : GenericStateManagedCollection<XyzItem> { }
EDIT:
I have most probably mis-understood your question! Assuming now that you want to put two different type of objects into the StateManagedCollection. From usage perspective, it doesn't make sense to have objects of completely unrelated types into the collection - you need to have some base class. For example, consider DataControlFieldCollection which holds instances of (abstract) type 'DataControField. BoundField, ButtonField etc inherits fromDataControField`.
So you need to go via similar route - for example,
public class MenuItemBase : IStateManager
{
// Use implementation from link you quoted (Mehdi Golchin's answer)
...
}
public class MenuItem : MenuItemBase
{
...
}
public class MenuItem2 : MenuItemBase
{
...
}
public class MenuItemCollection : StateManagedCollection
{
public MenuItemBase this[int index]
{
get { return (MenuItemBase)((IList)this)[index]; }
}
public int Add(MenuItemBaseitem)
{
return ((IList)this).Add(item);
}
public void Remove(MenuItemBaseitem)
{
((IList)this).Remove(item);
}
// Write Insert and RemoveAt methods
protected override void SetDirtyObject(object o)
{
((MenuItemBase)o).SetDirty();
}
// important to override CreateKnownType and GetKnownTypes
private static readonly Type[] _knownTypes = new Type[] {typeof(MenuItem), typeof(MenuItem2) }
protected override Type[] GetKnownTypes()
{
return _knownTypes;
}
protected override object CreateKnownType(int index)
{
switch (index)
{
case 0:
return new MenuItem();
case 1:
return new MenuItem2();
default:
throw new Exception("Invalid Index");
}
}
}
Note: Untested code

Scheduling child activity that implements an interface with input parameters

public sealed class Parent : NativeActivity
{
public Parent()
{
Childrens = new Collection<Activity>();
Variables = new Collection<Variable>();
_currentActivityIndex = new Variable<int>();
CurrentCustomTypeInstance= new Variable<MyCustomType>();
}
[Browsable(false)]
public Collection<Activity> Childrens { get; set; }
protected override void Execute(NativeActivityContext context)
{
_currentActivityIndex.Set(context, 0);
context.ScheduleActivity(FirstActivity, Callback);
}
private void Callback(NativeActivityContext context, ActivityInstance completedInstance, MyCustomType customTypeInstance)
{
CurrentCustomTypeInstance.Set(context, customTypeInstance);
ScheduleNextChildren(context, completedInstance);
}
private void ScheduleNextChildren(NativeActivityContext context, ActivityInstance completedInstance)
{
int nextActivityIndex = _currentActivityIndex.Get(context) + 1;
if (nextActivityIndex >= Childrens.Count)
return;
Activity nextActivity = Childrens[nextActivityIndex];
IFoo nextActivityAsIFoo = nextActivity as IFoo;
if (nextActivityAsIFoo != null)
{
var currentCustomTypeInstance = CurrentCustomTypeInstance.Get(context);
// HERE IS MY EXCEPTION
nextActivityAsIFoo.FooField.Set(context, currentCustomTypeInstance);
}
context.ScheduleActivity(nextActivity);
_currentActivityIndex.Set(context, nextActivityIndex);
}
}
And in register metadata:
metadata.SetChildrenCollection(Childrens);
I've already read http://msmvps.com/blogs/theproblemsolver/archive/2011/04/05/scheduling-child-activities-with-input-parameters.aspx but in my case, parent does not know the child activity
Edit
Similar to: Activity cannot set a Variable defined within its scope?
Activity '1.1: Parent' cannot access this variable because it is declared at the scope
of activity '1.1: Parent'. An activity can only access its own implementation variables.
But in my case, I don't need to get the return value, so, hope to be easier. Just need to pass FooField implicitly instead of leting it to flow author.
I need to do it implicitly! If it doesn't work at all, I will go with NativeActivityContext Properties
Got it! Looking at the Maurice's blog I got inspired to do it
Workflow(very very simple!)
static void Main(string[] args)
{
Parent parent = new Parent();
parent.Childrens.Add(new FooWriter());
parent.Childrens.Add(new FooFormater());
parent.Childrens.Add(new FooWriter());
WorkflowInvoker.Invoke(parent);
Console.Read();
}
Output
What's the Foo name?
Implicit FTW!
Im a custom Foo Handler, my Foo name is: Implicit FTW!
Im a custom Foo Handler, my Foo name is: IMPLICIT FTW!
Implementation
using System;
using System.Activities;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WorkflowConsoleApplication2
{
// Parent class that creates a Foo and passes it to their childrens
public sealed class Parent : NativeActivity
{
private Variable<int> _currentActivityIndex;
private Variable<Foo> _currentFoo;
public Parent()
{
Childrens = new Collection<Activity>();
_executionChildrens = new Collection<Tuple<Activity, ActivityAction<Foo>>>();
_currentActivityIndex = new Variable<int>();
_currentFoo = new Variable<Foo>();
}
[Browsable(false)]
public Collection<Activity> Childrens { get; set; }
private Collection<Tuple<Activity, ActivityAction<Foo>>> _executionChildrens;
protected override void Execute(NativeActivityContext context)
{
Console.WriteLine("What's the Foo name?");
_currentFoo.Set(context, new Foo { Name = Console.ReadLine() });
_currentActivityIndex.Set(context, 0);
ScheduleNextChildren(context, null);
}
private void ScheduleNextChildren(NativeActivityContext context, ActivityInstance completedInstance)
{
int currentActivityIndex = _currentActivityIndex.Get(context);
if (currentActivityIndex >= Childrens.Count)
return;
Tuple<Activity, ActivityAction<Foo>> nextActivity = _executionChildrens[currentActivityIndex];
if (IsFooHandler(nextActivity))
{
context.ScheduleAction(nextActivity.Item2, _currentFoo.Get(context),
ScheduleNextChildren);
}
else
{
context.ScheduleActivity(nextActivity.Item1,
ScheduleNextChildren);
}
_currentActivityIndex.Set(context, currentActivityIndex + 1);
}
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
metadata.SetArgumentsCollection(metadata.GetArgumentsWithReflection());
metadata.AddImplementationVariable(_currentActivityIndex);
metadata.AddImplementationVariable(_currentFoo);
RegisterChildrens(metadata, Childrens);
// remove "base.Cachemetadata" to "Childrens collection" doesn't become a child again }
public void RegisterChildrens(NativeActivityMetadata metadata, IEnumerable<Activity> childrens)
{
foreach (Activity child in childrens)
{
IFooHandler childAsIFooHandler = child as IFooHandler;
if (childAsIFooHandler != null)
{
ActivityAction<Foo> childsWrapperAction = new ActivityAction<Foo>();
var activityToActionBinderArgument = new DelegateInArgument<Foo>();
childsWrapperAction.Argument = activityToActionBinderArgument;
childAsIFooHandler.Foo = activityToActionBinderArgument;
childsWrapperAction.Handler = child;
metadata.AddDelegate(childsWrapperAction);
_executionChildrens.Add(new Tuple<Activity, ActivityAction<Foo>>(child, childsWrapperAction));
}
else
{
metadata.AddChild(child);
_executionChildrens.Add(new Tuple<Activity, ActivityAction<Foo>>(child, null));
}
}
}
public static bool IsFooHandler(Tuple<Activity, ActivityAction<Foo>> activity)
{
return activity.Item2 != null;
}
}
// samples of Foo handlers
public class FooWriter : CodeActivity, IFooHandler
{
/// When FooWriter is direct child of "Parent" this argument is passed implicitly
public InArgument<Foo> Foo { get; set; }
protected override void Execute(CodeActivityContext context)
{
Console.WriteLine("Im a custom Foo Handler, my Foo name is: {0}", Foo.Get(context).Name);
}
}
public class FooFormater : CodeActivity, IFooHandler
{
public InArgument<Foo> Foo { get; set; }
protected override void Execute(CodeActivityContext context)
{
Foo foo = Foo.Get(context);
foo.Name = foo.Name.ToUpper();
}
}
// sample classes
public class Foo
{
public string Name { get; set; }
}
public interface IFooHandler
{
InArgument<Foo> Foo { get; set; }
}
}
If anyone know how to do it in a better way please fell free to tell me. I will also need to pass the values to nested activities

Resources