示例#1
0
        public void Setup()
        {
            var authorMapping = new EntityMapping
            {
                EntityType = typeof(Author),
                PrimaryKey = new PrimaryKeyMapping { ColumnName = "Id", PropertyInfo = typeof(Author).GetProperty("Id") }
            };

            authorMapping.AddColumn(new ColumnMapping { ColumnName = "FirstName", PropertyInfo = typeof(Author).GetProperty("FirstName") });
            authorMapping.AddColumn(new ColumnMapping { ColumnName = "MiddleName", PropertyInfo = typeof(Author).GetProperty("MiddleName") });
            authorMapping.AddColumn(new ColumnMapping { ColumnName = "LastName", PropertyInfo = typeof(Author).GetProperty("LastName") });

            var bookMapping = new EntityMapping
            {
                EntityType = typeof(Book),
                PrimaryKey = new PrimaryKeyMapping { ColumnName = "Id", PropertyInfo = typeof(Book).GetProperty("Id") }
            };

            bookMapping.AddColumn(new ColumnMapping { ColumnName = "Title", PropertyInfo = typeof(Book).GetProperty("Title") });
            bookMapping.AddColumn(new ColumnMapping { ColumnName = "Description", PropertyInfo = typeof(Book).GetProperty("Description") });

            bookMapping.AddRelation(new OneToOneRelationMapping { ColumnName = "AuthorId", PropertyInfo = typeof(Book).GetProperty("Author"), ReferenceType = typeof(Author) });

            mappings = new Dictionary<string, EntityMapping>
            {
                {"Author", authorMapping},
                {"Book", bookMapping}
            };

            var metadataStoreMock = new Mock<IMetadataStore>();
            var sessionMock = new Mock<ISession>();
            var sessionLevelCacheMock = new Mock<ISessionLevelCache>();

            metadataStoreMock.Setup(_ => _.GetMapping(It.IsAny<Type>())).Returns<Type>(_ => mappings[_.Name]);
            metadataStoreMock.Setup(_ => _.GetMapping(It.IsAny<string>())).Returns<string>(_ => mappings[_]);

            var cache = new Dictionary<Type, Dictionary<string, object>>();
            sessionLevelCacheMock.Setup(_ => _.Store(It.IsAny<Type>(), It.IsAny<object>(), It.IsAny<object>()))
                .Callback<Type, object, object>((entityType, entityKey, entity) =>
                {
                    Dictionary<string, object> entityCache;
                    if (!cache.TryGetValue(entityType, out entityCache))
                    {
                        cache.Add(entityType, entityCache = new Dictionary<string, object>());
                    }
                    entityCache.Add(entityKey.ToString(), entity);
                });
            sessionLevelCacheMock.Setup(_ => _.TryToFind(It.IsAny<Type>(), It.IsAny<object>()))
                .Returns<Type, object>((entityType, entityKey) =>
                {
                    Dictionary<string, object> entityCache;
                    if (!cache.TryGetValue(entityType, out entityCache))
                    {
                        return null;
                    }
                    object entity;
                    if (!entityCache.TryGetValue(entityKey.ToString(), out entity))
                    {
                        return null;
                    }
                    return entity;
                });

            hydrator = new EntityHydrator(metadataStoreMock.Object, sessionMock.Object, sessionLevelCacheMock.Object);

            connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabase"].ConnectionString);
            connection.Open();
            transaction = connection.BeginTransaction();
        }