Get object from DAL via BLL into presentation via generics

I have a bunch of BLL objects which are directly mapped entities from the database in a model first scenario. I get theese objects from the DAL via BLL into the presentation layer via interfaces like this (from the BLL layer):

    public static ILanguage GetNewLanguage()
        return new Language();

    public static bool SaveLanguage(ILanguage language)
        return DAL.Repositories.LanguageRepository.Save(language);

And in the presentation layer I get the object simply by this call:

ILanguage language = BLL.Repository.GetNewLanguage();

Now as I have a bunch of objects, I would like to make the BLL methods generic, so I don't have to write the same code for every object.

But I am not sure how to do this. Any help is appriciated, thanks.



Creating a repository class for each entity type could result in a lot of redundant code .Just go through the following sample code to do that with generic repository pattern.


Well, it has been a while, sorry for that!

I succeeded in creating a generic method. Instead of this for every entity:

public static ILanguage GetNewLanguage()
        return new Language();

I am now using this generic method (but I think it is still a clumsy way to do this):

public static T CreateNew<T>(out string errmsg) where T : class
        errmsg = string.Empty;

        // Loading the DAL assembly as you cannot allways be sure that it is loaded,
        // as it can be used from the GAC and thereby not accessible as a loaded assembly.

        // From loaded assemblies get the DAL and get the specific class that implements 
        // the interface provided, <T>.
        // It is assumed for the time being, that only one BLL dataobject implements the interface.
        var type = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(s => s.GetTypes())
            .Where(p => typeof(T).IsAssignableFrom(p) && p.IsClass)

            // Create an instance of the class that implements the interface in question, unwrap it 
            // and send it back as the interface type.
            var s = Activator.CreateInstance("DAL", type.FullName);
            return (T)s.Unwrap();
        catch (Exception ex)
            errmsg = ex.ToString();
            return null;


The call from presentation layer now looks like this:

string errmsg = string.Empty;
ILanguage language = BLL.CreateNew<ILanguage>(out errmsg);

I solved the apparent problem, but still not in a fancy way. I have an idea of using DI to decouple assemblies from each other, but I am not sure how to do this. Comments are very wellcome. And I'll offcourse post a solution in a new thread if I find one.

And also I will post a solution to Crud's idea of how to decouple the BLL from the DAL with a repository class, in a new thread when I have figured that out!

Cheers Finn.

Need Your Help

Cannot create new Android application project or access Android preferences using Eclipse Kepler

android ubuntu eclipse-plugin eclipse-kepler

I am using Ubuntu 13.04 and Eclipse Keplar.When I try to create a new Android application project the following message is displayed: