<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using Caelan.Frameworks.BIZ.Interfaces;
using Caelan.Frameworks.DAL.Interfaces;

namespace Caelan.Frameworks.BIZ.Classes
{
    public abstract class BaseUnitOfWork : DynamicObject, IDisposable
    {
        private readonly Dictionary<string, BaseRepository> _repositories;

        protected BaseUnitOfWork()
        {
            _repositories = new Dictionary<string, BaseRepository>();
            //_repositories = (Assembly.GetCallingAssembly().GetTypes().Where(t => t.BaseType == typeof(BaseRepository)) ?? Assembly.GetCallingAssembly().GetReferencedAssemblies().OrderBy(t => t.Name).Select(Assembly.Load).SelectMany(assembly => assembly.GetTypes().Where(t => t.BaseType == typeof(BaseRepository)))).Select(t => new KeyValuePair<string, BaseRepository>(t.Name.Replace("Repository", string.Empty), Activator.CreateInstance(t.MakeGenericType(t.GetGenericArguments()), this) as BaseRepository)).ToDictionary(t => t.Key, t => t.Value);
        }

        protected abstract DbContext Context();

        internal DbSet<TEntity> GetDbSet<TEntity, TDTO, TKey>(BaseRepository<TEntity, TDTO, TKey> repository)
            where TEntity : class, IEntity<TKey>, new()
            where TDTO : class, IDTO<TKey>, new()
            where TKey : IEquatable<TKey>
        {
            return repository.DbSetFuncGetter().Invoke(Context());
        }

        public int SaveChanges()
        {
            return Context().SaveChanges();
        }

        public void Dispose()
        {
            //Context.Dispose();
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (!_repositories.ContainsKey(binder.Name))
            {
                var repoType = Type.GetType(binder.Name + "Repository");

                if (repoType == null)
                {
                    result = false;
                    return false;
                }

                var repo = Activator.CreateInstance(repoType, this) as BaseRepository;

                if (repo == null)
                {
                    result = null;
                    return false;
                }

                var property = Context().GetType().GetProperty(binder.Name);

                if (property == null)
                {
                    result = null;
                    return false;
                }

                if (!_repositories.ContainsKey(binder.Name))
                    _repositories.Add(binder.Name, repo);
                else
                    _repositories[binder.Name] = repo;
            }

            result = _repositories[binder.Name];

            return true;
        }

        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return _repositories.Keys;
        }
    }
}
<commit_msg>Changed BaseUOW for dynamic fixes<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Dynamic;
using System.Linq;
using Caelan.Frameworks.BIZ.Interfaces;
using Caelan.Frameworks.DAL.Interfaces;

namespace Caelan.Frameworks.BIZ.Classes
{
	public abstract class BaseUnitOfWork : DynamicObject, IDisposable
	{
		private readonly Dictionary<string, BaseRepository> _repositories;

		protected BaseUnitOfWork()
		{
			_repositories = GetType().Assembly.GetTypes().Where(t => t.BaseType == typeof(BaseRepository)).Select(t => new KeyValuePair<string, BaseRepository>(t.Name.Replace("Repository", string.Empty), Activator.CreateInstance(t.IsGenericType ? t.MakeGenericType(t.GetGenericArguments()) : t, this) as BaseRepository)).ToDictionary(t => t.Key, t => t.Value);
		}

		protected abstract DbContext Context();

		internal DbSet<TEntity> GetDbSet<TEntity, TDTO, TKey>(BaseRepository<TEntity, TDTO, TKey> repository)
			where TEntity : class, IEntity<TKey>, new()
			where TDTO : class, IDTO<TKey>, new()
			where TKey : IEquatable<TKey>
		{
			return repository.DbSetFuncGetter().Invoke(Context());
		}

		public int SaveChanges()
		{
			return Context().SaveChanges();
		}

		public void Dispose()
		{
			//Context.Dispose();
		}

		public override bool TryGetMember(GetMemberBinder binder, out object result)
		{
			if (!_repositories.ContainsKey(binder.Name))
			{
				var repoType = GetType().Assembly.GetTypes().FirstOrDefault(t => t.Name == binder.Name + "Repository");

				if (repoType == null)
				{
					result = false;
					return false;
				}

				var repo = Activator.CreateInstance(repoType, this) as BaseRepository;

				if (repo == null)
				{
					result = null;
					return false;
				}

				var property = Context().GetType().GetProperty(binder.Name);

				if (property == null)
				{
					result = null;
					return false;
				}

				if (!_repositories.ContainsKey(binder.Name))
					_repositories.Add(binder.Name, repo);
				else
					_repositories[binder.Name] = repo;
			}

			result = _repositories[binder.Name];

			return true;
		}

		public override IEnumerable<string> GetDynamicMemberNames()
		{
			return _repositories.Keys;
		}
	}
}

{'fraction_non_alphanumeric': 0.11233565351894818, 'fraction_numerical': 0.0, 'mean_word_length': 2.5553264604810995, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6978845', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1434, 'n_words': 301}