This doesn't sound like related to NHibernate. It's pure .NET.
I need to warn you. You are asking for one example to use Generics,
reference types, and in fact basic C# foundations. Maybe you need some
reading in these areas.
But this doesn't mean we can't go try to solve that particular situation
anyway :)
You can change your method to be: GetAll<TEntity>()
This way you can change the code to be like:
public IList<TEntity> GetAll<TEntity>() where TEntity : class
{
using (NHibernate.ISession session = OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
ICriteria crit = session.CreateCriteria( typeof(TEntity)
);
// I use QueryOVer not Criteria, but I think you can also
type session.CreateCriteria<TEntity>();
IList<TEntity> result = crit.List<TEntity>();
return result;
}
}
}
Your problem was that you don't return anything in the case of "MS". You
have to return something or the compiler will complain. This something must
be assigned a value not just declared (because it's reference type). One way
to do it is to return an empty collection for that specific case.
Let's add that to the function:
public IList<TEntity> GetAll<TEntity>() where TEntity : class
{
if( typeof(TEntity) == typeof(WhateverNameSpaceYouHave.IMS) )
return new List<WhateverNameSpaceYouHave.IMS>();
using (NHibernate.ISession session = OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
ICriteria crit = session.CreateCriteria( typeof(TEntity)
);
// I use QueryOVer not Criteria, but I think you can also
type session.CreateCriteria<TEntity>();
IList<TEntity> result = crit.List<TEntity>();
return result;
}
}
}
However, why you have all this logic in single place for all entities? Your
system design seems to have a serious problem that will face you in
maintaining code (for example, every type will have special condition and
your method will grow until it's pain to even read), and maybe even writing
and running the code originally as this one. You really need to check it
again!
I HIGHLY advise you to start a new thread with the details of how you
structure your classes for data access for friends over here to help you
spot the problems with that design and how to solve them.
*Mohamed Meligy
*Readify | Senior Developer
M:*+61 451 835006* | W: www.readify.net
[image: Description: Description: Description: Description:
rss_16]<http://gurustop.net/>
[image: Description: Description: Description: Description:
cid:[email protected]]
<http://www.linkedin.com/in/meligy> [image:
Description: Description: Description: Description:
cid:[email protected]] <http://twitter.com/meligy>
<http://www.readify.net/AboutUs/NewsItem.aspx?id=10>
On Sun, Sep 26, 2010 at 5:39 PM, adherence <[email protected]> wrote:
> Hey Everyone..
> i need to return the Ilist corresponding to the cases.
>
> i have a problem in switch.
> 1- when i m Returning a value , it gives error ("Not all
> paths return a value")
>
> and
>
> 2- when i put return outside the switch block or outside the
> case ,it gives error ("the retval doesnt exist in the current
>
> context")
>
> please help ..
>
>
> public object GetAll(object _oplObj)
> {
> using (NHibernate.ISession session = OpenSession())
> {
> using (ITransaction transaction =
> session.BeginTransaction())
> {
> ICriteria crit =
> session.CreateCriteria(_oplObj.GetType());
>
> switch (_oplObj.GetType().ToString())
> {
> case "IMS":
> break;
> case "TPF":
> IList<Jinnah.ObjectPersistanceLayer.TPF>
> retval = crit.List<Jinnah.ObjectPersistanceLayer.TPF>();
> return retval;
> break;
> }
>
> }
> }
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.