SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)

    Question Cascading problems.

    I am finding cascades very confusing. Invariably I wind up with an error from the ORM telling me I have circular/multiple cascades paths.

    I'll tell you, at this point, I am VERY tempted to drop any and all use of an orm. Is there a better solution to all this that doesn't involve the coding overhead of raw ado.net?

  2. #2
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Bah...here's what I resorted to: using the EF orm, just without the 'R' part. In other words, the 'entities' really are one dimensional row storage units.

    It worked out very nice actually. Here's the code:

    Code Csharp:
    namespace Blog.Domain
    {
        public class Category
        {
            public int Id { get; protected set; }
            public string Name { get; set; }
        }
    }

    Code Csharp:
    using System.Data.Entity;
    using Blog.Domain;
    namespace Blog.Persistence
    {
        internal class Session : DbContext
        {
            public DbSet<Category> Categories { get; set; }
        }
    }

    Code Csharp:
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using Blog.Domain.Persistence;
    namespace Blog.Persistence
    {
        public class Repository<T> : IRepository<T>
            where T : class
        {
            public IEnumerable<T> Select()
            {
                using (var session = new Session())
                    return session.Set<T>().ToArray().AsEnumerable();
            }
            public void Insert(T entity)
            {
                using (var session = new Session())
                {
                    session.Entry(entity).State = EntityState.Added;
                    session.SaveChanges();
                }
            }
            public void Update(T entity)
            {
                using (var session = new Session())
                {
                    session.Entry(entity).State = EntityState.Modified;
                    session.SaveChanges();
                }
            }
            public void Delete(T entity)
            {
                using (var session = new Session())
                {
                    session.Entry(entity).State = EntityState.Deleted;
                    session.SaveChanges();
                }
            }
        }
    }

    Code Csharp:
    using System.Linq;
    using Blog.Domain;
    using Blog.Domain.Persistence;
    namespace Blog.Persistence
    {
        public class CategoryRepository : Repository<Category>, ICategoryRepository
        {
            public Category SelectById(int id)
            {
                using (var session = new Session())
                    return session.Set<Category>().SingleOrDefault(x => x.Id == id);
            }
     
        }
    }

    Now I don't have to worry about dumb collections, eager vs lazy loading, cross-context errors, and still retain the nice mapping ability of the orm, such as testing nullable field values before assignment, and the inclusion of linq.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •