Public User GetUser(Expression> restriction) { return db.Users. Where(restriction).FirstOrDefault(); } Now use it: var you = Repository. GetUser(u => u.Logs.
Any(l => l.id. Equals(log. Id))) You can also use MS DynamicQuery : using System.Linq.
Dynamic; //... public User GetUser(string propertyName, int id) { var restriction = propertyName + ". Any(id = @0)"; return db.Users. Where(restriction, id).FirstOrDefault(); } var you = Repository.
GetUser("Logs", log. Id) I may not have the syntax quite correct, but you get the idea.
Public User GetUser(Expression> restriction) { return db.Users. Where(restriction).FirstOrDefault(); } Now use it: var you = Repository. GetUser(u => u.Logs.
Any(l => l.id. Equals(log. Id))); You can also use MS DynamicQuery: using System.Linq.
Dynamic; //... public User GetUser(string propertyName, int id) { var restriction = propertyName + ". Any(id = @0)"; return db.Users. Where(restriction, id).FirstOrDefault(); } var you = Repository.
GetUser("Logs", log. Id); I may not have the syntax quite correct, but you get the idea.
Great. But I need a "more automated" expression, maybe using the class name. – Sig.
Tolleranza Jan 21 '10 at 13:54 Then use MS Dynamic LINQ to do the same thing. I'll update the example. – Craig Stuntz Jan 21 '10 at 14:22 Thanks, I think I'll try the last one you suggest.
– Sig. Tolleranza Jan 25 '10 at 11:42.
If all the associated entities (Log, Product and Photo) will be searched by a common property (id INT) then maybe you could try something like this... First, create an interface: public interface IUserAssociation { int id { get; } } Then each of the three classes would implement this interface like so: public partial class Product : IUserAssociation { } The the GetUser method would look like so: public User GetUser(T entity) where T : IUserAssociation { var type = typeof(T); if (type == typeof(Log)) { return db.Users. FirstOrDefault(u => u.Logs. Any(l => l.id.
Equals(entity. Id))); } else if (type == typeof(Product)) { return db.Users. FirstOrDefault(u => u.Products.
Any(pr => pr.id. Equals(entity. Id))); } else if (type == typeof(Photo)) { return db.Users.
FirstOrDefault(u => u.Photos. Any(ph => ph.id. Equals(entity.Id))); } else { throw new ArgumentException(); } } Then you should be able to call GetUser and pass it a Log, Photo or Product entity from one method.
It is not very elegant but would work for this specific situation.
I like Craig's solution better but I'd suggest this: Repository. GetUser(u => u. Logs, log); Which will be possible if all your entities derive from public interface IEntity { public int Id { get; } } Then method will be like public User GetUser(Func> getlist, Y sample) where T: IEntity where Y: IEntity { return db.Users.
Select(x => getlist(x). Any(y => y. Id == sample.Id)).FirstOrDefault(); } Also if we take idea of S#arp Architecture that if entity1.
Id == entity2. Id (for persistent entities) then Equals(entity1, entity2) - we can use getlist(x). Contains(sample).
I'm using (Fluent) NHibernate, ASP.NET MVC 3 and am trying to apply the Repository pattern. I've decided to seperate my Business Logic into a seperate project and define services which wrap my repositories so that I can return POCOs instead of the NHibernate proxies and maintain more seperation between my Front end and DA logic. This will also give me the power to easily provide the same logic as an API later (a requirement).
I have chosen to use a generic IRepository interface where T is one of my NHibernate mapped Entities which all implement IEntity (my interface only a marker really). The problem is this goes against the aggregate root pattern and I'm starting to feel the pain of the anemic domain model. If I don't save the child first I get an exception from NHibernate.
I feel like my generic repository (I currently require 5 of them in one service) is not the right way to go. I feel like instead of making my code more flexible, it's making it more brittle. If I add a new table I need to go and change the constructor in all my tests (I'm using DI for the implementation so that's not too bad) but it seems a bit smelly.
Does anyone have any advice on how to restructure this sort of architecture? Should I be making my repositories more specific? Is the service abstraction layer a step too far?
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.