Quantcast
Channel: URF - Unit of Work & (extensible/generic) Repositories Framework
Viewing all articles
Browse latest Browse all 1539

New Post: Multiple Entity Framework Contexts(Multiple database types) on the same time.

$
0
0
Hi!

I had to use multiple contexts in a Windows Forms solution 4 months ago. I don't know if this would be helpful. At least, I think it's related to @Daniloloko's issue.
I used Ninject and this was my approach using attributes:
public class ProgramModule : NinjectModule
{
    public ProgramModule() { }

    public override void Load()
    {
        // Unit of Work & Generic Repository Patterns.
        Bind<IUnitOfWork>().To<UnitOfWork>().WhenTargetHas<InDirectorioEntitiesScope>().InSingletonScope();
        Bind<IUnitOfWork>().To<UnitOfWork>().WhenTargetHas<InGdEntitiesScope>().InSingletonScope();
        Bind<IUnitOfWork>().To<UnitOfWork>().WhenTargetHas<InVariosEntitiesScope>().InSingletonScope();

        // DataContexts: When any ancestor in the inheritance chain has been labeled with any of these attributes.
        Bind<IDataContext>().To<DirectorioEntities>()
            .WhenAnyAncestorMatches(Predicates.TargetHas<InDirectorioEntitiesScope>).InSingletonScope();
        Bind<IDataContext>().To<GdEntities>()
            .WhenAnyAncestorMatches(Predicates.TargetHas<InGdEntitiesScope>).InSingletonScope();
        Bind<IDataContext>().To<VariosEntities>()
            .WhenAnyAncestorMatches(Predicates.TargetHas<InVariosEntitiesScope>).InSingletonScope();
    }
}
These are the attributes...
namespace ExportGdInvoicesToSap.DependencyInjection
{
    public class InDirectorioEntitiesScope : Attribute { }
    public class InGdEntitiesScope : Attribute { }
    public class InVariosEntitiesScope : Attribute { }
}
These are the predicates...
public static class Predicates
{
    public static bool TargetHas<T>(IContext context)
    {
        return TargetHas<T>(context, false);
    }

    public static bool TargetHas<T>(IContext context, bool inherit)
    {
        var target = context.Request.Target;
        return target != null && target.IsDefined(typeof(T), inherit);
    }
}
Program...
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        // Configure Ninject Kernel
        CompositionRoot.Wire(new ProgramModule());
        IProgramKernel program = CompositionRoot.Resolve<ProgramKernel>();

        // Run App.
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm(program));
    }
}
Example of service...
public class PersonasService : Service, IPersonasService
{
    public PersonasService([InDirectorioEntitiesScope]IUnitOfWork unitOfWork)
        : base(unitOfWork)
    {
    }

    public Persona GetPersona(string iniciales) 
    {
        return UnitOfWork.Repository<Persona>().Query()
            .Filter(p => p.iniciales == iniciales).Get().FirstOrDefault();
    } 
}
I wish to take this opportunity to congratulate the team on the quality of the framework and their knowledge. You're awesome, guys!

Viewing all articles
Browse latest Browse all 1539

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>