Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

The factory object is pretty easy. You just need a little class reflection:

public class RepositoryFactory
{
    public RepositoryFactory()
    {
        context = new ApplicationDbContext();
    }

    private ApplicationDbContext context;

    public object Create<T>()
    {
        Type repositoryType = typeof(T);
        ConstructorInfo constructor = repositoryType.GetConstructor(new Type[1] { typeof(ApplicationDbContext) });

        if (constructor == null)
            throw new InvalidOperationException("No Constructor found for " + repositoryType.FullName);

        return constructor.Invoke(new object[1] { context });
    }
}

I would also recommend added a where constraint to your type argument:

public object Create<T>() where T : IRepository<BaseEntityObject>
{
    // ...
}

Also notice that the ApplicationDbContext object is now a private field, which allows your repositories to share the same data context. This also keeps you from editing this class each time you create a new repository.


Now on to more pressing issues. I see lots of try-catch-swallow code -- code that catches exceptions and swallows them.

Please, for the sake of Future S. Peter do not do this!!

If an exception gets thrown in the data layer, let it bubble up. Don't swallow it -- even if you log it! Let the application blow sky high. If you add a record or folder and that operation fails, that is a catastrophic problem the deserves the immediate cessation of program logic.

See my related answer (along with some humor and sarcasm):

Returning status codes from business layerReturning status codes from business layer

The factory object is pretty easy. You just need a little class reflection:

public class RepositoryFactory
{
    public RepositoryFactory()
    {
        context = new ApplicationDbContext();
    }

    private ApplicationDbContext context;

    public object Create<T>()
    {
        Type repositoryType = typeof(T);
        ConstructorInfo constructor = repositoryType.GetConstructor(new Type[1] { typeof(ApplicationDbContext) });

        if (constructor == null)
            throw new InvalidOperationException("No Constructor found for " + repositoryType.FullName);

        return constructor.Invoke(new object[1] { context });
    }
}

I would also recommend added a where constraint to your type argument:

public object Create<T>() where T : IRepository<BaseEntityObject>
{
    // ...
}

Also notice that the ApplicationDbContext object is now a private field, which allows your repositories to share the same data context. This also keeps you from editing this class each time you create a new repository.


Now on to more pressing issues. I see lots of try-catch-swallow code -- code that catches exceptions and swallows them.

Please, for the sake of Future S. Peter do not do this!!

If an exception gets thrown in the data layer, let it bubble up. Don't swallow it -- even if you log it! Let the application blow sky high. If you add a record or folder and that operation fails, that is a catastrophic problem the deserves the immediate cessation of program logic.

See my related answer (along with some humor and sarcasm):

Returning status codes from business layer

The factory object is pretty easy. You just need a little class reflection:

public class RepositoryFactory
{
    public RepositoryFactory()
    {
        context = new ApplicationDbContext();
    }

    private ApplicationDbContext context;

    public object Create<T>()
    {
        Type repositoryType = typeof(T);
        ConstructorInfo constructor = repositoryType.GetConstructor(new Type[1] { typeof(ApplicationDbContext) });

        if (constructor == null)
            throw new InvalidOperationException("No Constructor found for " + repositoryType.FullName);

        return constructor.Invoke(new object[1] { context });
    }
}

I would also recommend added a where constraint to your type argument:

public object Create<T>() where T : IRepository<BaseEntityObject>
{
    // ...
}

Also notice that the ApplicationDbContext object is now a private field, which allows your repositories to share the same data context. This also keeps you from editing this class each time you create a new repository.


Now on to more pressing issues. I see lots of try-catch-swallow code -- code that catches exceptions and swallows them.

Please, for the sake of Future S. Peter do not do this!!

If an exception gets thrown in the data layer, let it bubble up. Don't swallow it -- even if you log it! Let the application blow sky high. If you add a record or folder and that operation fails, that is a catastrophic problem the deserves the immediate cessation of program logic.

See my related answer (along with some humor and sarcasm):

Returning status codes from business layer

added 697 characters in body
Source Link

The factory object is pretty easy. You just need a little class reflection:

public class RepositoryFactory
{
    public RepositoryFactory()
    {
        context = new ApplicationDbContext();
    }

    private ApplicationDbContext context;

    public object Create<T>()
    {
        Type repositoryType = typeof(T);
        ConstructorInfo constructor = repositoryType.GetConstructor(new Type[1] { typeof(ApplicationDbContext) });

        if (constructor == null)
            throw new InvalidOperationException("No Constructor found for " + repositoryType.FullName);

        return constructor.Invoke(new object[1] { context });
    }
}

I would also recommend added a where constraint to your type argument:

public object Create<T>() where T : IRepository<BaseEntityObject>
{
    // ...
}

Also notice that the ApplicationDbContext object is now a private field, which allows your repositories to share the same data context. This also keeps you from editing this class each time you create a new repository.


Now on to more pressing issues. I see lots of try-catch-swallow code -- code that catches exceptions and swallows them.

Please, for the sake of Future S. Peter do not do this!!

If an exception gets thrown in the data layer, let it bubble up. Don't swallow it -- even if you log it! Let the application blow sky high. If you add a record or folder and that operation fails, that is a catastrophic problem the deserves the immediate cessation of program logic.

See my related answer (along with some humor and sarcasm):

Returning status codes from business layer

The factory object is pretty easy. You just need a little class reflection:

public class RepositoryFactory
{
    public RepositoryFactory()
    {
        context = new ApplicationDbContext();
    }

    private ApplicationDbContext context;

    public object Create<T>()
    {
        Type repositoryType = typeof(T);
        ConstructorInfo constructor = repositoryType.GetConstructor(new Type[1] { typeof(ApplicationDbContext) });

        if (constructor == null)
            throw new InvalidOperationException("No Constructor found for " + repositoryType.FullName);

        return constructor.Invoke(new object[1] { context });
    }
}

I would also recommend added a where constraint to your type argument:

public object Create<T>() where T : IRepository<BaseEntityObject>
{
    // ...
}

Also notice that the ApplicationDbContext object is now a private field, which allows your repositories to share the same data context. This also keeps you from editing this class each time you create a new repository.

The factory object is pretty easy. You just need a little class reflection:

public class RepositoryFactory
{
    public RepositoryFactory()
    {
        context = new ApplicationDbContext();
    }

    private ApplicationDbContext context;

    public object Create<T>()
    {
        Type repositoryType = typeof(T);
        ConstructorInfo constructor = repositoryType.GetConstructor(new Type[1] { typeof(ApplicationDbContext) });

        if (constructor == null)
            throw new InvalidOperationException("No Constructor found for " + repositoryType.FullName);

        return constructor.Invoke(new object[1] { context });
    }
}

I would also recommend added a where constraint to your type argument:

public object Create<T>() where T : IRepository<BaseEntityObject>
{
    // ...
}

Also notice that the ApplicationDbContext object is now a private field, which allows your repositories to share the same data context. This also keeps you from editing this class each time you create a new repository.


Now on to more pressing issues. I see lots of try-catch-swallow code -- code that catches exceptions and swallows them.

Please, for the sake of Future S. Peter do not do this!!

If an exception gets thrown in the data layer, let it bubble up. Don't swallow it -- even if you log it! Let the application blow sky high. If you add a record or folder and that operation fails, that is a catastrophic problem the deserves the immediate cessation of program logic.

See my related answer (along with some humor and sarcasm):

Returning status codes from business layer

Source Link

The factory object is pretty easy. You just need a little class reflection:

public class RepositoryFactory
{
    public RepositoryFactory()
    {
        context = new ApplicationDbContext();
    }

    private ApplicationDbContext context;

    public object Create<T>()
    {
        Type repositoryType = typeof(T);
        ConstructorInfo constructor = repositoryType.GetConstructor(new Type[1] { typeof(ApplicationDbContext) });

        if (constructor == null)
            throw new InvalidOperationException("No Constructor found for " + repositoryType.FullName);

        return constructor.Invoke(new object[1] { context });
    }
}

I would also recommend added a where constraint to your type argument:

public object Create<T>() where T : IRepository<BaseEntityObject>
{
    // ...
}

Also notice that the ApplicationDbContext object is now a private field, which allows your repositories to share the same data context. This also keeps you from editing this class each time you create a new repository.