[英]How to refactor method into generic base class implementation?
我已经实现了我的存储库类,但是我想知道如何将其方法重构为可以由不同存储库类型扩展的基类。
我从下面创建基本的Repository类开始,但是不确定每个方法应该有多抽象。 另外,我应该如何用通用占位符替换Model类型。
在此示例中 ,抽象方法仅包含方法定义,而不包含实现:
public abstract Array sort(Array arr);
谁能建议如何重构泛型方法?
我从创建基本抽象Repository类开始,但是被卡在了用通用类型和通用参数替换方法上。
下面的示例是Delete()
,它特定于CustomerModel。 它应该是通用的,以便于重用该类:
public abstract class BaseRepository
{
public async Task DeleteCustomer(CustomerModel customer)
{
var collection = StartConnection();
var filter = Builders<CustomerModel>.Filter.Where(x => x.Id == customer.Id);
var result = await collection.DeleteOneAsync(filter);
customers.Remove(customer);
}
}
因此,例如,这是完整的CustomerRepository类,其中包含针对我的远程数据库的CRUD操作。 这些方法都是特定于CustomerModel的,因此很难重用:
public class CustomerRepository : ICustomerRepository
{
private static List<CustomerModel> customers = new List<CustomerModel>();
static CustomerRepository()
{
}
private CustomerRepository()
{
}
public static CustomerRepository Instance
{
get
{
return instance;
}
}
public CustomerModel GetACustomer()
{
if (customers == null)
LoadCustomers();
return customers.FirstOrDefault();
}
public List<CustomerModel> GetCustomers()
{
if (customers.Count == 0)
LoadCustomers();
return customers;
}
public CustomerModel GetCustomerById(ObjectId id)
{
if (customers == null)
LoadCustomers();
return customers.Where(c => c.Id == id).FirstOrDefault();
}
public CustomerModel GetCustomerByEmail(string email)
{
if (customers == null)
LoadCustomers();
return customers.Where(c => c.Email == email).FirstOrDefault();
}
public async Task DeleteCustomer(CustomerModel customer)
{
var collection = StartConnection();
var filter = Builders<CustomerModel>.Filter.Where(x => x.Id == customer.Id);
var result = await collection.DeleteOneAsync(filter);
customers.Remove(customer);
}
public async Task AddCustomer(CustomerModel customer)
{
var collection = StartConnection();
await collection.InsertOneAsync(customer);
customers.Add(customer);
}
public async Task UpdateCustomer(CustomerModel customer)
{
var collection = StartConnection();
var filter = Builders<CustomerModel>.Filter.Where(x => x.Id == customer.Id);
collection.Find(filter).ToString();
var result = await collection.ReplaceOneAsync(filter, customer, new UpdateOptions { IsUpsert = true });
var index = customers.FindIndex(a => a.Id == customer.Id);
customers[index] = customer;
}
private void LoadCustomers()
{
var collection = StartConnection();
try
{
customers = collection.Find(new BsonDocument()).ToListAsync().GetAwaiter().GetResult();
}
catch (MongoException ex)
{
//Log exception here:
MessageBox.Show("A connection error occurred: " + ex.Message, "Connection Exception", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private static IMongoCollection<CustomerModel> StartConnection()
{
var client = new MongoClient(connectionString);
var database = client.GetDatabase("orders");
//Get a handle on the customers collection:
var collection = database.GetCollection<CustomerModel>("customers");
return collection;
}
}
不要使基类包含特定于实体的方法。 从一开始就使基础通用。
public class Repository<T> where T : new()
{
public async Task<T> GetAsync(object key)
{}
public async Task DeleteAsync(T t)
{}
}
添加您喜欢的任何抽象级别。 如果您在回购中使用了某种ORM,那么这就足够了。
您不能在继承类中更改方法名称。 如果所有操作都具有相同的方法,则使用repos更直接。
如果可以按原样使用base,则仅Type
参数会更改。 因此,您将在代码中实例化Repository<Customer>
。
如果实体之间的逻辑不同,则使用例如CustomerRepository : Repository<Customer>
并在其中放置逻辑。 同样,将base标记为abstract,将方法标记为abstract / virtual。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.