मैं भी इस सवाल का था, मैं जानता हूँ कि यह नहीं है कि वास्तव में आप क्या चाहते हैं और आप और अधिक कोड लिखने की जरूरत है लेकिन यह उन लोगों की तुलना में बहुत साफ है जिन्हें आप लिखना चाहते हैं।
भंडार पैटर्न का उपयोग
प्रत्येक तालिका के लिए आपके पास मॉडल श्रेणी और रिपोजिटरी कक्षा होनी चाहिए।
इस कोड पर विचार करें (अपने प्रोजेक्ट में से एक से इस कोड)
यह मेरी टिप्पणी तालिका है (के साथ या बिना नेविगेशन संपत्ति इस कुछ भी हो सकता)
public sealed class Comment
{
public string CommentText { get; set; }
public DateTime PostDate { get; set; }
public int PostId { get; set; }
public int? PageId { get; set; }
public Page Page { get; set; }
public User User { get; set; }
public string UserId { get; set; }
public int? ParentId { get; set; }
public Comment[] ChildComments { get; set; }
}
RepositoryComment
public sealed class CommentRepository : BaseRepository<Comment>
{
public CommentRepository(BabySitterContext context)
: base(context)
{
}
}
और एक आधार कक्षा जिसे आप टेबल नाम (यहां मॉडल) और फ़ील्ड के साथ अपनी क्वेरी भेजते हैं (आप अधिक कार्यक्षमता के लिए क्लैस का विस्तार कर सकते हैं)
public class BaseRepository<T> where T : class
{
protected BabySitterContext Context;
private readonly PluralizationService _pluralizer = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en"));
public BaseRepository(BabySitterContext context)
{
this.Context = context;
}
public bool Add(T t)
{
Context.Set<T>().Add(t);
Context.SaveChanges();
return true;
}
public bool Update(T t)
{
var entityName = GetEntityName<T>();
object originalItem;
var key = ((IObjectContextAdapter)Context).ObjectContext.CreateEntityKey(entityName, t);
if (((IObjectContextAdapter)Context).ObjectContext.TryGetObjectByKey(key, out originalItem))
{
((IObjectContextAdapter)Context).ObjectContext.ApplyCurrentValues(key.EntitySetName, t);
}
Context.SaveChanges();
return true;
}
public void Attach(T t)
{
if (t == null)
{
throw new ArgumentNullException("t");
}
Context.Set<T>().Attach(t);
Context.SaveChanges();
}
public void Remove(T t)
{
if (t == null)
{
throw new ArgumentNullException("t");
}
Context.Set<T>().Remove(t);
Context.SaveChanges();
}
public IEnumerable<T> Get(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "")
{
IQueryable<T> query = Context.Set<T>();
if (filter != null)
{
query = query.Where(filter.Expand());
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
private string GetEntityName<TEntity>() where TEntity : class
{
return string.Format("{0}.{1}", ((IObjectContextAdapter)Context).ObjectContext.DefaultContainerName, _pluralizer.Pluralize(typeof(TEntity).Name));
}
public virtual IEnumerable<T> GetByBusinessKey(T entity)
{
return null;
}
}
किसी अन्य तालिका के लिए केवल आपके कार्यक्षमता उस तरह मन में पहली बात यह है प्रतिबिंब है की जरूरत है आधार वर्ग
से तो inherite मॉडल वर्ग और reposiotry बनाने कोड
var context = new BabySitterContext();
var _commentRepository = new CommentRepository(context);
var comment = _commentRepository.Get(x => x.PostId == id).FirstOrDefault();
अच्छी तरह से
का उपयोग करना (लेकिन IQueryable के अंदर) : var tableItem = db.table_Name.Where (p => p.Id = PK_val) .FirstOrDefault(); var x = tableItem.GetType()। GetProperty (FieldName) .GetValue (tabelItem, null); – Den
क्या आपने रिपोजिटरी पैटर्न का उपयोग करने पर विचार किया है? – Arash
@Arashjo नहीं, क्या आप मुझे रास्ता दिखा सकते हैं, जवाब दे सकते हैं ?? –