TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Xxxsenatorxxx
1.7k
92
766
How to use service(BLL) in Form(windowsForm) With DI
Jan 15 2019 2:30 AM
I have a WindowsForm Project With this design :
DAL(GenericRepository => UnitOfWork) => BLL(Service) => UI
And EntityFramWork, Interface, GenericRepository, Dependency Injection
My Code in Repository(DAL) :
public
class
Repository
: RepositoryBase, IDisposable, IRepository
where T :
class
{
private
readonly
DbSet
dbSet;
private
bool
disposed =
false
;
public
Repository(GlobalERPEntities dbContext)
{
DBContext = dbContext;
dbSet = DBContext.Set
();
}
public
virtual
IEnumerable
GetAll()
{
return
dbSet.ToList();
}
UnitOfWork(DAL) :
public
class
UnitOfWork : RepositoryBase, IUnitOfWork, IDisposable
{
private
Dictionary
object
> repositories;
private
bool
disposed =
false
;
public
UnitOfWork(GlobalERPEntities dbContext)
{
DBContext = dbContext;
}
public
IRepository
Repository
() where T :
class
{
if
(repositories ==
null
)
{
repositories =
new
Dictionary
object
>();
}
if
(repositories.Keys.Contains(
typeof
(T)) ==
true
)
{
return
repositories[
typeof
(T)]
as
Repository
;
}
Repository
repo =
new
Repository
(DBContext);
repositories.Add(
typeof
(T), repo);
return
repo;
}
Service(BLL) :
public
class
Service_HR_Person : IService_HR_Person , IDisposable
{
private
readonly
IUnitOfWork UnitOfWork;
public
Service_HR_Person(IUnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
}
public
virtual
IEnumerable
GetAll()
{
return
UnitOfWork.Repository
().GetAll().ToList();
}
MyForm(UI) :
using
(Service_HR_Person SrvPerson =
new
Service_HR_Person())
{
SrvPerson.Delete(
base
.rowid);
try
{
SrvPerson.Save();
MessageManager.Show(Enums.MessageBoxType.InformationTransactionSuccessfully);
}
catch
(Exception ex)
{
MessageManager.Show(ErrorManager.ProccessException(ex), Enums.MessageBoxType.Error);
}
}
Iknow should not use DAL Layer in UI layer and BLL is between DAL and UI
but i have error in ui
using
(Service_HR_Person SrvPerson =
new
Service_HR_Person())
Service_HR_Person say need an arguman in () that is unitofwork but we should not use unitofwork in UI
please help me with code
thankyou
Reply
Answers (
2
)
Force subclass to implement private constructor
What is Gateway Design Pattern