repository-pattern

Using Unity, how do you register type mappings for generics?

送分小仙女□ 提交于 2019-11-28 12:07:36
I'm trying to implement a repository solution for Entity Framework but I am having trouble registering the types that include generics using Unity. Given: // IRepository interface public interface IRepository<TEntity> { // omitted for brevity } // Repository implementation public class Repository<TEntity, TContext> : IRepository<TEntity>, IDisposable where TEntity : class where TContext : DbContext { // omitted for brevity } // service layer constructor public MyServiceConstructor(IRepository<Account> repository) { _repository = repository; } I need to register the type mapping for IRepository

What is the point of the Update function in the Repository EF pattern?

ⅰ亾dé卋堺 提交于 2019-11-28 11:36:39
问题 I am using the repository pattern within EF using an Update function I found online public class Repository<T> : IRepository<T> where T : class { public virtual void Update(T entity) { var entry = this.context.Entry(entity); this.dbset.Attach(entity); entry.State = System.Data.Entity.EntityState.Modified; } } I then use it within a DeviceService like so: public void UpdateDevice(Device device) { this.serviceCollection.Update(device); this.uow.Save(); } I have realise that what this actually

Appropriate lifecycle for repository classes using Castle Windsor

ぐ巨炮叔叔 提交于 2019-11-28 10:17:51
问题 When I started with Windsor I thought DI would be simple. Now it's causing me more and more confusion. A repository strikes me as a class with a singleton lifecycle. I should have a single instance of a FooRepository to load and save Foos to the database during the application's lifetime. However, each repository holds a reference to a UnitOfWork, which does the dirty checking, works with the database etc. The UnitOfWork has a lifecycle of PerWebRequest - it makes no sense at all for the

Dependency Injection

非 Y 不嫁゛ 提交于 2019-11-28 09:05:27
问题 We are builing a windows desktop application (not web based) and trying to come up with the best way to implement Repository and UnitOfWork Pattern. In a typical Asp.Net Mvc application your repositories are injected with data context, services are injected with repositories and finally controllers are injected with services and all is well if you don't hit any exception, you will commit the changes. In windows forms/wpf applications it is not advisable to use single datacontext ( Oren has a

Pure POCO entity update problem in repository pattern

巧了我就是萌 提交于 2019-11-28 08:49:29
I have a problem in my UserRepository in which I want to update a user. I dont want certain fields updated, such as password, unless specified. For example, When I pass the User from the view, to the service to the repository, it sends up the user with a null or empty password string. This null gets written to the database (which I dont want). How do I handle a situation like this? Domain public class User { public int UserId { get; set; } public string Email { get; set; } public string Password { get; set; } } Repository public User Save(User user) { if (user.UserId > 0) { User dbUser =

Is this Repository pattern efficient with LINQ-to-SQL?

放肆的年华 提交于 2019-11-28 07:53:39
I'm currently reading the book Pro Asp.Net MVC Framework. In the book, the author suggests using a repository pattern similar to the following. [Table(Name = "Products")] public class Product { [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] public int ProductId { get; set; } [Column] public string Name { get; set; } [Column] public string Description { get; set; } [Column] public decimal Price { get; set; } [Column] public string Category { get; set; } } public interface IProductsRepository { IQueryable<Product> Products { get; } } public class

C#/EF and the Repository Pattern: Where to put the ObjectContext in a solution with multiple repositories?

有些话、适合烂在心里 提交于 2019-11-28 07:51:28
I have multiple repositories in my application. Where should I put the ObjectContext? Right now, I have a reference like ObjectContext ctx; in every repository. What is the smartest and safest way to go about this? A design with multiple ObjectContext instances is only acceptable if your Repository methods commit the transaction. Otherwise, it is possible that external calls to commit the transaction may not persist everything you intend, because you will hold references to different instances of the ObjectContext . If you want to restrict the ObjectContext to a single instance, then you can

Implementing retry logic for deadlock exceptions

谁说胖子不能爱 提交于 2019-11-28 06:46:14
I've implemented a generic repository and was wondering if there is a smart way to implement a retry logic in case of a deadlock exception? The approach should be the same for all repository methods. So is there anyway I can avoid writing 'try/catch - call method again with retry-count', in every single method? Any suggetsion are welcome. A bit of my Repository code: public class GenericRepository : IRepository { private ObjectContext _context; public List<TEntity> ExecuteStoreQuery<TEntity>(string commandText, params object[] parameters) where TEntity : class { List<TEntity> myList = new List

How do I correctly use Unity to pass a ConnectionString to my repository classes?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 05:27:18
I've literally just started using the Unity Application Blocks Dependency Injection library from Microsoft, and I've come unstuck. This is my IoC class that'll handle the instantiation of my concrete classes to their interface types (so I don't have to keep called Resolve on the IoC container each time I want a repository in my controller): public class IoC { public static void Intialise(UnityConfigurationSection section, string connectionString) { _connectionString = connectionString; _container = new UnityContainer(); section.Configure(_container); } private static IUnityContainer _container

Readonly properties in EF 4.1

自古美人都是妖i 提交于 2019-11-28 05:09:07
I've faced with situation when I need to have EF readonly property in case of 'optimistic update'(you do not load current state of your domain object from database to check what properties are really changed. You just set your object as Modified and update it to database. You avoid redundant select and merge operations in this case). You can't write something like this : DataContext.Entry(entity).Property(propertyName).IsModified = false; , because setting of 'false' value is not supported and you will get an exception. (in EF 4.1) I've created a simple structure for registering readonly