Creating Repositories

Mar 7, 2011 at 3:32 AM

I'm trying to create my first Datamodel using this software.  I downloaded and installed using VS2010 Extension manager.

My problem is that I create a partial class for one Enitity set using name of -  "Enitity Name" + "Repository.cs"  per directions on your blog.

What I'm not seeing is how the rest of the partial class is to be generated automatically?

Can you please explain what the procedure is?  

 

 

Coordinator
Apr 4, 2011 at 11:44 PM

I'm not sure if I understand your problem correctly.

Do you
a) have problems to use the repo generator?
b) or does the T4 templates do not generate all required files?

Apr 5, 2011 at 12:52 AM

I resolved the problem.  It was a case of my not understanding completely as to how it works.

I made one modification to the template though.  We use stylecop, so I added all of the comments that it expects into the template.

There is one thing you may want to consider adding to template.  I manually created a singleton class (see below) to handle instancing the repository.  By doing it this way, I hide all details about how to construct a repository.  I did it as a Singleton because of how the connection string was passed in.  But it does work fairly well this way.

Overall, this template is now a standard part of my projects.  An excellent piece of code. 

Thanks

Tom

 

 

        /// <summary>
        /// Private backing field for DayScheduleRepository.
        /// </summary>
        private static DayScheduleRepository privateDayScheduleRepository;

        /// <summary>
        /// Gets  DayScheduleRepository .
        /// </summary>
        public static DayScheduleRepository DayScheduleRepository
        {
            get
            {
                if (null == privateDayScheduleRepository)
                {
                    //Logger.Info("Creating Repository for DayScheduleRepository");
                    privateDayScheduleRepository = new DayScheduleRepository(new EfRepository<DaySchedule>() as IRepository<DaySchedule>, UnitOfWork);
                }

                return privateDayScheduleRepository;
            }
        }


        private static EfUnitOfWork unitOfWork;

        /// <summary>
        /// The unit of work.  This is the overall device context for the system.
        /// </summary>
        public static EfUnitOfWork UnitOfWork
        {
            get
            {
                if (unitOfWork == null)
                {
                   unitOfWork = new EfUnitOfWork();
                }
                return unitOfWork;
            }
            private set
            {
                unitOfWork = value;
            }
        }

        #region Methods

        /// <summary>
        /// The constructor helper.
        /// </summary>
        /// <param name="newDatabase">
        /// The new database.
        /// </param>
        private static void ConstructorHelper(string newDatabase)
        {
            string database2Open = string.IsNullOrEmpty(newDatabase) ? "VisServer" : newDatabase;
            string connectionString = ConnectiionString.GetSqlConnectionString(".\\sqlexpress", database2Open);
           // Logger.Info("Initialized database", Logger.VarName(() => database2Open));
            UnitOfWork = new EfUnitOfWork(connectionString);
            //Logger.Info("Database Context <EfUnitOfWork> Created");
        }

        #endregion
    }